Skip to content

Commit

Permalink
Handle a TTL of 0 correctly (#2588)
Browse files Browse the repository at this point in the history
* Handle a TTL of 0 correctly

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.

* Update CHANGELOG.md
  • Loading branch information
sebnow authored and abernix committed Apr 25, 2019
1 parent 0b602fd commit 0667c4b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
### vNEXT

- Allow `GraphQLRequestListener` callbacks in plugins to depend on `this`. [PR #2470](https://github.com/apollographql/apollo-server/pull/2470)
- Fix `Invalid argument` in IE11, when using `apollo-datasource-rest` when `this.headers` is `undefined`. [PR #2607](https://github.com/apollographql/apollo-server/pull/2607)
- `apollo-datasource-rest`: Correctly allow a TTL value of `0` to represent "not-cacheable". [PR #2588](https://github.com/apollographql/apollo-server/pull/2588)
- `apollo-datasource-rest`: Fix `Invalid argument` in IE11, when `this.headers` is `undefined`. [PR #2607](https://github.com/apollographql/apollo-server/pull/2607)

### v2.4.8

Expand Down
5 changes: 4 additions & 1 deletion packages/apollo-datasource-rest/src/HTTPCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ 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 0667c4b

Please sign in to comment.