Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Allow startPolling in useLazyQuery without having to exec first #3645

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
39 changes: 39 additions & 0 deletions packages/hooks/src/__tests__/useLazyQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<MockedProvider mocks={CAR_MOCKS}>
<Component />
</MockedProvider>
);
});
});
9 changes: 8 additions & 1 deletion packages/hooks/src/data/QueryData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,14 @@ export class QueryData<TData, TVariables> 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<TData, TVariables>
]
: [this.runLazyQuery, this.execute()];
Expand Down