How to use fetchQuery without Subscription?
- Dominant language
- Rust
- Stars
- 19k
- Forks
- 1.9k
- PR merge metrics
- No merged PRs in 30d
Description
[Docs](https://relay.dev/docs/api-reference/fetch-query/) says that we need to use fetchQuery with a subscription, and it is not recommended to use it with toPromise().
It's unclear then, how I can fetch value without a subscription in an event handler. I don't need to render this data, so usePreloadedQuery is not an option.
How I understand it should work:
```typescript
const QUERY_AUTOLOGOUT_TIMELEFT = graphql`
query AutoLogoutTimeleftQuery {
autologoutTimeleft
}
onClick = () => {
fetchQuery(
environment,
QUERY_AUTOLOGOUT_TIMELEFT,
{},
{
fetchPolicy: 'network-only',
}
).toPromise().then((data) => console.log(data?.autologoutTimeleft));
}
```
But 1) the doc says don't use it this way, and also, 2) data can be undefined here
It works well if I use the subscribe, but it's unclear, whether I need to close the subscription after the first "next" call.
```typescript
fetchQuery(
environment,
QUERY_AUTOLOGOUT_TIMELEFT,
{},
{
fetchPolicy: 'network-only',
}
).subscribe({
next(data) {
console.log(data.autologoutTimeleft);
},
});
```
Contributor guide
Assessment
This issue has not been assessed yet.