You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
create an app context in which I parse a client ID from a JWT header
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.
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
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}.
The text was updated successfully, but these errors were encountered:
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.
Describe the bug
The workflow I want to use is:
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 propertysomeNumber
, and the intellisense in the IDE shows me that I am right to expect that, but it isn't there.Screenshots or Videos
Platform
@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 theschema
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 theschema
creation function is arequest
object, when as far as I can see it is actually of type{req, res, request}
.The text was updated successfully, but these errors were encountered: