graphql-mesh icon indicating copy to clipboard operation
graphql-mesh copied to clipboard

Inaccurate transformed document caching

Open hajnalben opened this issue 1 year ago • 2 comments

The caching part of the code for transformed documents in the legacy runtime is totally inaccurate. It doesn't prevent the call of applyRequestTransforms but instead of it overwrites its result.

Source: packages/legacy/runtime/src/useSubschema.ts

The code below is copied from the master branch ("version": "0.99.7")

const transformationContext: Record<string, any> = {};
const transformedRequest = applyRequestTransforms(
  originalRequest,
  delegationContext,
  transformationContext,
  subschema.transforms,
);
const cachedTransfomedDocumentNode: DocumentNode = transformedDocumentNodeCache.get(
  originalRequest.document,
);
if (cachedTransfomedDocumentNode) {
  transformedRequest.document = cachedTransfomedDocumentNode;
} else {
  transformedDocumentNodeCache.set(originalRequest.document, transformedRequest.document);
}

As you can see the applyRequestTransforms is called every time without checking the cache for match. After that the transformed document will be overwritten by the cache result. Also the caching logic doesn't consider transformers that produce different transformed documents based on the originalRequest.variables (example: @graphprotocol/client-auto-pagination)

hajnalben avatar May 28 '24 21:05 hajnalben

Would you create a reproduction?

ardatan avatar May 28 '24 21:05 ardatan

Reproduction repository

See the race1.ts and race2.ts in the repo. Run them separately and see the difference. They are executing the same query with different variables (A -> B | B -> A). You could debug the installed "@graphql-mesh/runtime": "0.99.7" in the node_modules and see the missfunctionality of the cache I described above. Also fixed it locally and based on that I have created the PR.

hajnalben avatar May 28 '24 22:05 hajnalben

See https://github.com/ardatan/graphql-mesh/pull/7039#issuecomment-2329180770

ardatan avatar Sep 04 '24 14:09 ardatan