Skip to content

Instantly share code, notes, and snippets.

@kluu1
Last active March 26, 2020 19:22
Show Gist options
  • Select an option

  • Save kluu1/573707b0e499a9778eee0362b5d361c9 to your computer and use it in GitHub Desktop.

Select an option

Save kluu1/573707b0e499a9778eee0362b5d361c9 to your computer and use it in GitHub Desktop.
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