Skip to content

Instantly share code, notes, and snippets.

@salwator
Created October 8, 2019 14:09
Show Gist options
  • Save salwator/a440874fae352f0181d6076d180b55c7 to your computer and use it in GitHub Desktop.
Save salwator/a440874fae352f0181d6076d180b55c7 to your computer and use it in GitHub Desktop.
Apollo Server for saleor federation with JWT
const { ApolloServer } = require("apollo-server");
const { ApolloGateway, RemoteGraphQLDataSource } = require("@apollo/gateway");
const gateway = new ApolloGateway({
serviceList: [
{ name: "saleor", url: "http://localhost:8000/graphql/" },
{ name: "reviews", url: "http://localhost:8080/" },
],
buildService({ name, url }) {
return new RemoteGraphQLDataSource({
url,
willSendRequest({ request, context }) {
request.http.headers.set('Authorization', context.Authorization);
},
});
},
});
(async () => {
const { schema, executor } = await gateway.load();
const server = new ApolloServer({
schema, executor, context: ({ req }) => {
return {
Authorization: req.headers.authorization || null
}
}
});
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment