How to use @apollo/gateway - 10 common examples

To help you get started, we’ve selected a few @apollo/gateway examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github kriswep / graphql-microservices / service-gateway / src / index.js View on Github external
import { ApolloServer } from 'apollo-server';
import { ApolloGateway } from '@apollo/gateway';

const PORT = process.env.PORT || 3000;

const postUrl = process.env.POST_URL || 'http://localhost:3010/graphql';
const userUrl = process.env.USER_URL || 'http://localhost:3020/graphql';

const gateway = new ApolloGateway({
  serviceList: [
    { name: 'post', url: postUrl },
    { name: 'user', url: userUrl }
    // more services here
  ]
});

const startGateway = async () => {
  const { schema, executor } = await gateway.load();

  const server = new ApolloServer({ schema, executor });

  server.listen(PORT).then(({ url }) => {
    console.log(`🚀 Server ready at ${url}`);
  });
};
github webiny / webiny-js / examples / functions / code / api-gateway / src / handler.js View on Github external
//     host = process.env.API_HOST;
    // }

    const services = [];
    Object.keys(process.env).forEach(key => {
        if (key.startsWith("SERVICE_")) {
            services.push({
                name: key.replace("SERVICE_", ""),
                url: process.env[key]
            });
        }
    });

    console.log("services", services);

    const gateway = new ApolloGateway({
        serviceList: services,
        buildService({ url }) {
            return new RemoteGraphQLDataSource({
                url,
                willSendRequest({ request, context }) {
                    Object.keys(context.headers).forEach(key => {
                        if (context.headers[key]) {
                            request.http.headers.set(key, context.headers[key]);
                        }
                    });
                }
            });
        }
    });

    const { schema, executor } = await gateway.load();
github juicycleff / ultimate-backend / apps / gateway-admin / src / simple.gateway.ts View on Github external
async function bootstrapSimple() {

  const gateway = new ApolloGateway({
    serviceList: [
      { name: 'auth', url: process.env.AUTH_ENDPOINT || 'http://localhost:9900/graphql' },
      { name: 'user', url: process.env.USER_ENDPOINT || 'http://localhost:9000/graphql' },
      { name: 'project', url: process.env.PROJECT_ENDPOINT || 'http://localhost:9100/graphql' },
      { name: 'tenant', url: process.env.TENANT_ENDPOINT || 'http://localhost:9200/graphql' },
      { name: 'payment', url: process.env.PAYMENT_ENDPOINT || 'http://localhost:9500/graphql' },
      // more services
    ],
    buildService({ url }) {
      return new HeadersDatasource({ url });
    },
  });

  const server = new ApolloServer({
    gateway,
    subscriptions: false,
github juicycleff / ultimate-backend / libs / nestjs-graphql-gateway / src / nestjs-graphql-distributed-gateway.module.ts View on Github external
__exposeQueryPlanExperimental,
        debug,
        // @ts-ignore
        serviceList,
        path,
        disableHealthCheck,
        onHealthCheck,
        cors,
        bodyParserConfig,
        installSubscriptionHandlers,
        buildService,
        ...rest
      },
    } = this;

    const gateway = new ApolloGateway({
      __exposeQueryPlanExperimental,
      debug,
      serviceList,
      buildService,
    });

    this.apolloServer = new ApolloServer({
      gateway,
      ...rest,
    });

    this.apolloServer.applyMiddleware({
      app,
      path,
      disableHealthCheck,
      onHealthCheck,
github ExpediaGroup / graphql-component / test / examples / federation / gateway / index.js View on Github external
const { ApolloServer } = require('apollo-server');
const { ApolloGateway } = require('@apollo/gateway');

const gateway = new ApolloGateway({
  serviceList: [
    { name: 'property', url: 'http://property:4000' },
    { name: 'reviews', url: 'http://reviews:4000' }
  ]
});

const server = new ApolloServer({ 
  gateway,
  subscriptions: false
});

server.listen().then(({ url }) => {
  console.log(`🚀 Server ready at ${url}`)
});
github preply / graphene-federation / integration_tests / federation / src / index.ts View on Github external
import {ApolloServer} from 'apollo-server'
import {ApolloGateway} from '@apollo/gateway'

const serviceA_url: string = 'http://service_a:5000/graphql';
const serviceB_url: string = 'http://service_b:5000/graphql';
const serviceC_url: string = 'http://service_c:5000/graphql';
const serviceD_url: string = 'http://service_d:5000/graphql';

const gateway = new ApolloGateway({
    serviceList: [
        { name: 'service_a', url: serviceA_url },
        { name: 'service_b', url: serviceB_url },
        { name: 'service_c', url: serviceC_url },
        { name: 'service_d', url: serviceD_url },
    ],
});

const server = new ApolloServer({
    gateway,
    subscriptions: false
});

server.listen(3000).then(({ url }) => {
  console.log(`🚀 Server ready at ${url}`);
});
github TheBrainFamily / federation-testing-tool / index.js View on Github external
function execute(schema, query, mutation, serviceMap, variables, context) {
  const operationContext = buildOperationContext(schema, query || mutation);
  const queryPlan = buildQueryPlan(operationContext);

  return executeQueryPlan(
    queryPlan,
    serviceMap,
    buildRequestContext(variables, context),
    operationContext
  );
}
github apollographql / apollo-server / packages / apollo-gateway / src / __tests__ / execution-utils.ts View on Github external
}),
  );

  let errors: GraphQLError[];

  ({ schema, errors } = composeAndValidate(
    Object.entries(serviceMap).map(([serviceName, service]) => ({
      name: serviceName,
      typeDefs: service.sdl(),
    })),
  ));

  if (errors && errors.length > 0) {
    throw new GraphQLSchemaValidationError(errors);
  }
  const operationContext = buildOperationContext(schema, request.query);

  const queryPlan = buildQueryPlan(operationContext);

  const result = await executeQueryPlan(
    queryPlan,
    serviceMap,
    {
      cache: undefined as any,
      context: {},
      request,
    },
    operationContext,
  );

  return { ...result, queryPlan };
}
github TheBrainFamily / federation-testing-tool / index.js View on Github external
function execute(schema, query, mutation, serviceMap, variables, context) {
  const operationContext = buildOperationContext(schema, query || mutation);
  const queryPlan = buildQueryPlan(operationContext);

  return executeQueryPlan(
    queryPlan,
    serviceMap,
    buildRequestContext(variables, context),
    operationContext
  );
}
github apollographql / apollo-server / packages / apollo-gateway / src / __tests__ / execution-utils.ts View on Github external
let errors: GraphQLError[];

  ({ schema, errors } = composeAndValidate(
    Object.entries(serviceMap).map(([serviceName, service]) => ({
      name: serviceName,
      typeDefs: service.sdl(),
    })),
  ));

  if (errors && errors.length > 0) {
    throw new GraphQLSchemaValidationError(errors);
  }
  const operationContext = buildOperationContext(schema, request.query);

  const queryPlan = buildQueryPlan(operationContext);

  const result = await executeQueryPlan(
    queryPlan,
    serviceMap,
    {
      cache: undefined as any,
      context: {},
      request,
    },
    operationContext,
  );

  return { ...result, queryPlan };
}