Skip to content

Commit

Permalink
Fix the cacheKey from #397 (#399)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilkisiela authored Sep 22, 2022
1 parent 50405d0 commit bd6e500
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/tricky-ducks-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-hive/client': patch
---

Fix the wrong cacheKey from #397
8 changes: 7 additions & 1 deletion packages/libraries/client/src/internal/usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,13 @@ export function createCollector({
};
}

return cache(collect, cacheDocumentKey, LRU<CacheResult>(max, ttl));
return cache(
collect,
function cacheKey(doc, variables) {
return cacheDocumentKey(doc, processVariables === true ? variables : null);
},
LRU<CacheResult>(max, ttl)
);
}

function resolveTypeName(inputType: GraphQLType): string {
Expand Down
2 changes: 2 additions & 0 deletions packages/libraries/client/src/internal/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export function cache<R, A, K, V>(
return {
key,
value: cachedValue,
cacheHit: true,
};
}

Expand All @@ -48,6 +49,7 @@ export function cache<R, A, K, V>(
return {
key,
value,
cacheHit: false,
};
};
}
Expand Down
69 changes: 69 additions & 0 deletions packages/libraries/client/tests/usage-collector.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit bd6e500

Please sign in to comment.