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

Rest DataSource Caching Errors #5608

Closed
cxl-todd opened this issue Aug 12, 2021 · 3 comments
Closed

Rest DataSource Caching Errors #5608

cxl-todd opened this issue Aug 12, 2021 · 3 comments

Comments

@cxl-todd
Copy link

I'm experiencing a similar issue as #1481. However, I have the latest rev: "apollo-datasource-rest": "^3.1.1" and it is clearly caching HTTP errors. Is there anyway to control the cache on a per-request level?

In my situation, our JWT auth expires and the subsequent call returns unauth 401. The code immediately requests a new JWT vi the login endpoint, but cannot resolve the original request because it has cached the 401 response.

@cxl-todd
Copy link
Author

Seems the issue is with memoization. Based on some points noted in this bug I added:

  didEncounterError(error, request) {
    this.memoizedResults.delete(this.cacheKeyFor(request));
    return super.didEncounterError(error, request);
  }

This seems to ensure that error responses are not held onto indefinitely.

A few takeaways:

  1. It fails the test of least surprise to see that errors are cached. Seems that this should not be the default behavior, imo.
  2. Memoization appears to cache across requests. This is unexpected per https://www.apollographql.com/docs/apollo-server/data/data-sources/#using-with-dataloader where it says:

...a memoization cache, which avoids loading the same object multiple times during a single GraphQL request.

My experience shows that across separate requests, the 401 response continues to be served up regardless.

Cheers

@alexandernanberg
Copy link

Where/how do you initialize your data sources? AFAIK they need to be inside the dataSources callback.

const app = new ApolloServer({
  schema: executableSchema,
  dataSources: () => ({
    api: new MyApi(),
  }),
});

If you do something like this the data source will only be created once, meaning memoization won't reset between requests

const dataSources = {
  api: new MyApi(),
};
const app = new ApolloServer({
  schema: executableSchema,
  dataSources: () => dataSources,
});

@glasser
Copy link
Member

glasser commented Sep 28, 2021

@alexandernanberg is correct: data sources are per-request. If the issue here is about long-lived data sources, then it should be closed. If this is a misunderstanding, happy to reopen.

@glasser glasser closed this as completed Sep 28, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
3 participants