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

Conversation

davidfant
Copy link

Summary

In my React app I want to be able to await the refetch function, for example to call another function after refetch is done

Set of changes

Mainly the React package's useQuery.ts. Let me know if there's a better way to promisify this using wonka

Copy link

changeset-bot bot commented Nov 16, 2023

⚠️ No Changeset found

Latest commit: c6e57e4

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@@ -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

});
},
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), 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

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()}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants