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

Apollo RESTDataSource is caching the error response #6438

Closed
psk-dreampay opened this issue May 17, 2022 · 1 comment
Closed

Apollo RESTDataSource is caching the error response #6438

psk-dreampay opened this issue May 17, 2022 · 1 comment

Comments

@psk-dreampay
Copy link

psk-dreampay commented May 17, 2022

I am using apollo in memory LRU caching along with RESTDataSource. This works fine in case of 200 response where the DS caches the response. But in case of any API error, the RESTDataSource is still caching the response. How can I disable the cache or set the TTL to 0 in case of an error?

import { RESTDataSource } from 'apollo-datasource-rest';

class UserDataSource extends RESTDataSource {
    constructor() {
        super();
        this.baseURL = 'https://myexample-api.com';
    }

    async getUsers() {
        return this.get('/users', {}, { cacheOptions: { ttl: 60 }).then(res);
    }
}
@glasser
Copy link
Member

glasser commented May 18, 2022

This is not well enough documented, but this is working as intended. RESTDataSource has two levels of caching: one which caches the response to a particular HTTP method within a single operation's execution, and one that writes to the shared cache. The second cache pays attention to HTTP error response codes; the first does not, assuming that an operation that returns an error will continue to return the same error if invoked during the same operation. See #1562 (comment) for some discussions of this.

(One could imagine that this is not the ideal behavior, though. RESTDataSource has not been actively maintained for a while but we hope to fix that this year.)

@glasser glasser closed this as completed May 18, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants