Skip to content

Commit

Permalink
temp fix of useLazyQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
brainkim committed Dec 8, 2021
1 parent f5582f8 commit bb6389d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
5 changes: 2 additions & 3 deletions src/react/hooks/__tests__/useLazyQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ describe('useLazyQuery Hook', () => {
expect(await executeResult).toEqual(result.current[1]);
});

it('should have matching results from execution function and hook', async () => {
it.only('should have matching results from execution function and hook', async () => {
const query = gql`
query GetCountries($filter: String) {
countries(filter: $filter) {
Expand Down Expand Up @@ -636,9 +636,8 @@ describe('useLazyQuery Hook', () => {
executeResult = execute({ variables: { filter: "BA" } });
});

// TODO: GET RID OF THIS RANDOM FUCKING RENDER.
await waitForNextUpdate();
expect(result.current[1].loading).toBe(true);

await waitForNextUpdate();
expect(result.current[1].loading).toBe(false);
expect(result.current[1].data).toEqual({
Expand Down
9 changes: 7 additions & 2 deletions src/react/hooks/useLazyQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ export function useLazyQuery<TData = any, TVariables = OperationVariables>(
);
setExecution((execution) => {
if (execution.called) {
result && result.refetch(executeOptions?.variables);
console.log(executeOptions?.variables);
resolve(result.refetch(executeOptions?.variables) as any);
return execution;
}

return {
Expand All @@ -66,10 +68,13 @@ export function useLazyQuery<TData = any, TVariables = OperationVariables>(
fetchPolicy: execution.called ? options?.fetchPolicy : 'standby',
skip: undefined,
});

useEffect(() => {
const { resolves } = execution;
if (!result.loading && resolves.length) {
setExecution((execution) => ({ ...execution, resolves: [] }));
setExecution((execution) => {
return { ...execution, resolves: [] };
});
resolves.forEach((resolve) => resolve(result));
}
}, [result, execution]);
Expand Down

0 comments on commit bb6389d

Please sign in to comment.