Skip to content

Commit

Permalink
Handle a TTL of 0 correctly
Browse files Browse the repository at this point in the history
When a TTL override of 0 is set, the override is overriden with the
policy due to using the || operator, which considers 0 to be false.
  • Loading branch information
sebnow committed Apr 18, 2019
1 parent 5a0e848 commit d24d497
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### vNEXT

- Allow `GraphQLRequestListener` callbacks in plugins to depend on `this`. [PR #2470](https://github.com/apollographql/apollo-server/pull/2470)
- Handle a TTL value of 0 as not cachable. [PR #2588](https://github.com/apollographql/apollo-server/pull/2588)

### v2.4.8

Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-datasource-rest/src/HTTPCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export class HTTPCache {
return response;
}

let ttl = ttlOverride || Math.round(policy.timeToLive() / 1000);
let ttl = ttlOverride === undefined ? Math.round(policy.timeToLive() / 1000) : ttlOverride;
if (ttl <= 0) return response;

// If a response can be revalidated, we don't want to remove it from the cache right after it expires.
Expand Down
15 changes: 15 additions & 0 deletions packages/apollo-datasource-rest/src/__tests__/HTTPCache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,21 @@ describe('HTTPCache', () => {
expect(await response.json()).toEqual({ name: 'Ada Lovelace' });
expect(response.headers.get('Age')).toEqual('10');
});

it('allows disabling caching when the TTL is 0 (falsy)', async () => {
fetch.mockJSONResponseOnce(
{ name: 'Ada Lovelace' },
{ 'Cache-Control': 'max-age=30' },
);

await httpCache.fetch(new Request('https://api.example.com/people/1'), {
cacheOptions: (response: Response, request: Request) => ({
ttl: 0,
}),
});

expect(store.size).toEqual(0);
});
});

it('allows specifying a custom cache key', async () => {
Expand Down

0 comments on commit d24d497

Please sign in to comment.