Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename property for RESTDataSource #5

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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