Skip to content

Commit

Permalink
no need to graphql parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
n1ru4l committed Jul 28, 2022
1 parent 9fea97a commit 6478015
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ it('cache a query operation per session', async () => {
const yoga = createYoga({
plugins: [
useResponseCache({
session: (_, request) => request.headers.get('x-session-id') ?? null,
session: (request) => request.headers.get('x-session-id') ?? null,
includeExtensionMetadata: true,
}),
],
Expand Down
21 changes: 7 additions & 14 deletions packages/plugins/response-cache/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,14 @@ import {
useResponseCache as useEnvelopResponseCache,
UseResponseCacheParameter as UseEnvelopResponseCacheParameter,
} from '@envelop/response-cache'
import {
GraphQLParams,
Maybe,
Plugin,
PromiseOrValue,
YogaInitialContext,
} from 'graphql-yoga'
import { Maybe, Plugin, PromiseOrValue, YogaInitialContext } from 'graphql-yoga'

export type UseResponseCacheParameter = Omit<
UseEnvelopResponseCacheParameter,
'getDocumentString' | 'session'
> & {
session: (
params: GraphQLParams,
request: Request,
) => PromiseOrValue<Maybe<string>>
enabled?: (params: GraphQLParams, request: Request) => boolean
session: (request: Request) => PromiseOrValue<Maybe<string>>
enabled?: (request: Request) => boolean
}

const operationIdByRequest = new WeakMap<Request, string>()
Expand Down Expand Up @@ -49,12 +40,12 @@ export function useResponseCache(options: UseResponseCacheParameter): Plugin {
onRequestParse({ request }) {
return {
async onRequestParseDone({ params, setResult }) {
if (enabled(params, request)) {
if (enabled(request)) {
const operationId = await buildResponseCacheKey({
documentString: params.query!,
variableValues: params.variables,
operationName: params.operationName,
sessionId: await options.session(params, request),
sessionId: await options.session(request),
})
const cachedResponse = await cache.get(operationId)
if (cachedResponse) {
Expand All @@ -79,3 +70,5 @@ export function useResponseCache(options: UseResponseCacheParameter): Plugin {
},
}
}

export { createInMemoryCache }

0 comments on commit 6478015

Please sign in to comment.