Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the cacheKey from #397 #399

Merged
merged 3 commits into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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