From 34fd9deb00e6d1fbdb9f6fd5c7538b19ca11c43f Mon Sep 17 00:00:00 2001 From: Duncan Chen Date: Tue, 29 Oct 2019 08:48:58 -0500 Subject: [PATCH 1/2] Update authentication.md I follow this example on Azure Function and it failed. After a little digging, I found out the req object is passed as "request". There may be some inconsistency in implementation. --- docs/source/security/authentication.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/docs/source/security/authentication.md b/docs/source/security/authentication.md index 82fc4575577..0288cb4ca77 100644 --- a/docs/source/security/authentication.md +++ b/docs/source/security/authentication.md @@ -22,9 +22,12 @@ const { ApolloServer } = require('apollo-server'); const server = new ApolloServer({ typeDefs, resolvers, - context: ({ req }) => { + context: ({ request, req }) => { + + let combineReq = request || req + // get the user token from the headers - const token = req.headers.authorization || ''; + const token = combineReq.headers.authorization || ''; // try to retrieve a user with the token const user = getUser(token); From 19453920b541c8541fcda9cc4dc5dbbdb4a8fbde Mon Sep 17 00:00:00 2001 From: Jesse Rosenberger Date: Thu, 14 Nov 2019 11:08:41 +0200 Subject: [PATCH 2/2] Adjust #3455 to account for new content in #3506. This should hopefully make it more clear what information is available on the argument to `context`, and how it might need to be adjusted for each specific integration. --- docs/source/security/authentication.md | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/docs/source/security/authentication.md b/docs/source/security/authentication.md index 0288cb4ca77..674381c5334 100644 --- a/docs/source/security/authentication.md +++ b/docs/source/security/authentication.md @@ -22,16 +22,21 @@ const { ApolloServer } = require('apollo-server'); const server = new ApolloServer({ typeDefs, resolvers, - context: ({ request, req }) => { - - let combineReq = request || req - - // get the user token from the headers - const token = combineReq.headers.authorization || ''; - + context: ({ req }) => { + // Note! This example uses the `req` object to access headers, + // but the arguments received by `context` vary by integration. + // This means they will vary for Express, Koa, Lambda, etc.! + // + // To find out the correct arguments for a specific integration, + // see the `context` option in the API reference for `apollo-server`: + // https://www.apollographql.com/docs/apollo-server/api/apollo-server/ + + // Get the user token from the headers. + const token = req.headers.authorization || ''; + // try to retrieve a user with the token const user = getUser(token); - + // add the user to the context return { user }; },