Skip to content

Commit

Permalink
Allow observableQuery methods to be called by useLazyQuery
Browse files Browse the repository at this point in the history
Fixes #5140
  • Loading branch information
brainkim committed Aug 16, 2021
1 parent 72ac3bf commit 2245d99
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 23 deletions.
54 changes: 54 additions & 0 deletions src/react/hooks/__tests__/useLazyQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -327,4 +327,58 @@ describe('useLazyQuery Hook', () => {
expect(result.current[1].data).toEqual({ hello: 'world 2' });
expect(result.current[1].previousData).toEqual({ hello: 'world 1' });
});

it('should allow for the query to start with polling', async () => {
const query = gql`{ hello }`;
const mocks = [
{
request: { query },
result: { data: { hello: "world 1" } },
},
{
request: { query },
result: { data: { hello: "world 2" } },
},
{
request: { query },
result: { data: { hello: "world 3" } },
},
];

const wrapper = ({ children }: any) => (
<MockedProvider mocks={mocks}>{children}</MockedProvider>
);

const { result, waitForNextUpdate } = renderHook(
() => useLazyQuery(query),
{ wrapper },
);

expect(result.current[1].loading).toBe(false);
expect(result.current[1].data).toBe(undefined);

setTimeout(() => {
result.current[1].startPolling(10);
});

await waitForNextUpdate();
expect(result.current[1].loading).toBe(true);
expect(result.current[1].data).toBe(undefined);

await waitForNextUpdate();
expect(result.current[1].loading).toBe(false);
expect(result.current[1].data).toEqual({ hello: "world 1" });

await waitForNextUpdate();
expect(result.current[1].loading).toBe(false);
expect(result.current[1].data).toEqual({ hello: "world 2" });

await waitForNextUpdate();

expect(result.current[1].loading).toBe(false);
expect(result.current[1].data).toEqual({ hello: "world 3" });

result.current[1].stopPolling();
await expect(waitForNextUpdate({ timeout: 20 })).rejects.toThrow('Timed out');
});
});
10 changes: 10 additions & 0 deletions src/react/hooks/useLazyQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ export function useLazyQuery<TData = any, TVariables = OperationVariables>(
});

if (!execution.called) {
for (const key in result) {
if (typeof (result as any)[key] === 'function') {
const method = (result as any)[key];
(result as any)[key] = (...args: any) => {
setExecution({ called: true });
return method(...args);
};
}
}

result = {
...result,
loading: false,
Expand Down
25 changes: 2 additions & 23 deletions src/react/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,29 +104,8 @@ export interface QueryLazyOptions<TVariables> {
context?: DefaultContext;
}

type UnexecutedLazyFields = {
loading: false;
networkStatus: NetworkStatus.ready;
called: false;
data: undefined;
previousData?: undefined;
}

type Impartial<T> = {
[P in keyof T]?: never;
}

type AbsentLazyResultFields =
Omit<
Impartial<QueryResult<unknown, unknown>>,
keyof UnexecutedLazyFields>

type UnexecutedLazyResult =
UnexecutedLazyFields & AbsentLazyResultFields

export type LazyQueryResult<TData, TVariables> =
| UnexecutedLazyResult
| QueryResult<TData, TVariables>;
// TODO: Delete this
export type LazyQueryResult<TData, TVariables> = QueryResult<TData, TVariables>;

export type QueryTuple<TData, TVariables> = [
(options?: QueryLazyOptions<TVariables>) => void,
Expand Down

0 comments on commit 2245d99

Please sign in to comment.