From 0e3076738b2e3ca1b7ce8dfb508644ae60a28a27 Mon Sep 17 00:00:00 2001 From: Shane Myrick Date: Mon, 22 Aug 2022 12:20:45 -0700 Subject: [PATCH 1/3] Rename property for RESTDataSource --- README.md | 29 ++++++++++++++-------------- src/RESTDataSource.ts | 4 ++-- src/__tests__/RESTDataSource.test.ts | 6 +++--- 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 1a2030c..f45bc41 100644 --- a/README.md +++ b/README.md @@ -65,25 +65,26 @@ 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; - } - // Outgoing requests are never cached, however the response cache is still enabled - async getMovie(id) { - return this.get( - `https://movies-api.example.com/movies/${encodeURIComponent(id)}` // path - ); - } + constructor() { + super(); + // Defaults to true + this.memoizeGetRequests = false; + } + + // Outgoing requests are never cached, however the response cache is still enabled + async getMovie(id) { + return this.get( + `https://movies-api.example.com/movies/${encodeURIComponent(id)}` // path + ); + } } ``` diff --git a/src/RESTDataSource.ts b/src/RESTDataSource.ts index afdd2a6..23cdd4f 100644 --- a/src/RESTDataSource.ts +++ b/src/RESTDataSource.ts @@ -58,7 +58,7 @@ export abstract class RESTDataSource { httpCache: HTTPCache; memoizedResults = new Map>(); baseURL?: string; - requestCacheEnabled: boolean = true; + memoizeGetRequests: boolean = true; constructor(config?: DataSourceConfig) { this.httpCache = new HTTPCache(config?.cache, config?.fetch); @@ -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; diff --git a/src/__tests__/RESTDataSource.test.ts b/src/__tests__/RESTDataSource.test.ts index 3a04107..3aef2da 100644 --- a/src/__tests__/RESTDataSource.test.ts +++ b/src/__tests__/RESTDataSource.test.ts @@ -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}`); @@ -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}`); @@ -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}`); From 94b7b21712a5722537dde28d0c20b493accadb22 Mon Sep 17 00:00:00 2001 From: Trevor Scheer Date: Mon, 22 Aug 2022 12:26:35 -0700 Subject: [PATCH 2/3] Add changeset --- .changeset/brave-cows-attend.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .changeset/brave-cows-attend.md diff --git a/.changeset/brave-cows-attend.md b/.changeset/brave-cows-attend.md new file mode 100644 index 0000000..54b1b31 --- /dev/null +++ b/.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. From 136326a831a8ac83fbe34a875266b73c0c2e59bc Mon Sep 17 00:00:00 2001 From: Trevor Scheer Date: Mon, 22 Aug 2022 12:28:51 -0700 Subject: [PATCH 3/3] align README code indentation --- README.md | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index f45bc41..43f8c2c 100644 --- a/README.md +++ b/README.md @@ -73,18 +73,18 @@ If you would like to disable the GET request cache, set the `memoizeGetRequests` ```js title="memoizeGetRequests.js" class MoviesAPI extends RESTDataSource { - constructor() { - super(); - // Defaults to true - this.memoizeGetRequests = false; - } + constructor() { + super(); + // Defaults to true + this.memoizeGetRequests = false; + } - // Outgoing requests are never cached, however the response cache is still enabled - async getMovie(id) { - return this.get( - `https://movies-api.example.com/movies/${encodeURIComponent(id)}` // path - ); - } + // Outgoing requests are never cached, however the response cache is still enabled + async getMovie(id) { + return this.get( + `https://movies-api.example.com/movies/${encodeURIComponent(id)}` // path + ); + } } ``` @@ -103,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 + } } ```