Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pollInterval support to client.executeQuery #397

Merged
merged 7 commits into from
Aug 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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