diff --git a/packages/hooks/src/__tests__/useLazyQuery.test.tsx b/packages/hooks/src/__tests__/useLazyQuery.test.tsx index 4b7062cef8..784f09734b 100644 --- a/packages/hooks/src/__tests__/useLazyQuery.test.tsx +++ b/packages/hooks/src/__tests__/useLazyQuery.test.tsx @@ -389,4 +389,43 @@ describe('useLazyQuery Hook', () => { }); } ); + + it('should support polling programatically', done => { + let renderCount = 0; + const Component = () => { + let [_, { data, loading, startPolling, stopPolling }] = useLazyQuery( + CAR_QUERY + ); + switch (renderCount) { + case 0: + expect(loading).toBeFalsy(); + startPolling(10); + break; + case 1: + expect(loading).toBeTruthy(); + break; + case 2: + expect(loading).toBeFalsy(); + expect(data).toEqual(CAR_RESULT_DATA); + stopPolling(); + setTimeout(() => { + done(); + }, 10); + break; + case 3: + done.fail('Uh oh - we should have stopped polling!'); + break; + default: + // Do nothing + } + renderCount += 1; + return null; + }; + + render( + + + + ); + }); }); diff --git a/packages/hooks/src/data/QueryData.ts b/packages/hooks/src/data/QueryData.ts index 1cb88001e2..6f0653bcdd 100644 --- a/packages/hooks/src/data/QueryData.ts +++ b/packages/hooks/src/data/QueryData.ts @@ -69,7 +69,14 @@ export class QueryData extends OperationData { loading: false, networkStatus: NetworkStatus.ready, called: false, - data: undefined + data: undefined, + startPolling: (pollInterval: number) => { + this.setOptions({ + ...super.getOptions(), + pollInterval + }); + this.runLazyQuery(); + } } as QueryResult ] : [this.runLazyQuery, this.execute()];