Repository: FormidableLabs/urql
Pull request #376 (2019-08-07 08:40:46)

(feat) - default request policy in client creation
Allow the consumers of the urql package to define a default requestPolicy this will be used in all operation creation and can be overriden on a per-query basis.

resolves: https://github.com/FormidableLabs/urql/issues/211


Patch:
diff --git a/src/client.ts b/src/client.ts
index e414628d..23e354c2 100755
--- a/src/client.ts
+++ b/src/client.ts
@@ -23,6 +23,7 @@ import {
   OperationContext,
   OperationResult,
   OperationType,
+  RequestPolicy,
 } from './types';
 
 import { toSuspenseSource } from './utils';
@@ -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 {
@@ -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;
@@ -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
@@ -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,


Test patch:
diff --git a/src/__snapshots__/client.test.ts.snap b/src/__snapshots__/client.test.ts.snap
index 4885bab4..ec12ddf3 100644
--- a/src/__snapshots__/client.test.ts.snap
+++ b/src/__snapshots__/client.test.ts.snap
@@ -14,6 +14,7 @@ Client {
   "fetchOptions": undefined,
   "operations$": [Function],
   "reexecuteOperation": [Function],
+  "requestPolicy": "cache-first",
   "results$": [Function],
   "suspense": false,
   "url": "https://hostname.com",
diff --git a/src/__snapshots__/context.test.ts.snap b/src/__snapshots__/context.test.ts.snap
index 3a40f8a5..f223c558 100644
--- a/src/__snapshots__/context.test.ts.snap
+++ b/src/__snapshots__/context.test.ts.snap
@@ -27,6 +27,7 @@ Object {
       "fetchOptions": undefined,
       "operations$": [Function],
       "reexecuteOperation": [Function],
+      "requestPolicy": "cache-first",
       "results$": [Function],
       "suspense": false,
       "url": "/graphql",
@@ -44,6 +45,7 @@ Object {
       "fetchOptions": undefined,
       "operations$": [Function],
       "reexecuteOperation": [Function],
+      "requestPolicy": "cache-first",
       "results$": [Function],
       "suspense": false,
       "url": "/graphql",
@@ -80,6 +82,7 @@ Object {
       "fetchOptions": undefined,
       "operations$": [Function],
       "reexecuteOperation": [Function],
+      "requestPolicy": "cache-first",
       "results$": [Function],
       "suspense": false,
       "url": "/graphql",
@@ -97,6 +100,7 @@ Object {
       "fetchOptions": undefined,
       "operations$": [Function],
       "reexecuteOperation": [Function],
+      "requestPolicy": "cache-first",
       "results$": [Function],
       "suspense": false,
       "url": "/graphql",
diff --git a/src/client.test.ts b/src/client.test.ts
index 8d656d32..485449be 100755
--- a/src/client.test.ts
+++ b/src/client.test.ts
@@ -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', () => {
@@ -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),