n1ru4l / envelop

Envelop is a lightweight library allowing developers to easily develop, share, collaborate and extend their GraphQL execution layer. Envelop is the missing GraphQL plugin system.
https://envelop.dev
MIT License
771 stars 119 forks source link

[useResponseCache] Caching not working as expected when using federation + gateway #2252

Open vinaybedre opened 1 month ago

vinaybedre commented 1 month ago

Issue workflow progress

Progress of the issue based on the Contributor Workflow

Describe the bug

The caching via plugin useResponseCache is not working as expected when used in the gateway of a federated service. The cache does not store ObjectTypes of GraphQL at all, but only the etag call.

To Reproduce Steps to reproduce the behavior:

  1. Setup a federated service gateway.
  2. Implement the useResponseCache plugin.
  3. Perform GraphQL queries that should be cached.
  4. Observe that only the etag calls are cached, but ObjectTypes are not.

Expected behavior

The cache should store ObjectTypes of GraphQL responses, ensuring that repeated queries retrieve data from the cache as expected.

Environment:

Additional context

A reproducible repo is provided here: Stackblitz Reproduction

EmrysMyrddin commented 1 month ago

Hi! Thank you for raising this issue!

The problem seems to be the federation driver, which uses useApolloFederation plugin under the hood to setup a federation executor.

The problem is that this plugin is added at the end of the plugin list, which overrides the custom executor from the useResponseCache.

Do you have any warnings coming from the response cache plugin ? It should detect this case and warn about this problem.

To fix this, the federation yoga server should be updated to put the federation plugin at the start of the plugin list instead of the end.

vinaybedre commented 1 month ago

@EmrysMyrddin you are correct, I am getting a warning [useResponseCache] The cached execute function was not called, another plugin might have overwritten it. Please check your plugin order. But in federation, the plugin useApolloFederation is applied at the end as per this line https://github.com/dotansimha/graphql-yoga/blob/3d0cfd29fff92a98308b20b08c45aff68e0e9fa1/packages/nestjs-federation/src/index.ts#L111

or you mean to say that useApolloFederation must be at the start?

vinaybedre commented 1 month ago

@EmrysMyrddin I just created a patch by changing useApolloFederation to be the first plugin. You can see the changes at https://stackblitz.com/~/github.com/vinaybedre/envelop-response-cache-bug

After patching i see no warning, but caching is still not working.

image