Last active
March 26, 2020 19:22
-
-
Save kluu1/573707b0e499a9778eee0362b5d361c9 to your computer and use it in GitHub Desktop.
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, AuthenticationError } = require('apollo-server'); | |
| const typeDefs = require('./graphql/typeDefs'); | |
| const resolvers = require('./graphql/resolver'); | |
| const getUser = require('./auth'); | |
| const server = new ApolloServer({ | |
| typeDefs, | |
| resolvers, | |
| context: ({ req }) => { | |
| // Get the user token from the headers | |
| const token = req.headers.authorization || ''; | |
| // throw error if token is missing | |
| if (!token) { | |
| throw new AuthenticationError('Required token is missing'); | |
| } | |
| // try to retrieve a user with the token | |
| const user = getUser(token); | |
| // add the user to the context | |
| return { user }; | |
| } | |
| }); | |
| 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