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

@graphql-yoga/plugin-response-cache not yielding context to buildResponseCacheKey #3021

Closed
Tracked by #2700 ...
klippx opened this issue Sep 27, 2023 · 1 comment · Fixed by #2993
Closed
Tracked by #2700 ...

@graphql-yoga/plugin-response-cache not yielding context to buildResponseCacheKey #3021

klippx opened this issue Sep 27, 2023 · 1 comment · Fixed by #2993

Comments

@klippx
Copy link
Contributor

klippx commented Sep 27, 2023

Describe the bug

I am following the docs at https://the-guild.dev/graphql/yoga-server/docs/features/response-caching and trying to pass buildResponseCacheKey which is one of the configurable parameter options.

If you go to "Show type definition" this looks like this:

/**
 * Function for building the response cache key based on the input parameters
 */
export type BuildResponseCacheKeyFunction = (params: {
    /** Raw document string as sent from the client. */
    documentString: string;
    /** Variable values as sent form the client. */
    variableValues: ExecutionArgs['variableValues'];
    /** The name of the GraphQL operation that should be executed from within the document. */
    operationName?: Maybe<string>;
    /** optional sessionId for make unique cache keys based on the session.  */
    sessionId: Maybe<string>;
    /** GraphQL Context */
    context: ExecutionArgs['contextValue'];
}) => Promise<string>;

I.e context is supposed to be yielded to the function. However, this is always undefined. This is due to graphql-yoga not passing this context:

        async onParams({ params, request, setResult }) {
            const operationId = await buildResponseCacheKey({
                documentString: params.query || '',
                variableValues: params.variables,
                operationName: params.operationName,
                sessionId: await options.session(request),
            });

Your Example Website or App

https://codesandbox.io/p/sandbox/blissful-bouman-7jwxxn?file=%2Fsrc%2Fmain.ts%3A55%2C30

Steps to Reproduce the Bug or Issue

  1. Go to /graphql and send a request
  2. This will log context is undefined

Unfortuntely something with "etag" is not working with GraphiQL which is a separate issue, so this sandbox doesn't work.

Expected behavior

I want context yielded as per type definitions

Screenshots or Videos

No response

Platform

  • OS: [*]
  • NodeJS: [*]
  • @graphql-yoga/plugin-response-cache version(s): [e.g. 2.1.1]

Additional context

n/a

@EmrysMyrddin
Copy link
Collaborator

Hi, thank you for the report.

Yes, the type is currently not really accurate, sorry about that. A PR is open and will fix it. It will also give access to the HTTP request instead of the context (the context is not available in Yoga since the key has to be built before the context initialization).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants