Context function passed to `createGraphQLHandler` runs only once on first request

If I pass context function to createGraphQLHandler, it is only called once on first request and then context is same for all subsequent requests. (when running yarn rw dev). I was expecting userContext to run on every request.

export const handler = createGraphQLHandler({
  context: userContext,
  schema: makeMergedSchema({
schemas,
services: makeServices({ services }),
  }),
  db,
})

That is 100% the expectation - could you share some code with me? I’m assuming userContext is a function?

Thanks @peterp for reply. I’m trying to read session from cookie and yes userContext is a function.

Here is the representative code, basically I’m trying to setup context based on cookie.

Hello @peterp - Can you please point me in right direction If above code is non how userContext should be used? Otherwise if it’s bug I can file bug report. Thanks!

Hey @nexneo

You’re almost on the right path, but what you’re looking for is getCurrentUser https://github.com/redwoodjs/playground-auth/blob/main/api/src/functions/graphql.js#L14

Instead of context.

Thanks @peterp How can I read event in getCurrentUser because event is not passed down to that function as per this code, https://github.com/redwoodjs/redwood/blob/main/packages/api/src/functions/graphql.ts#L46

In fact I considered that before and choose userContext function because it receives event and since event is request specific I thought it will be called everytime.

Ah ok, I see you’re trying to implement a custom solution! And I think there’s a shortcoming in my design!

I would love to make a second argument include the event - but in the meantime I’ll debug your current solution to figure out why it’s only firing once.

Thanks @peterp much appreciated.

I have tried to debug in a limited way and found two things if that helps you,

  1. if I pass getCurrentUser and context both to createGraphQLHandler. getCurrentUser is called on every request but context is called only on first request.
  2. If I don’t use createGraphQLHandler and create ApolloServer handler myself then context I passed will be called on every request.

And again this is when running dev-server. I don’t know how this behaves on AWS Lambda or Netlify functions

I found possible issue, userContext was overwritten after first run and this patch may be fix it. Although after patch I ran tests and all passed but before I submit pull request can you please check. You can check diff on following link,

https://github.com/redwoodjs/redwood/compare/main...nexneo:fix-context-overwrite-issue

@peterp with this bug same user will be logged in for all subsequent requests regardless of from where that request came in. I don’t know if anyone else is using context as a function but since it’s standard ApolloServer config it should be working with RedwoodJS

1 Like

Ah, super interesting! Thanks for this! Could you write a test for this?

1 Like

Thanks! I have added tests and created PR https://github.com/redwoodjs/redwood/pull/770

Please check.