-
Notifications
You must be signed in to change notification settings - Fork 568
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
feat(response-cache): accept serverContext
as the 2nd param in enabled
and session
#3285
base: main
Are you sure you want to change the base?
Changes from all commits
8f0fc3f
87003b3
286afc2
80a162d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'graphql-yoga': minor | ||
--- | ||
|
||
Now `onParams` hook's payload has `serverContext` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@graphql-yoga/plugin-response-cache': patch | ||
--- | ||
|
||
Now `enabled` and `session` factory functions take a second parameter `ServerContext` that includes the server specific context object. But this object is not the one provided by the user. [Learn more about the difference between the server context and the user context](https://the-guild.dev/graphql/yoga-server/docs/features/context#advanced-context-life-cycle) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,13 +20,13 @@ export type BuildResponseCacheKeyFunction = ( | |
}, | ||
) => ReturnType<EnvelopBuildResponseCacheKeyFunction>; | ||
|
||
export type UseResponseCacheParameter = Omit< | ||
export type UseResponseCacheParameter<TServerContext> = Omit< | ||
UseEnvelopResponseCacheParameter, | ||
'getDocumentString' | 'session' | 'cache' | 'enabled' | 'buildResponseCacheKey' | ||
> & { | ||
cache?: Cache; | ||
session: (request: Request) => PromiseOrValue<Maybe<string>>; | ||
enabled?: (request: Request) => boolean; | ||
session: (request: Request, serverContext?: TServerContext) => PromiseOrValue<Maybe<string>>; | ||
enabled?: (request: Request, serverContext?: TServerContext) => boolean; | ||
Comment on lines
+28
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know, the server context object passed here is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if it is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. because the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In what scenario is the server context undefined in real-world usage? |
||
buildResponseCacheKey?: BuildResponseCacheKeyFunction; | ||
}; | ||
|
||
|
@@ -82,7 +82,9 @@ export interface Cache extends EnvelopCache { | |
>; | ||
} | ||
|
||
export function useResponseCache(options: UseResponseCacheParameter): Plugin { | ||
export function useResponseCache<TServerContext extends Record<string, unknown>>( | ||
options: UseResponseCacheParameter<TServerContext>, | ||
): Plugin<TServerContext, TServerContext> { | ||
const buildResponseCacheKey: BuildResponseCacheKeyFunction = | ||
options?.buildResponseCacheKey || defaultBuildResponseCacheKey; | ||
const cache = options.cache ?? createInMemoryCache(); | ||
|
@@ -94,10 +96,10 @@ export function useResponseCache(options: UseResponseCacheParameter): Plugin { | |
}, | ||
onPluginInit({ addPlugin }) { | ||
addPlugin( | ||
useEnvelopResponseCache({ | ||
useEnvelopResponseCache<YogaInitialContext & TServerContext>({ | ||
...options, | ||
enabled({ request }) { | ||
return enabled(request); | ||
enabled(ctx: YogaInitialContext & TServerContext) { | ||
return enabled(ctx.request, ctx); | ||
}, | ||
cache, | ||
getDocumentString: getDocumentStringForEnvelop, | ||
|
@@ -129,8 +131,8 @@ export function useResponseCache(options: UseResponseCacheParameter): Plugin { | |
}), | ||
); | ||
}, | ||
async onRequest({ request, fetchAPI, endResponse }) { | ||
if (enabled(request)) { | ||
async onRequest({ request, serverContext, fetchAPI, endResponse }) { | ||
if (enabled(request, serverContext)) { | ||
const operationId = request.headers.get('If-None-Match'); | ||
if (operationId) { | ||
const cachedResponse = await cache.get(operationId); | ||
|
@@ -158,8 +160,8 @@ export function useResponseCache(options: UseResponseCacheParameter): Plugin { | |
} | ||
} | ||
}, | ||
async onParams({ params, request, setResult }) { | ||
const sessionId = await options.session(request); | ||
async onParams({ params, request, serverContext, setResult }) { | ||
const sessionId = await options.session(request, serverContext); | ||
const operationId = await buildResponseCacheKey({ | ||
documentString: params.query || '', | ||
variableValues: params.variables, | ||
|
@@ -169,7 +171,7 @@ export function useResponseCache(options: UseResponseCacheParameter): Plugin { | |
}); | ||
operationIdByRequest.set(request, operationId); | ||
sessionByRequest.set(request, sessionId); | ||
if (enabled(request)) { | ||
if (enabled(request, serverContext)) { | ||
const cachedResponse = await cache.get(operationId); | ||
if (cachedResponse) { | ||
const responseWithSymbol = { | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as https://github.com/dotansimha/graphql-yoga/pull/3285/files#r1607853806
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #3285 (comment)