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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(cancel): add graphql-request example #2948

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions docs/src/pages/guides/query-cancellation.md
Expand Up @@ -97,6 +97,19 @@ const query = useQuery('todos', ({ signal }) => {
})
```

## Using `graphql-request`

An `AbortSignal` can be set in the `GraphQLClient` constructor.

```js
const query = useQuery('todos', ({ signal }) => {
const client = new GraphQLClient(endpoint, {
signal,
});
return client.request(query, variables)
})
```

## Manual Cancellation

You might want to cancel a query manually. For example, if the request takes a long time to finish, you can allow the user to click a cancel button to stop the request. To do this, you just need to call `queryClient.cancelQueries(key)`, which will cancel the query and revert it back to its previous state. If `promise.cancel` is available, or you have consumed the `signal` passed to the query function, React Query will additionally also cancel the Promise.
Expand Down