Skip to content

Commit

Permalink
Ensure cache transform respects subfield arguments. (ardatan#1990)
Browse files Browse the repository at this point in the history
* Ensure cache transform respects subfield arguments.

* Add changeset file.
  • Loading branch information
galpin authored Apr 21, 2021
1 parent 271e4d2 commit 9f204b0
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/tricky-eyes-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-mesh/transform-cache': patch
---

Ensure cache transform respects subfield arguments.
2 changes: 1 addition & 1 deletion packages/transforms/cache/src/compute-cache-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function computeCacheKey(options: {
info: GraphQLResolveInfo;
}): string {
const argsHash = options.args ? objectHash(options.args, { ignoreUnknown: true }) : '';
const fieldsObj = graphqlFields(options.info);
const fieldsObj = graphqlFields(options.info, {}, { processArguments: true });
const fieldNamesHash = objectHash(fieldsObj, { ignoreUnknown: true });

if (!options.keyStr) {
Expand Down
59 changes: 59 additions & 0 deletions packages/transforms/cache/test/cache.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ const spies = {
});
}),
},
User: {
friend: jest.fn().mockImplementation((_, { id }) => {
return MOCK_DATA.find(u => u.id.toString() === id.toString());
}),
},
};

describe('cache', () => {
Expand Down Expand Up @@ -132,6 +137,7 @@ describe('cache', () => {
username: String!
email: String!
profile: Profile!
friend(id: ID!): User
}
type Profile {
Expand Down Expand Up @@ -476,5 +482,58 @@ describe('cache', () => {
expect(await cache.get(expectedCacheKey)).toBeDefined();
expect(spies.Query.user.mock.calls.length).toBe(2);
});

describe('Subfields', () => {
it('Should cache queries including subfield arguments', async () => {
const transform = new CacheTransform({
config: [{ field: 'Query.user' }],
cache,
pubsub,
baseDir,
});
const schemaWithCache = transform.transformSchema(schema);

// First query should call resolver and fill cache
const executeOptions1 = {
schema: schemaWithCache,
document: parse(/* GraphQL */ `
query {
user(id: 1) {
friend(id: 2) {
id
}
}
}
`),
};
const { data: actual1 } = await execute(executeOptions1);
expect(spies.Query.user.mock.calls.length).toBe(1);
expect(actual1.user.friend.id).toBe('2');

// Second query should call resolver and also fill cache
const executeOptions2 = {
schema: schemaWithCache,
document: parse(/* GraphQL */ `
query {
user(id: 1) {
friend(id: 3) {
id
}
}
}
`),
};
const { data: actual2 } = await execute(executeOptions2);
expect(spies.Query.user.mock.calls.length).toBe(2);
expect(actual2.user.friend.id).toBe('3');

// Repeat both queries, no new calls for resolver
const { data: repeat1 } = await execute(executeOptions1);
const { data: repeat2 } = await execute(executeOptions2);
expect(spies.Query.user.mock.calls.length).toBe(2);
expect(repeat1.user.friend.id).toBe('2');
expect(repeat2.user.friend.id).toBe('3');
});
});
});
});

0 comments on commit 9f204b0

Please sign in to comment.