Skip to content

Commit

Permalink
fix type for in memory cache factory (#3000)
Browse files Browse the repository at this point in the history
* fix type for in memory cache factory

* changeset

* make TypeScript happy with a cast instead of nested function
  • Loading branch information
EmrysMyrddin authored Sep 27, 2023
1 parent cf77a9a commit caf7588
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/hungry-numbers-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-yoga/plugin-response-cache': patch
---

Fix incompatible type of in-memory cache factory with `cache` option
60 changes: 59 additions & 1 deletion packages/plugins/response-cache/__tests__/response-cache.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createSchema, createYoga } from 'graphql-yoga';
import { cacheControlDirective } from '@envelop/response-cache';
import { useResponseCache } from '@graphql-yoga/plugin-response-cache';
import { createInMemoryCache, useResponseCache } from '@graphql-yoga/plugin-response-cache';

const schema = createSchema({
typeDefs: /* GraphQL */ `
Expand Down Expand Up @@ -391,3 +391,61 @@ it('should work with @cacheControl directive', async () => {
},
});
});

it('should allow to create the cache outside of the plugin', async () => {
const onEnveloped = jest.fn();
const yoga = createYoga({
schema,
plugins: [
useResponseCache({
session: () => null,
includeExtensionMetadata: true,
cache: createInMemoryCache(),
}),
{
onEnveloped,
},
],
});
const response = await yoga.fetch('http://localhost:3000/graphql', {
method: 'POST',
headers: {
'content-type': 'application/json',
},
body: JSON.stringify({ query: '{ _ }' }),
});

expect(response.status).toEqual(200);
const body = await response.json();
expect(body).toEqual({
data: {
_: 'DUMMY',
},
extensions: {
responseCache: {
didCache: true,
hit: false,
ttl: null,
},
},
});
const response2 = await yoga.fetch('http://localhost:3000/graphql', {
method: 'POST',
headers: {
'content-type': 'application/json',
},
body: JSON.stringify({ query: '{ _ }' }),
});
const body2 = await response2.json();
expect(body2).toMatchObject({
data: {
_: 'DUMMY',
},
extensions: {
responseCache: {
hit: true,
},
},
});
expect(onEnveloped).toHaveBeenCalledTimes(1);
});
6 changes: 4 additions & 2 deletions packages/plugins/response-cache/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { ExecutionResult } from 'graphql';
import { Maybe, Plugin, PromiseOrValue, YogaInitialContext, YogaLogger } from 'graphql-yoga';
import {
BuildResponseCacheKeyFunction,
createInMemoryCache,
defaultBuildResponseCacheKey,
Cache as EnvelopCache,
createInMemoryCache as envelopCreateInMemoryCache,
ResponseCacheExtensions as EnvelopResponseCacheExtensions,
GetDocumentStringFunction,
useResponseCache as useEnvelopResponseCache,
Expand Down Expand Up @@ -160,4 +160,6 @@ export function useResponseCache(options: UseResponseCacheParameter): Plugin {
};
}

export { createInMemoryCache };
export const createInMemoryCache = envelopCreateInMemoryCache as (
...args: Parameters<typeof envelopCreateInMemoryCache>
) => Cache;

0 comments on commit caf7588

Please sign in to comment.