Skip to content

Commit

Permalink
feat: add ability to skip http cache on request (#332)
Browse files Browse the repository at this point in the history
Add ability to skip checking http cache

---------

Co-authored-by: A.J. Zozakiewicz <[email protected]>
Co-authored-by: Taylor Ninesling <[email protected]>
  • Loading branch information
3 people authored Jun 10, 2024
1 parent a2520b5 commit 8a4578d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/tiny-queens-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@apollo/datasource-rest": minor
---

Allow cache to be skipped on RestDataSource HTTP requests
5 changes: 4 additions & 1 deletion src/HTTPCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ export class HTTPCache<CO extends CacheOptions = CacheOptions> {
return { response: await this.httpFetch(urlString, requestOpts) };
}

const entry = await this.keyValueCache.get(cacheKey);
const entry =
requestOpts.skipCache !== true
? await this.keyValueCache.get(cacheKey)
: undefined;
if (!entry) {
// There's nothing in our cache. Fetch the URL and save it to the cache if
// we're allowed.
Expand Down
5 changes: 5 additions & 0 deletions src/RESTDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ export type RequestOptions<CO extends CacheOptions = CacheOptions> =
response: FetcherResponse,
request: RequestOptions<CO>,
) => ValueOrPromise<CO | undefined>);
/**
* Do not check the cache ignoring TTL or other cache settings
* Useful when you suspect a value may be cached but you want to force fresh data
*/
skipCache?: boolean;
/**
* If provided, this is passed through as the third argument to `new
* CachePolicy()` from the `http-cache-semantics` npm package as part of the
Expand Down
30 changes: 30 additions & 0 deletions src/__tests__/HTTPCache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,36 @@ describe('HTTPCache', () => {
expect(response.headers.get('age')).toEqual('10');
});

it('fetches fresh response when TTL not expired but skipCache true', async () => {
mockGetAdaLovelace({
'cache-control': 'private, no-cache',
'set-cookie': 'foo',
});

const { cacheWritePromise } = await httpCache.fetch(
adaUrl,
{},
{
cacheOptions: {
ttl: 30,
},
},
);

await cacheWritePromise;
jest.advanceTimersByTime(10000);

mockGetAlanTuring({
'cache-control': 'private, no-cache',
'set-cookie': 'foo',
});

const { response } = await httpCache.fetch(adaUrl, { skipCache: true });

expect(await response.json()).toEqual({ name: 'Alan Turing' });
expect(response.headers.get('age')).toBeNull();
});

it('fetches a fresh response from the origin when the overridden TTL expired', async () => {
mockGetAdaLovelace({
'cache-control': 'private, no-cache',
Expand Down

0 comments on commit 8a4578d

Please sign in to comment.