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

Make useQuery refetch async #3434

Open
wants to merge 2 commits into
base: main
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
4 changes: 3 additions & 1 deletion packages/react-urql/src/hooks/useQuery.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,9 @@ describe('useQuery', () => {
expect(client.executeQuery).toBeCalledTimes(1);

const [, executeQuery] = result.current;
act(() => executeQuery());
act(() => {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this returns a promise now, tsc complained

executeQuery();
});
await waitForNextUpdate();
expect(client.executeQuery).toBeCalledTimes(2);
});
Expand Down
45 changes: 26 additions & 19 deletions packages/react-urql/src/hooks/useQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ export interface UseQueryState<
* };
* ```
*/
export type UseQueryExecute = (opts?: Partial<OperationContext>) => void;
export type UseQueryExecute = (
opts?: Partial<OperationContext>
) => Promise<unknown>;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering whether this could turn into a breaking change, i.e. something complaining about not awaiting/... when doing onClick={() => executeQuery()}


/** Result tuple returned by the {@link useQuery} hook.
*
Expand Down Expand Up @@ -355,25 +357,30 @@ export function useQuery<
}, [cache, state[0], state[2][1]]);

const executeQuery = React.useCallback(
(opts?: Partial<OperationContext>) => {
const context = {
requestPolicy: args.requestPolicy,
...args.context,
...opts,
};
(opts?: Partial<OperationContext>) =>
new Promise<void>(resolve => {
const context = {
requestPolicy: args.requestPolicy,
...args.context,
...opts,
};

deferDispatch(setState, state => {
const source = suspense
? pipe(
client.executeQuery(request, context),
onPush(result => {
cache.set(request.key, result);
})
)
: client.executeQuery(request, context);
return [source, state[1], deps];
});
},
deferDispatch(setState, state => {
const source = suspense
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also did source.then(resolve) below everything (which works fine when running locally), but Typescript complains

? pipe(
client.executeQuery(request, context),
onPush(result => {
cache.set(request.key, result);
resolve();
})
)
: pipe(client.executeQuery(request, context), result => {
resolve();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in a cache-and-network call this would most likely return the stale equivalent. We do already have a streaming util to convert this to a Promise conditionally in [https://github.com/urql-graphql/urql/blob/main/packages/core/src/utils/streamUtils.ts#L15].

So what we could do is create the source, do a deferDispatch to set the state to React and return the source

return result;
});
return [source, state[1], deps];
});
}),
[
client,
cache,
Expand Down