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 #6834

Merged
merged 3 commits into from Aug 19, 2022
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -10,7 +10,7 @@ The version headers in this history reflect the versions of Apollo Server itself

## vNEXT

- [apollo-datasource-rest] Add option to disable GET cache [PR #6650](https://github.com/apollographql/apollo-server/pull/6650)
- [apollo-datasource-rest] Add option to disable GET cache [PR #6650](https://github.com/apollographql/apollo-server/pull/6650) and [PR #6834](https://github.com/apollographql/apollo-server/pull/6834)

## v3.10.1

Expand Down
8 changes: 4 additions & 4 deletions docs/source/data/data-sources.mdx
Expand Up @@ -152,18 +152,18 @@ 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
Expand Down
8 changes: 4 additions & 4 deletions packages/apollo-datasource-rest/README.md
Expand Up @@ -69,18 +69,18 @@ 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
Expand Down
4 changes: 2 additions & 2 deletions packages/apollo-datasource-rest/src/RESTDataSource.ts
Expand Up @@ -50,7 +50,7 @@ export abstract class RESTDataSource<TContext = any> extends DataSource {
context!: TContext;
memoizedResults = new Map<string, Promise<any>>();
baseURL?: string;
requestCacheEnabled: boolean = true;
memoizeGetRequests: boolean = true;

constructor(private httpFetch?: typeof fetch) {
super();
Expand Down Expand Up @@ -268,7 +268,7 @@ export abstract class RESTDataSource<TContext = any> extends DataSource {

// 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
Expand Up @@ -639,7 +639,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 @@ -808,7 +808,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 Down Expand Up @@ -837,7 +837,7 @@ describe('RESTDataSource', () => {
it('allows setting a short TTL for the 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