title: Apollo GraphQL Server authentication with passportJS
body: <p>I am trying to authenticate a user before allowing access to my '/graphql' endpoint.</p>
  
  <p>According to <a href="https://www.apollographql.com/docs/apollo-server/setup.html#graphqlOptions.context" rel="nofollow noreferrer">apollo-server</a> documentation regarding setting a context I can do something like this.</p>
  
  <pre><code>app.use(
    '/graphql',
    bodyParser.json(),
    graphqlExpress(req =&gt; {
      // Some sort of auth function
      const userForThisRequest = getUserFromRequest(req);
  
      return {
        schema: myGraphQLSchema,
        context: {
          user: userForThisRequest,
        },
        // other options here
      };
    }),
  );
  </code></pre>
  
  <p>I am trying to use passportJS's authenticate() function in the placeholder for "Some sort of auth function", but I can't seem to understand how to utilize the 'req' parameter that I have access to. Should I call passport.authenticate() after the bodyParser middleware or inside the graphqlExpress method?</p>
  
  <p>So my question is how can I use passportJS's authenticate mechanism in this context? Also, is this the best way to implement Authentication on Apollo-server?</p>