Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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}`);
});
};
// 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();
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,
__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,
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}`)
});
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}`);
});
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
);
}
}),
);
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 };
}
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
);
}
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 };
}