Skip to content

Commit

Permalink
Add pollInterval support to client.executeQuery (#397)
Browse files Browse the repository at this point in the history
* Add pollInterval argument to queries

* Use client.reexecuteOperation instead of executing the query again

* Add pollInterval to docs

* Move pollInterval logic to client.executeQuery

* Remove outdated tests

* Add crude test

* Explicitly test for teardown operation
  • Loading branch information
kitten authored and JoviDeCroock committed Aug 20, 2019
1 parent e94f044 commit 2f34c2e
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ interface UseQueryArgs {
variables?: any;
requestPolicy?: RequestPolicy;
pause?: boolean;
pollInterval?: number;
context?: Partial<OperationContext>;
}
```
Expand Down Expand Up @@ -109,6 +110,7 @@ interface UseSubscriptionState<T> {
| context | `?object` | The GraphQL request's context |
| requestPolicy | `?RequestPolicy` | An optional request policy that should be used |
| pause | `?boolean` | A boolean flag instructing `Query` to pause execution of the subsequent query operation |
| pollInterval | `?number` | Every `pollInterval` milliseconds the query will be refetched |
| children | `RenderProps => ReactNode` | A function that follows the typical render props pattern. The shape of the render props is as follows |

#### Render Props
Expand Down
15 changes: 15 additions & 0 deletions src/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,21 @@ describe('executeQuery', () => {

expect(receivedOps[0]).toHaveProperty('context.url', url);
});

it('polls when pollInterval is specified', () => {
jest.useFakeTimers();
pipe(
client.executeQuery(query, { pollInterval: 10000 }),
subscribe(x => x)
);

expect(receivedOps.length).toEqual(1);
jest.runOnlyPendingTimers();
expect(receivedOps.length).toEqual(3);
expect(receivedOps[0].operationName).toEqual('query');
expect(receivedOps[1].operationName).toEqual('teardown');
expect(receivedOps[2].operationName).toEqual('query');
});
});

describe('executeMutation', () => {
Expand Down
16 changes: 15 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import {
share,
Source,
take,
merge,
interval,
fromValue,
switchMap,
} from 'wonka';

import {
Expand Down Expand Up @@ -197,7 +201,17 @@ export class Client {
opts?: Partial<OperationContext>
): Source<OperationResult> => {
const operation = this.createRequestOperation('query', query, opts);
return this.executeRequestOperation(operation);
const response$ = this.executeRequestOperation(operation);
const { pollInterval } = operation.context;

if (pollInterval) {
return pipe(
merge([fromValue(0), interval(pollInterval)]),
switchMap(() => response$)
);
} else {
return response$;
}
};

executeSubscription = (
Expand Down
3 changes: 3 additions & 0 deletions src/hooks/useQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface UseQueryArgs<V> {
query: string | DocumentNode;
variables?: V;
requestPolicy?: RequestPolicy;
pollInterval?: number;
context?: Partial<OperationContext>;
pause?: boolean;
}
Expand Down Expand Up @@ -58,6 +59,7 @@ export const useQuery = <T = any, V = object>(
[unsubscribe.current] = pipe(
client.executeQuery(request, {
requestPolicy: args.requestPolicy,
pollInterval: args.pollInterval,
...args.context,
...opts,
...devtoolsContext,
Expand All @@ -70,6 +72,7 @@ export const useQuery = <T = any, V = object>(
[
args.context,
args.requestPolicy,
args.pollInterval,
client,
devtoolsContext,
request,
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export interface OperationContext {
fetchOptions?: RequestInit | (() => RequestInit);
requestPolicy: RequestPolicy;
url: string;
pollInterval?: number;
meta?: OperationDebugMeta;
}

Expand Down

0 comments on commit 2f34c2e

Please sign in to comment.