diff --git a/.changeset/tricky-ducks-yell.md b/.changeset/tricky-ducks-yell.md new file mode 100644 index 000000000..663c593d2 --- /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 4351dfe0e..b46dec3f2 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 2171d74de..8fe0e3ddc 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 e005b028b..f688ada2f 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,