@graphql-yoga/plugin-response-cache 2.1.1 → 2.1.2-rc-20230908124130-6372febf

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/cjs/index.js CHANGED
@@ -4,15 +4,20 @@ exports.createInMemoryCache = exports.useResponseCache = void 0;
4
4
  const response_cache_1 = require("@envelop/response-cache");
5
5
  Object.defineProperty(exports, "createInMemoryCache", { enumerable: true, get: function () { return response_cache_1.createInMemoryCache; } });
6
6
  const operationIdByRequest = new WeakMap();
7
- // We trick Envelop plugin by passing operationId as sessionId so we can take it from cache key builder we pass to Envelop
7
+ const sessionByRequest = new WeakMap();
8
8
  function sessionFactoryForEnvelop({ request }) {
9
- return operationIdByRequest.get(request);
9
+ return sessionByRequest.get(request);
10
10
  }
11
- const cacheKeyFactoryForEnvelop = async function cacheKeyFactoryForEnvelop({ sessionId }) {
12
- if (sessionId == null) {
11
+ const cacheKeyFactoryForEnvelop = async function cacheKeyFactoryForEnvelop({ context }) {
12
+ const request = context.request;
13
+ if (request == null) {
13
14
  throw new Error('[useResponseCache] This plugin is not configured correctly. Make sure you use this plugin with GraphQL Yoga');
14
15
  }
15
- return sessionId;
16
+ const operationId = operationIdByRequest.get(request);
17
+ if (operationId == null) {
18
+ throw new Error('[useResponseCache] This plugin is not configured correctly. Make sure you use this plugin with GraphQL Yoga');
19
+ }
20
+ return operationId;
16
21
  };
17
22
  const getDocumentStringForEnvelop = executionArgs => {
18
23
  const context = executionArgs.contextValue;
@@ -86,13 +91,15 @@ function useResponseCache(options) {
86
91
  }
87
92
  },
88
93
  async onParams({ params, request, setResult }) {
94
+ const sessionId = await options.session(request);
89
95
  const operationId = await buildResponseCacheKey({
90
96
  documentString: params.query || '',
91
97
  variableValues: params.variables,
92
98
  operationName: params.operationName,
93
- sessionId: await options.session(request),
99
+ sessionId,
94
100
  });
95
101
  operationIdByRequest.set(request, operationId);
102
+ sessionByRequest.set(request, sessionId);
96
103
  if (enabled(request)) {
97
104
  const cachedResponse = await cache.get(operationId);
98
105
  if (cachedResponse) {
package/esm/index.js CHANGED
@@ -1,14 +1,19 @@
1
1
  import { createInMemoryCache, defaultBuildResponseCacheKey, useResponseCache as useEnvelopResponseCache, } from '@envelop/response-cache';
2
2
  const operationIdByRequest = new WeakMap();
3
- // We trick Envelop plugin by passing operationId as sessionId so we can take it from cache key builder we pass to Envelop
3
+ const sessionByRequest = new WeakMap();
4
4
  function sessionFactoryForEnvelop({ request }) {
5
- return operationIdByRequest.get(request);
5
+ return sessionByRequest.get(request);
6
6
  }
7
- const cacheKeyFactoryForEnvelop = async function cacheKeyFactoryForEnvelop({ sessionId }) {
8
- if (sessionId == null) {
7
+ const cacheKeyFactoryForEnvelop = async function cacheKeyFactoryForEnvelop({ context }) {
8
+ const request = context.request;
9
+ if (request == null) {
9
10
  throw new Error('[useResponseCache] This plugin is not configured correctly. Make sure you use this plugin with GraphQL Yoga');
10
11
  }
11
- return sessionId;
12
+ const operationId = operationIdByRequest.get(request);
13
+ if (operationId == null) {
14
+ throw new Error('[useResponseCache] This plugin is not configured correctly. Make sure you use this plugin with GraphQL Yoga');
15
+ }
16
+ return operationId;
12
17
  };
13
18
  const getDocumentStringForEnvelop = executionArgs => {
14
19
  const context = executionArgs.contextValue;
@@ -82,13 +87,15 @@ export function useResponseCache(options) {
82
87
  }
83
88
  },
84
89
  async onParams({ params, request, setResult }) {
90
+ const sessionId = await options.session(request);
85
91
  const operationId = await buildResponseCacheKey({
86
92
  documentString: params.query || '',
87
93
  variableValues: params.variables,
88
94
  operationName: params.operationName,
89
- sessionId: await options.session(request),
95
+ sessionId,
90
96
  });
91
97
  operationIdByRequest.set(request, operationId);
98
+ sessionByRequest.set(request, sessionId);
92
99
  if (enabled(request)) {
93
100
  const cachedResponse = await cache.get(operationId);
94
101
  if (cachedResponse) {
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@graphql-yoga/plugin-response-cache",
3
- "version": "2.1.1",
3
+ "version": "2.1.2-rc-20230908124130-6372febf",
4
4
  "description": "",
5
5
  "peerDependencies": {
6
6
  "graphql": "^15.2.0 || ^16.0.0",
7
7
  "graphql-yoga": "^4.0.4"
8
8
  },
9
9
  "dependencies": {
10
- "@envelop/response-cache": "^5.1.0"
10
+ "@envelop/response-cache": "5.3.0-alpha-20230908121255-f3c216b2"
11
11
  },
12
12
  "repository": {
13
13
  "type": "git",
@@ -1,10 +1,12 @@
1
1
  import { ExecutionResult } from 'graphql';
2
2
  import { Maybe, Plugin, PromiseOrValue } from 'graphql-yoga';
3
- import { createInMemoryCache, Cache as EnvelopCache, ResponseCacheExtensions as EnvelopResponseCacheExtensions, UseResponseCacheParameter as UseEnvelopResponseCacheParameter } from '@envelop/response-cache';
4
- export type UseResponseCacheParameter = Omit<UseEnvelopResponseCacheParameter, 'getDocumentString' | 'session' | 'cache' | 'enabled'> & {
3
+ import { createInMemoryCache, BuildResponseCacheKeyFunction as EnvelopBuildResponseCacheKeyFunction, Cache as EnvelopCache, ResponseCacheExtensions as EnvelopResponseCacheExtensions, UseResponseCacheParameter as UseEnvelopResponseCacheParameter } from '@envelop/response-cache';
4
+ export type BuildResponseCacheKeyFunction = (params: Omit<Parameters<EnvelopBuildResponseCacheKeyFunction>[0], 'context'>) => ReturnType<EnvelopBuildResponseCacheKeyFunction>;
5
+ export type UseResponseCacheParameter = Omit<UseEnvelopResponseCacheParameter, 'getDocumentString' | 'session' | 'cache' | 'enabled' | 'buildResponseCacheKey'> & {
5
6
  cache?: Cache;
6
7
  session: (request: Request) => PromiseOrValue<Maybe<string>>;
7
8
  enabled?: (request: Request) => boolean;
9
+ buildResponseCacheKey?: BuildResponseCacheKeyFunction;
8
10
  };
9
11
  export interface ResponseCachePluginExtensions {
10
12
  http?: {
@@ -1,10 +1,12 @@
1
1
  import { ExecutionResult } from 'graphql';
2
2
  import { Maybe, Plugin, PromiseOrValue } from 'graphql-yoga';
3
- import { createInMemoryCache, Cache as EnvelopCache, ResponseCacheExtensions as EnvelopResponseCacheExtensions, UseResponseCacheParameter as UseEnvelopResponseCacheParameter } from '@envelop/response-cache';
4
- export type UseResponseCacheParameter = Omit<UseEnvelopResponseCacheParameter, 'getDocumentString' | 'session' | 'cache' | 'enabled'> & {
3
+ import { createInMemoryCache, BuildResponseCacheKeyFunction as EnvelopBuildResponseCacheKeyFunction, Cache as EnvelopCache, ResponseCacheExtensions as EnvelopResponseCacheExtensions, UseResponseCacheParameter as UseEnvelopResponseCacheParameter } from '@envelop/response-cache';
4
+ export type BuildResponseCacheKeyFunction = (params: Omit<Parameters<EnvelopBuildResponseCacheKeyFunction>[0], 'context'>) => ReturnType<EnvelopBuildResponseCacheKeyFunction>;
5
+ export type UseResponseCacheParameter = Omit<UseEnvelopResponseCacheParameter, 'getDocumentString' | 'session' | 'cache' | 'enabled' | 'buildResponseCacheKey'> & {
5
6
  cache?: Cache;
6
7
  session: (request: Request) => PromiseOrValue<Maybe<string>>;
7
8
  enabled?: (request: Request) => boolean;
9
+ buildResponseCacheKey?: BuildResponseCacheKeyFunction;
8
10
  };
9
11
  export interface ResponseCachePluginExtensions {
10
12
  http?: {