From bd6e500532ed4878a069883fadeaf3bb00e38aeb Mon Sep 17 00:00:00 2001 From: Kamil Kisiela Date: Thu, 22 Sep 2022 19:13:19 +0200 Subject: [PATCH] Fix the cacheKey from #397 (#399) --- .changeset/tricky-ducks-yell.md | 5 ++ .../libraries/client/src/internal/usage.ts | 8 ++- .../libraries/client/src/internal/utils.ts | 2 + .../client/tests/usage-collector.spec.ts | 69 +++++++++++++++++++ 4 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 .changeset/tricky-ducks-yell.md diff --git a/.changeset/tricky-ducks-yell.md b/.changeset/tricky-ducks-yell.md new file mode 100644 index 0000000000..663c593d20 --- /dev/null +++ b/.changeset/tricky-ducks-yell.md @@ -0,0 +1,5 @@ +--- +'@graphql-hive/client': patch +--- + +Fix the wrong cacheKey from #397 diff --git a/packages/libraries/client/src/internal/usage.ts b/packages/libraries/client/src/internal/usage.ts index 4351dfe0ef..b46dec3f21 100644 --- a/packages/libraries/client/src/internal/usage.ts +++ b/packages/libraries/client/src/internal/usage.ts @@ -401,7 +401,13 @@ export function createCollector({ }; } - return cache(collect, cacheDocumentKey, LRU(max, ttl)); + return cache( + collect, + function cacheKey(doc, variables) { + return cacheDocumentKey(doc, processVariables === true ? variables : null); + }, + LRU(max, ttl) + ); } function resolveTypeName(inputType: GraphQLType): string { diff --git a/packages/libraries/client/src/internal/utils.ts b/packages/libraries/client/src/internal/utils.ts index 2171d74de3..8fe0e3ddcb 100644 --- a/packages/libraries/client/src/internal/utils.ts +++ b/packages/libraries/client/src/internal/utils.ts @@ -39,6 +39,7 @@ export function cache( return { key, value: cachedValue, + cacheHit: true, }; } @@ -48,6 +49,7 @@ export function cache( return { key, value, + cacheHit: false, }; }; } diff --git a/packages/libraries/client/tests/usage-collector.spec.ts b/packages/libraries/client/tests/usage-collector.spec.ts index e005b028b7..f688ada2f8 100644 --- a/packages/libraries/client/tests/usage-collector.spec.ts +++ b/packages/libraries/client/tests/usage-collector.spec.ts @@ -239,6 +239,75 @@ test('collect all input fields when `processVariables` has not been passed and i expect(info.fields).toContain(`PaginationInput.offset`); }); +test('should get a cache hit when document is the same but variables are different (by default)', async () => { + const collect = createCollector({ + schema, + max: 1, + }); + const doc = parse(/* GraphQL */ ` + query getProjects($pagination: PaginationInput!, $type: ProjectType!) { + projects(filter: { pagination: $pagination, type: $type }) { + id + } + } + `); + const first = collect(doc, { + pagination: { + limit: 1, + }, + type: 'STITCHING', + }); + + const second = collect(doc, { + pagination: { + offset: 2, + }, + type: 'STITCHING', + }); + + expect(first.cacheHit).toBe(false); + expect(second.cacheHit).toBe(true); +}); + +test('(processVariables: true) should get a cache miss when document is the same but variables are different', async () => { + const collect = createCollector({ + schema, + max: 1, + processVariables: true, + }); + const doc = parse(/* GraphQL */ ` + query getProjects($pagination: PaginationInput!, $type: ProjectType!) { + projects(filter: { pagination: $pagination, type: $type }) { + id + } + } + `); + const first = collect(doc, { + pagination: { + limit: 1, + }, + type: 'STITCHING', + }); + + const second = collect(doc, { + pagination: { + offset: 2, + }, + type: 'STITCHING', + }); + + const third = collect(doc, { + pagination: { + offset: 2, + }, + type: 'STITCHING', + }); + + expect(first.cacheHit).toBe(false); + expect(second.cacheHit).toBe(false); + expect(third.cacheHit).toBe(true); +}); + test('(processVariables: true) collect used-only input fields', async () => { const collect = createCollector({ schema,