Skip to content

Commit

Permalink
(feat) - default request policy in client creation (#376)
Browse files Browse the repository at this point in the history
* (feat) - allow passing of a default requestPolicy and use this as a default for createOperationContext

* (tests) - test if the defaultRequestPolicy is applied correctly and if override of it works

* (chore) - update snapshots
  • Loading branch information
JoviDeCroock authored and kitten committed Aug 7, 2019
1 parent 07b7fea commit 2cc81e9
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/__snapshots__/client.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Client {
"fetchOptions": undefined,
"operations$": [Function],
"reexecuteOperation": [Function],
"requestPolicy": "cache-first",
"results$": [Function],
"suspense": false,
"url": "https://hostname.com",
Expand Down
4 changes: 4 additions & 0 deletions src/__snapshots__/context.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Object {
"fetchOptions": undefined,
"operations$": [Function],
"reexecuteOperation": [Function],
"requestPolicy": "cache-first",
"results$": [Function],
"suspense": false,
"url": "/graphql",
Expand All @@ -44,6 +45,7 @@ Object {
"fetchOptions": undefined,
"operations$": [Function],
"reexecuteOperation": [Function],
"requestPolicy": "cache-first",
"results$": [Function],
"suspense": false,
"url": "/graphql",
Expand Down Expand Up @@ -80,6 +82,7 @@ Object {
"fetchOptions": undefined,
"operations$": [Function],
"reexecuteOperation": [Function],
"requestPolicy": "cache-first",
"results$": [Function],
"suspense": false,
"url": "/graphql",
Expand All @@ -97,6 +100,7 @@ Object {
"fetchOptions": undefined,
"operations$": [Function],
"reexecuteOperation": [Function],
"requestPolicy": "cache-first",
"results$": [Function],
"suspense": false,
"url": "/graphql",
Expand Down
30 changes: 29 additions & 1 deletion src/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ beforeEach(() => {
receivedOps = [];
exchangeMock.mockClear();
receiveMock.mockClear();
client = createClient({ url, exchanges: [exchangeMock] as any[] });
client = createClient({
url,
exchanges: [exchangeMock] as any[],
requestPolicy: 'cache-and-network',
});
});

describe('exchange args', () => {
Expand Down Expand Up @@ -85,6 +89,30 @@ describe('executeQuery', () => {
expect(receivedOps[0]).toHaveProperty('variables', query.variables);
});

it('passes requestPolicy to exchange', () => {
pipe(
client.executeQuery(query),
subscribe(x => x)
);

expect(receivedOps[0].context).toHaveProperty(
'requestPolicy',
'cache-and-network'
);
});

it('allows overriding the requestPolicy', () => {
pipe(
client.executeQuery(query, { requestPolicy: 'cache-first' }),
subscribe(x => x)
);

expect(receivedOps[0].context).toHaveProperty(
'requestPolicy',
'cache-first'
);
});

it('passes operationName type to exchange', () => {
pipe(
client.executeQuery(query),
Expand Down
7 changes: 6 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
OperationContext,
OperationResult,
OperationType,
RequestPolicy,
} from './types';

import { toSuspenseSource } from './utils';
Expand All @@ -39,6 +40,8 @@ export interface ClientOptions {
exchanges?: Exchange[];
/** Activates support for Suspense. */
suspense?: boolean;
/** The default request policy for requests. */
requestPolicy?: RequestPolicy;
}

interface ActiveOperations {
Expand All @@ -55,6 +58,7 @@ export class Client {
fetchOptions?: RequestInit | (() => RequestInit);
exchange: Exchange;
suspense: boolean;
requestPolicy: RequestPolicy;

// These are internals to be used to keep track of operations
dispatchOperation: (operation: Operation) => void;
Expand All @@ -67,6 +71,7 @@ export class Client {
this.fetchOptions = opts.fetchOptions;
this.fetch = opts.fetch;
this.suspense = !!opts.suspense;
this.requestPolicy = opts.requestPolicy || 'cache-first';

// This subject forms the input of operations; executeOperation may be
// called to dispatch a new operation on the subject
Expand Down Expand Up @@ -111,7 +116,7 @@ export class Client {
private createOperationContext = (
opts?: Partial<OperationContext>
): OperationContext => {
const { requestPolicy = 'cache-first' } = opts || {};
const { requestPolicy = this.requestPolicy } = opts || {};

return {
url: this.url,
Expand Down

0 comments on commit 2cc81e9

Please sign in to comment.