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

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

Open
1 of 4 tasks
vinaybedre opened this issue Jun 12, 2024 · 3 comments
Open
1 of 4 tasks
Labels
kind/bug Something isn't working stage/1-reproduction A reproduction exists

Comments

@vinaybedre
Copy link

vinaybedre commented Jun 12, 2024

Issue workflow progress

Progress of the issue based on the
Contributor Workflow

  • 1. The issue provides a
    minimal reproduction available on
    Stackblitz.
    • Please install the latest @envelop/* packages that you are using.
    • Please make sure the reproduction is as small as possible.
  • 2. A failing test has been provided
  • 3. A local solution has been provided
  • 4. A pull request is pending review

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:

  • OS: macOS
  • NodeJS: v20.12.2
  • @envelop/* versions:
    "@envelop/response-cache": "^6.1.2"

Additional context

A reproducible repo is provided here: Stackblitz Reproduction

@EmrysMyrddin
Copy link
Collaborator

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.

@EmrysMyrddin EmrysMyrddin added kind/bug Something isn't working stage/1-reproduction A reproduction exists labels Jun 12, 2024
@vinaybedre
Copy link
Author

vinaybedre commented Jun 12, 2024

@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
Copy link
Author

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Something isn't working stage/1-reproduction A reproduction exists
Projects
None yet
Development

No branches or pull requests

2 participants