Skip to content

Commit 1857515

Browse files
authored
Rename property for RESTDataSource (#5)
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
1 parent 67e0db8 commit 1857515

File tree

4 files changed

+20
-12
lines changed

4 files changed

+20
-12
lines changed

.changeset/brave-cows-attend.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@apollo/datasource-rest': minor
3+
---
4+
5+
Rename `requestCacheEnabled` to `memoizeGetRequests`. Acknowledging this is
6+
actually a breaking change, but this package has been live for a weekend with
7+
nothing recommending its usage yet.

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,20 @@ class MoviesAPI extends RESTDataSource {
6565
}
6666
```
6767

68-
##### `requestCacheEnabled`
68+
##### `memoizeGetRequests`
6969
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.
7070
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.
7171

72-
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.
72+
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.
7373

74-
```js title="requestCacheEnabled.js"
74+
```js title="memoizeGetRequests.js"
7575
class MoviesAPI extends RESTDataSource {
7676
constructor() {
7777
super();
7878
// Defaults to true
79-
this.requestCacheEnabled = false;
79+
this.memoizeGetRequests = false;
8080
}
81+
8182
// Outgoing requests are never cached, however the response cache is still enabled
8283
async getMovie(id) {
8384
return this.get(
@@ -102,9 +103,9 @@ Allows setting the `CacheOptions` to be used for each request/response in the HT
102103

103104
```javascript
104105
override cacheOptionsFor() {
105-
return {
106-
ttl: 1
107-
}
106+
return {
107+
ttl: 1
108+
}
108109
}
109110
```
110111

src/RESTDataSource.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export abstract class RESTDataSource {
5858
httpCache: HTTPCache;
5959
memoizedResults = new Map<string, Promise<any>>();
6060
baseURL?: string;
61-
requestCacheEnabled: boolean = true;
61+
memoizeGetRequests: boolean = true;
6262

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

261261
// Cache GET requests based on the calculated cache key
262262
// Disabling the request cache does not disable the response cache
263-
if (this.requestCacheEnabled) {
263+
if (this.memoizeGetRequests) {
264264
if (request.method === 'GET') {
265265
let promise = this.memoizedResults.get(cacheKey);
266266
if (promise) return promise;

src/__tests__/RESTDataSource.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ describe('RESTDataSource', () => {
590590
it('allows disabling the GET cache', async () => {
591591
const dataSource = new (class extends RESTDataSource {
592592
override baseURL = 'https://api.example.com';
593-
override requestCacheEnabled = false;
593+
override memoizeGetRequests = false;
594594

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

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

773773
const dataSource = new (class extends RESTDataSource {
774774
override baseURL = 'https://api.example.com';
775-
override requestCacheEnabled = false;
775+
override memoizeGetRequests = false;
776776

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

0 commit comments

Comments
 (0)