Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: Custom context not available in function to provide schema #2999

Closed
Tracked by #2700 ...
wwarby opened this issue Sep 14, 2023 · 1 comment · Fixed by #3004
Closed
Tracked by #2700 ...

bug: Custom context not available in function to provide schema #2999

wwarby opened this issue Sep 14, 2023 · 1 comment · Fixed by #3004
Assignees
Labels
stage/2-failing-test A failing test was created that describes the issue

Comments

@wwarby
Copy link

wwarby commented Sep 14, 2023

Describe the bug

The workflow I want to use is:

  1. create an app context in which I parse a client ID from a JWT header
  2. use the client ID from my app context to dynamically compose the schema (or get a previously composed schema from cache) because each client needs a different schema.

According to the TypeScript typings for graphql-yoga, this should work. The argument passed in to the function I set as the schema property should receive my custom app context as it's parameter. But it doesn't.

Your Example Website or App

https://codesandbox.io/p/sandbox/tender-elgamal-jgmnm9?file=/src/main.ts:17,59

Steps to Reproduce the Bug or Issue

Run the code sandbox example. Line 17 logs undefined to the console.

Expected behavior

I expect ctx to contain the property someNumber, and the intellisense in the IDE shows me that I am right to expect that, but it isn't there.

Screenshots or Videos

image

Platform

  • OS: [e.g. macOS, Windows, Linux]
  • NodeJS: [e.g. 18.1.0]
  • @graphql-yoga/* version(s): [e.g. 2.5.1]

Additional context

I can see that my context creation function is not called before the call to the schema creation function is made. This means I have no access to the context at schema creation time, which is inconvenient for my purposes. I'm not sure whether this behaviour is intentional, but if so the TypeScript type hints are definitely wrong. The documentation also seems to be wrong, suggesting that the argument passed into the schema creation function is a request object, when as far as I can see it is actually of type {req, res, request}.

@EmrysMyrddin
Copy link
Collaborator

Thank you for the report !

The documentation and the type are misleading, the actual parameter given to this function is the server context (which contains req and res by default) combined with a request property containing the HTTP Request.

You can't access your context here because it is not built at that phase of the execution. The GraphQL context is built after both Request parsing and schema initialisation.

We recommend to compute the data based on HTTP request in a dedicated function, like getSomeNumber(request) and use it both in schema function and context factory. If the data is heavy to compute, this function could cache it based on the request.

const someNumberCache = new WeakMap()
function getSomeNumber(request: Request) {
  if (!someNumberCache.has(request)) {
    someNumberCache.set(request, computeSomeNumber(request))
  }
  return someNumberCache.get(request)
}

@EmrysMyrddin EmrysMyrddin self-assigned this Sep 22, 2023
@EmrysMyrddin EmrysMyrddin added the stage/2-failing-test A failing test was created that describes the issue label Sep 22, 2023
This was referenced May 7, 2024
This was referenced May 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stage/2-failing-test A failing test was created that describes the issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants