Skip to content

Instantly share code, notes, and snippets.

@antonkalik
Created August 30, 2022 15:43
Show Gist options
  • Save antonkalik/566ccdf58d4c7e844d87ad1f2822c0b3 to your computer and use it in GitHub Desktop.
Save antonkalik/566ccdf58d4c7e844d87ad1f2822c0b3 to your computer and use it in GitHub Desktop.
Apollo Server with merging schemas
// the same imports as it was in boilerplate-graphql-koa-server-external
const { loadSchema } = require('@graphql-tools/load');
const { UrlLoader } = require('@graphql-tools/url-loader');
const { makeExecutableSchema, mergeSchemas } = require('@graphql-tools/schema');
const EXTERNAL_ENDPOINT = 'http://localhost:4000/api/v1/graphql';
async function server({ typeDefs, resolvers }) {
// app and httpServer the same like in boilerplate-graphql-koa-server-external
const internalSchema = makeExecutableSchema({
typeDefs,
resolvers,
});
const schemas = [internalSchema];
try {
const externalSchema = await loadSchema(EXTERNAL_ENDPOINT, {
loaders: [new UrlLoader()],
});
schemas.push(externalSchema);
} catch {
console.warn('⚠️️ External Schema has not been loaded');
}
const apolloServer = new ApolloServer({
introspection: true,
schema: mergeSchemas({
schemas,
}),
});
// the same start server how at boilerplate-graphql-koa-server-external
console.log(`🚀 Server ready at http://localhost:${process.env.PORT}${apolloServer.graphqlPath}`);
return { apolloServer, app };
}
// the same server call like in boilerplate-graphql-koa-server-external
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment