Apollo-server: Apollo server for koa passes wrong argument to context function?

Created on 9 Jan 2019  路  6Comments  路  Source: apollographql/apollo-server

Im using [email protected], In the documentation for authentication here:
https://www.apollographql.com/docs/apollo-server/features/authentication.html
The code example shows that the context function receives an object with the property req inside of it, however, the object passed to it looks like this:

{
  ctx: {
    req,
    ...etc
  }
}

So either the example is wrong, or most likely the argument passed to the context is.

Most helpful comment

Correct, documentation seems to not be in sync with the implementation of apollo-server-koa. I can verify that this implementation passes an object with a ctx property to the context function, as opposed to the ctx object itself.
I think this is a good thing too, and anyway couldn't be changed without breaking existing code, but it should probably be mentioned in the docs.

All 6 comments

.... or both are correct ;-)
It's just object destructuring. The code example only wants the req property of the passed object.

@chenglabs think you didn鈥檛 understand the issue.
The examples are wrong because the object doesn鈥檛 have a req property. It has a ctx property that has a req prop, or ctx.req, so the object destruction won鈥檛 work like in the examples.

Ah.. ok. I understand your issue.
I only know apollo-server-express, the object passed is
{ req,
..etc
}

Correct, documentation seems to not be in sync with the implementation of apollo-server-koa. I can verify that this implementation passes an object with a ctx property to the context function, as opposed to the ctx object itself.
I think this is a good thing too, and anyway couldn't be changed without breaking existing code, but it should probably be mentioned in the docs.

I notice this too, it doesn't bother me.
However i noticed that for subscriptions the ctx object
is not passed at all .. which is problematic for me .. since i use it
to attach my db connection.

const apollo = new ApolloServer({
  typeDefs,
  resolvers,
  mocks,
  //optional parameter
  context: async ({ connection, ctx }) => {
    debugger;
    let context = {}
    if (ctx){
      const { req } = ctx;
      //const { req } = ctx;
      context.db = ctx.app.context.db;
      if (connection) {
        // check connection for metadata
        return connection.context;
      } else {
        // check from req
        const token = req.headers.authorization || "";
        context.token = token;
      }
    }
    return context
  },
  onHealthCheck: () =>
    new Promise((resolve, reject) => {
      //database check or other asynchronous action
    }),
});

altho .. i just found a work around to it, at least for the db stuff ... i would still need access
to the req and headers

We won't be directly maintaining integrations with specific frameworks in Apollo Server 3, so closing this issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dbrrt picture dbrrt  路  3Comments

jminz picture jminz  路  3Comments

stevezau picture stevezau  路  3Comments

manuelfink picture manuelfink  路  3Comments

dupski picture dupski  路  3Comments