Skip to content

Commit

Permalink
Added functionality to pass custom fetch to rest data source
Browse files Browse the repository at this point in the history
This is an optional constructor parameter. If a custom fetch function is not defined, the default fetch will be used.

This new functionality has been covered in the http cache tests.

Co-Authored-By: Jae Anne Bach Hardie <j@limina.net>
  • Loading branch information
Jae Anne Bach Hardie and Jae Anne Bach Hardie committed Oct 12, 2018
1 parent f920f5f commit 07e2f4c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
9 changes: 6 additions & 3 deletions packages/apollo-datasource-rest/src/HTTPCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import { KeyValueCache, InMemoryLRUCache } from 'apollo-server-caching';
import { CacheOptions } from './RESTDataSource';

export class HTTPCache {
constructor(private keyValueCache: KeyValueCache = new InMemoryLRUCache()) {}
constructor(
private keyValueCache: KeyValueCache = new InMemoryLRUCache(),
private httpFetch: typeof fetch = fetch,
) {}

async fetch(
request: Request,
Expand All @@ -21,7 +24,7 @@ export class HTTPCache {

const entry = await this.keyValueCache.get(`httpcache:${cacheKey}`);
if (!entry) {
const response = await fetch(request);
const response = await this.httpFetch(request);

const policy = new CachePolicy(
policyRequestFrom(request),
Expand Down Expand Up @@ -61,7 +64,7 @@ export class HTTPCache {
const revalidationRequest = new Request(request, {
headers: revalidationHeaders,
});
const revalidationResponse = await fetch(revalidationRequest);
const revalidationResponse = await this.httpFetch(revalidationRequest);

const { policy: revalidatedPolicy, modified } = policy.revalidatedPolicy(
policyRequestFrom(revalidationRequest),
Expand Down
7 changes: 6 additions & 1 deletion packages/apollo-datasource-rest/src/RESTDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
URL,
URLSearchParams,
URLSearchParamsInit,
fetch,
} from 'apollo-server-env';

import { DataSource, DataSourceConfig } from 'apollo-datasource';
Expand Down Expand Up @@ -48,9 +49,13 @@ export abstract class RESTDataSource<TContext = any> extends DataSource {
context!: TContext;
memoizedResults = new Map<string, Promise<any>>();

constructor(private httpFetch?: typeof fetch) {
super();
}

initialize(config: DataSourceConfig<TContext>): void {
this.context = config.context;
this.httpCache = new HTTPCache(config.cache);
this.httpCache = new HTTPCache(config.cache, this.httpFetch);
}

baseURL?: string;
Expand Down
14 changes: 14 additions & 0 deletions packages/apollo-datasource-rest/src/__tests__/HTTPCache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,4 +452,18 @@ describe('HTTPCache', () => {
expect(await response2.json()).toEqual({ name: 'Alan Turing' });
expect(response2.headers.get('Age')).toEqual('10');
});

it('fetches a response from the origin with a custom fetch function', async () => {
fetch.mockJSONResponseOnce({ name: 'Ada Lovelace' });

const customFetch = jest.fn(fetch);
const customHttpCache = new HTTPCache(store as any, customFetch);

const response = await customHttpCache.fetch(
new Request('https://api.example.com/people/1'),
);

expect(customFetch.mock.calls.length).toEqual(1);
expect(await response.json()).toEqual({ name: 'Ada Lovelace' });
});
});

0 comments on commit 07e2f4c

Please sign in to comment.