Skip to content

Commit

Permalink
Rename property for RESTDataSource (#5)
Browse files Browse the repository at this point in the history
Rename property for RESTDataSource from requestCacheEnabled to memoizeGetRequests

From the initial PR, the name of the new property was a little confusing since there are actually 
two caches, but what is being cached is the response data in the end.

Original PR #3
Back-port of apollographql/apollo-server#6834
  • Loading branch information
smyrick committed Aug 22, 2022
1 parent 67e0db8 commit 1857515
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
7 changes: 7 additions & 0 deletions .changeset/brave-cows-attend.md
@@ -0,0 +1,7 @@
---
'@apollo/datasource-rest': minor
---

Rename `requestCacheEnabled` to `memoizeGetRequests`. Acknowledging this is
actually a breaking change, but this package has been live for a weekend with
nothing recommending its usage yet.
15 changes: 8 additions & 7 deletions README.md
Expand Up @@ -65,19 +65,20 @@ class MoviesAPI extends RESTDataSource {
}
```

##### `requestCacheEnabled`
##### `memoizeGetRequests`
By default, `RESTDataSource` caches all outgoing GET **requests** in a separate memoized cache from the regular response cache. It makes the assumption that all responses from HTTP GET calls are cacheable by their URL.
If a request is made with the same cache key (URL by default) but with an HTTP method other than GET, the cached request is then cleared.

If you would like to disable the GET request cache, set the `requestCacheEnabled` property to `false`. You might want to do this if your API is not actually cacheable or your data changes over time.
If you would like to disable the GET request cache, set the `memoizeGetRequests` property to `false`. You might want to do this if your API is not actually cacheable or your data changes over time.

```js title="requestCacheEnabled.js"
```js title="memoizeGetRequests.js"
class MoviesAPI extends RESTDataSource {
constructor() {
super();
// Defaults to true
this.requestCacheEnabled = false;
this.memoizeGetRequests = false;
}

// Outgoing requests are never cached, however the response cache is still enabled
async getMovie(id) {
return this.get(
Expand All @@ -102,9 +103,9 @@ Allows setting the `CacheOptions` to be used for each request/response in the HT

```javascript
override cacheOptionsFor() {
return {
ttl: 1
}
return {
ttl: 1
}
}
```

Expand Down
4 changes: 2 additions & 2 deletions src/RESTDataSource.ts
Expand Up @@ -58,7 +58,7 @@ export abstract class RESTDataSource {
httpCache: HTTPCache;
memoizedResults = new Map<string, Promise<any>>();
baseURL?: string;
requestCacheEnabled: boolean = true;
memoizeGetRequests: boolean = true;

constructor(config?: DataSourceConfig) {
this.httpCache = new HTTPCache(config?.cache, config?.fetch);
Expand Down Expand Up @@ -260,7 +260,7 @@ export abstract class RESTDataSource {

// Cache GET requests based on the calculated cache key
// Disabling the request cache does not disable the response cache
if (this.requestCacheEnabled) {
if (this.memoizeGetRequests) {
if (request.method === 'GET') {
let promise = this.memoizedResults.get(cacheKey);
if (promise) return promise;
Expand Down
6 changes: 3 additions & 3 deletions src/__tests__/RESTDataSource.test.ts
Expand Up @@ -590,7 +590,7 @@ describe('RESTDataSource', () => {
it('allows disabling the GET cache', async () => {
const dataSource = new (class extends RESTDataSource {
override baseURL = 'https://api.example.com';
override requestCacheEnabled = false;
override memoizeGetRequests = false;

getFoo(id: number) {
return this.get(`foo/${id}`);
Expand Down Expand Up @@ -745,7 +745,7 @@ describe('RESTDataSource', () => {
it('allows setting cache options for each request', async () => {
const dataSource = new (class extends RESTDataSource {
override baseURL = 'https://api.example.com';
override requestCacheEnabled = false;
override memoizeGetRequests = false;

getFoo(id: number) {
return this.get(`foo/${id}`);
Expand All @@ -772,7 +772,7 @@ describe('RESTDataSource', () => {

const dataSource = new (class extends RESTDataSource {
override baseURL = 'https://api.example.com';
override requestCacheEnabled = false;
override memoizeGetRequests = false;

getFoo(id: number) {
return this.get(`foo/${id}`);
Expand Down

0 comments on commit 1857515

Please sign in to comment.