Created
October 8, 2019 14:09
-
-
Save salwator/a440874fae352f0181d6076d180b55c7 to your computer and use it in GitHub Desktop.
Apollo Server for saleor federation with JWT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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