apollo-datasource-rest
Advanced tools
Comparing version 3.6.1 to 3.7.0
@@ -27,5 +27,6 @@ import { Request, RequestInit, Response, BodyInit, Headers, URL, URLSearchParams, URLSearchParamsInit, fetch } from 'apollo-server-env'; | ||
memoizedResults: Map<string, Promise<any>>; | ||
baseURL?: string; | ||
memoizeGetRequests: boolean; | ||
constructor(httpFetch?: typeof fetch | undefined); | ||
initialize(config: DataSourceConfig<TContext>): void; | ||
baseURL?: string; | ||
protected cacheKeyFor(request: Request): string; | ||
@@ -32,0 +33,0 @@ protected willSendRequest?(request: RequestOptions): ValueOrPromise<void>; |
@@ -15,2 +15,3 @@ "use strict"; | ||
this.memoizedResults = new Map(); | ||
this.memoizeGetRequests = true; | ||
} | ||
@@ -149,12 +150,17 @@ initialize(config) { | ||
}; | ||
if (request.method === 'GET') { | ||
let promise = this.memoizedResults.get(cacheKey); | ||
if (promise) | ||
if (this.memoizeGetRequests) { | ||
if (request.method === 'GET') { | ||
let promise = this.memoizedResults.get(cacheKey); | ||
if (promise) | ||
return promise; | ||
promise = performRequest(); | ||
this.memoizedResults.set(cacheKey, promise); | ||
return promise; | ||
promise = performRequest(); | ||
this.memoizedResults.set(cacheKey, promise); | ||
return promise; | ||
} | ||
else { | ||
this.memoizedResults.delete(cacheKey); | ||
return performRequest(); | ||
} | ||
} | ||
else { | ||
this.memoizedResults.delete(cacheKey); | ||
return performRequest(); | ||
@@ -161,0 +167,0 @@ } |
{ | ||
"name": "apollo-datasource-rest", | ||
"version": "3.6.1", | ||
"version": "3.7.0", | ||
"author": "Apollo <packages@apollographql.com>", | ||
@@ -30,3 +30,3 @@ "license": "MIT", | ||
}, | ||
"gitHead": "36ecbb116cef0b8b62b3ee3b557c4db8c975a406" | ||
"gitHead": "c8ebdc7162a419cc4d00a90041cefbf2951535b6" | ||
} |
@@ -44,2 +44,76 @@ # Apollo REST Data Source | ||
## API Reference | ||
View the source code to see the all the properties and functions that can be overridden and their specific parameters. This section lists the usage and default behavior of the `RESTDatasource` class. | ||
### Constructor Parameters | ||
#### `httpFetch` | ||
Optional constructor option which allows overriding the `fetch` implementation used when calling data sources. | ||
### Properties | ||
#### `baseURL` | ||
Optional value to use for all the REST calls. If it is set in your class implementation, this base URL is used as the prefix for all calls. If it is not set, then the value passed to the REST call is exactly the value used. | ||
```js title="baseURL.js" | ||
class MoviesAPI extends RESTDataSource { | ||
constructor() { | ||
super(); | ||
this.baseURL = 'https://movies-api.example.com/'; | ||
} | ||
// GET | ||
async getMovie(id) { | ||
return this.get( | ||
`movies/${encodeURIComponent(id)}` // path | ||
); | ||
} | ||
} | ||
``` | ||
#### `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 `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="memoizeGetRequests.js" | ||
class MoviesAPI extends RESTDataSource { | ||
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 | ||
); | ||
} | ||
} | ||
``` | ||
### Methods | ||
#### `cacheKeyFor` | ||
By default, `RESTDatasource` uses the full request URL as the cache key. Override this method to remove query parameters or compute a custom cache key. | ||
For example, you could use this to use header fields as part of the cache key. Even though we do validate header fields and don't serve responses from cache when they don't match, new responses overwrite old ones with different header fields. | ||
#### `willSendRequest` | ||
This method is invoked just before the fetch call is made. If a `Promise` is returned from this method it will wait until the promise is completed to continue executing the request. | ||
#### `cacheOptionsFor` | ||
Allows setting the `CacheOptions` to be used for each request/response in the HTTPCache. This is separate from the request-only cache. | ||
#### `didReceiveResponse` | ||
By default, this method checks if the response was returned successfully and parses the response into the result object. If the response had an error, it detects which type of HTTP error and throws the error result. | ||
If you override this behavior, be sure to implement the proper error handling. | ||
#### `didEncounterError` | ||
By default, this method just throws the `error` it was given. If you override this method, you can choose to either perform some additional logic and still throw, or to swallow the error by not throwing the error result. | ||
## Examples | ||
### HTTP Methods | ||
@@ -46,0 +120,0 @@ |
@@ -52,2 +52,4 @@ import { | ||
memoizedResults = new Map<string, Promise<any>>(); | ||
baseURL?: string; | ||
memoizeGetRequests: boolean = true; | ||
@@ -63,4 +65,2 @@ constructor(private httpFetch?: typeof fetch) { | ||
baseURL?: string; | ||
// By default, we use the full request URL as the cache key. | ||
@@ -271,11 +271,17 @@ // You can override this to remove query parameters or compute a cache key in any way that makes sense. | ||
if (request.method === 'GET') { | ||
let promise = this.memoizedResults.get(cacheKey); | ||
if (promise) return promise; | ||
// Cache GET requests based on the calculated cache key | ||
// Disabling the request cache does not disable the response cache | ||
if (this.memoizeGetRequests) { | ||
if (request.method === 'GET') { | ||
let promise = this.memoizedResults.get(cacheKey); | ||
if (promise) return promise; | ||
promise = performRequest(); | ||
this.memoizedResults.set(cacheKey, promise); | ||
return promise; | ||
promise = performRequest(); | ||
this.memoizedResults.set(cacheKey, promise); | ||
return promise; | ||
} else { | ||
this.memoizedResults.delete(cacheKey); | ||
return performRequest(); | ||
} | ||
} else { | ||
this.memoizedResults.delete(cacheKey); | ||
return performRequest(); | ||
@@ -282,0 +288,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
55040
820
261