| title | ObservableQuery |
|---|---|
| description | API reference |
{/* @import {MDXProvidedComponents} from '../../../shared/MdxProvidedComponents.js' */}
An ObservableQuery is created by calling the client.watchQuery method.
It represents a query that can be observed for changes, allowing you to reactively update your UI.
ObservableQuery implements the RxJS InteropObservable interface which means you can convert it into an RxJS Observable via from(observableQuery).
It also provides the subscribe and pipe functions like an RxJS Observable.
Refer to the RxJS documentation for additional context and API options.
To get a single result from an ObservableQuery as a Promise, use the firstValueFrom helper:
import { firstValueFrom, from } from "rxjs";
// The `from` is necessary to turn `observableQuery` into an RxJS observable
const result = await firstValueFrom(from(observableQuery));ObservableQueryinstances are only registered withApolloClientwhile they have active subscribers- Unsubscribing from an
ObservableQuerywhile a request is in flight does not terminate the request - Unsubscribing before any value has been emitted removes the query from the tracked list and makes it ineligible for query deduplication
ObservableQuerydoes not terminate on errors - instead it emits anextvalue with anerrorproperty. This ensuresObservableQuerysubscriptions can continue receiving updates after errors without resubscription.
- When
notifyOnNetworkStatusChangeistrue(the default), an initial loading state is emitted when subscribing ObservableQuerypreservesdatawhen emitting a loading state unlessqueryorvariableschanged (note:@exportvariables are not considered for this check)- When the query can be fulfilled by the cache or when the link chain responds synchronously, a loading state is omitted
cache-onlyqueries initialize withnetworkStatus: NetworkStatus.readywhen there is no data in the cachestandbyqueries initialize withnetworkStatus: NetworkStatus.readybefore subscribing to the query
refetch()andreobserve()return aResultPromisewith an additional.retain()method- By default, the network operation is cancelled when
ObservableQueryno longer requires the result, such as whenObservableQueryis unsubscribed or variables change, and the returnedPromisewill reject with anAbortError - Calling
.retain()keeps the network operation running even when theObservableQueryno longer requires the result setVariables()andrefetch()guarantee that a value will be emitted from the observable, even when the result is deeply equal to the previous result
- Active queries: Have at least one subscriber and are not skipped or have a
fetchPolicyofstandby - Inactive queries: Have a subscriber but are either skipped or have a
fetchPolicyofstandby ObservableQuerys without subscribers but with an active network request are handled as if they had a subscriber for the duration of the query- Only queries with subscribers can be refetched using
ApolloClient.refetchQueries