Fix the cacheKey from #397 (#399)

This commit is contained in:
Kamil Kisiela 2022-09-22 19:13:19 +02:00 committed by GitHub
parent 50405d0bb3
commit bd6e500532
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 83 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
'@graphql-hive/client': patch
---
Fix the wrong cacheKey from #397

View file

@ -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 {

View file

@ -39,6 +39,7 @@ export function cache<R, A, K, V>(
return {
key,
value: cachedValue,
cacheHit: true,
};
}
@ -48,6 +49,7 @@ export function cache<R, A, K, V>(
return {
key,
value,
cacheHit: false,
};
};
}

View file

@ -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,