ObservableQuery

API reference


ObservableQuery functions

ApolloClient Observables extend the Observables implementation provided by zen-observable. Refer to the zen-observable documentation for additional context and API options.

⚠️ Deprecated

result will be removed in Apollo Client 4.0.

Recommended now

If you continue to need this functionality, subscribe to ObservableQuery to get the first value emitted from the observable, then immediately unsubscribe.

When upgrading

Use RxJS's firstResultFrom function to mimic this functionality.

TypeScript
1const result = await firstValueFrom(from(observableQuery));

Signature

TypeScript
1result(): Promise<ApolloQueryResult<MaybeMasked<TData>>>

Result

Promise<ApolloQueryResult<MaybeMasked<TData>>>
Show/hide child attributes
Operation data
MaybeMasked<TData>

An object containing the result of your GraphQL query after it completes.

This value might be undefined if a query results in one or more errors (depending on the query's errorPolicy).

Other
ApolloError

The single Error object that is passed to onError and useQuery hooks, and is often thrown during manual client.query calls. This will contain both a NetworkError field and any GraphQLErrors. See https://www.apollographql.com/docs/react/data/error-handling/ for more information.

ReadonlyArray<GraphQLFormattedError>

A list of any errors that occurred during server-side execution of a GraphQL operation. See https://www.apollographql.com/docs/react/data/error-handling/ for more information.

boolean
NetworkStatus

Signature

TypeScript
1getCurrentResult(
2  saveAsLastResult?: boolean
3): ApolloQueryResult<MaybeMasked<TData>>

Parameters

Name / Type
Description

Result

ApolloQueryResult<MaybeMasked<TData>>
Show/hide child attributes
Operation data
MaybeMasked<TData>

An object containing the result of your GraphQL query after it completes.

This value might be undefined if a query results in one or more errors (depending on the query's errorPolicy).

Other
ApolloError

The single Error object that is passed to onError and useQuery hooks, and is often thrown during manual client.query calls. This will contain both a NetworkError field and any GraphQLErrors. See https://www.apollographql.com/docs/react/data/error-handling/ for more information.

ReadonlyArray<GraphQLFormattedError>

A list of any errors that occurred during server-side execution of a GraphQL operation. See https://www.apollographql.com/docs/react/data/error-handling/ for more information.

boolean
NetworkStatus

Update the variables of this observable query, and fetch the new results. This method should be preferred over setVariables in most use cases.

Signature

TypeScript
1refetch(
2  variables?: Partial<TVariables>
3): Promise<InteropApolloQueryResult<MaybeMasked<TData>>>

Parameters

Name / Type
Description
variables (optional)
Partial<TVariables>

The new set of variables. If there are missing variables, the previous values of those variables will be used.

Result

Promise<InteropApolloQueryResult<MaybeMasked<TData>>>
Show/hide child attributes
Operation data
MaybeMasked<TData>

An object containing the result of your GraphQL query after it completes.

This value might be undefined if a query results in one or more errors (depending on the query's errorPolicy).

Other
ApolloError

The single Error object that is passed to onError and useQuery hooks, and is often thrown during manual client.query calls. This will contain both a NetworkError field and any GraphQLErrors. See https://www.apollographql.com/docs/react/data/error-handling/ for more information.

ReadonlyArray<GraphQLFormattedError>

⚠️ Deprecated

errors will no longer available on the result in Apollo Client 4.0. This value is safe to use in Apollo Client 3.x.

Recommended now

No action needed

When upgrading

errors has been consolidated to the error property. You will need to read any errors on the error property on the resolved value instead.

boolean

⚠️ Deprecated

loading will no longer available on the result in Apollo Client 4.0. This value is always true when the resolved and can safely ignored.

NetworkStatus

⚠️ Deprecated

loading will no longer available on the result in Apollo Client 4.0. This value is always NetworkStatus.ready or NetworkStatus.error. To determine if the result contains an error, read from the error or errors property instead.

⚠️ Deprecated

partial will no longer available on the result in Apollo Client 4.0. This value is always false if there is a data value since the result never contains partial cache data.

⚠️ Deprecated

setOptions will be removed in Apollo Client 4.0. Please use observableQuery.reobserve(newOptions) instead.

Signature

TypeScript
1setOptions(
2  newOptions: Partial<WatchQueryOptions<TVariables, TData>>
3): Promise<InteropApolloQueryResult<MaybeMasked<TData>>>

Parameters

Name / Type
Description
newOptions
Partial<WatchQueryOptions<TVariables, TData>>

Result

Promise<InteropApolloQueryResult<MaybeMasked<TData>>>
Show/hide child attributes
Operation data
MaybeMasked<TData>

An object containing the result of your GraphQL query after it completes.

This value might be undefined if a query results in one or more errors (depending on the query's errorPolicy).

Other
ApolloError

The single Error object that is passed to onError and useQuery hooks, and is often thrown during manual client.query calls. This will contain both a NetworkError field and any GraphQLErrors. See https://www.apollographql.com/docs/react/data/error-handling/ for more information.

ReadonlyArray<GraphQLFormattedError>

⚠️ Deprecated

errors will no longer available on the result in Apollo Client 4.0. This value is safe to use in Apollo Client 3.x.

Recommended now

No action needed

When upgrading

errors has been consolidated to the error property. You will need to read any errors on the error property on the resolved value instead.

boolean

⚠️ Deprecated

loading will no longer available on the result in Apollo Client 4.0. This value is always true when the resolved and can safely ignored.

NetworkStatus

⚠️ Deprecated

loading will no longer available on the result in Apollo Client 4.0. This value is always NetworkStatus.ready or NetworkStatus.error. To determine if the result contains an error, read from the error or errors property instead.

⚠️ Deprecated

partial will no longer available on the result in Apollo Client 4.0. This value is always false if there is a data value since the result never contains partial cache data.

Update the variables of this observable query, and fetch the new results if they've changed. Most users should prefer refetch instead of setVariables in order to to be properly notified of results even when they come from the cache.

Note: the next callback will not fire if the variables have not changed or if the result is coming from cache.

Note: the promise will return the old results immediately if the variables have not changed.

Note: the promise will return null immediately if the query is not active (there are no subscribers).

Signature

TypeScript
1setVariables(
2  variables: TVariables
3): Promise<InteropApolloQueryResult<MaybeMasked<TData>> | void>

Parameters

Name / Type
Description
variables
TVariables

The new set of variables. If there are missing variables, the previous values of those variables will be used.

Result

Promise<InteropApolloQueryResult<MaybeMasked<TData>> | void>
Show/hide child attributes
Operation data
MaybeMasked<TData>

An object containing the result of your GraphQL query after it completes.

This value might be undefined if a query results in one or more errors (depending on the query's errorPolicy).

Other
ApolloError

The single Error object that is passed to onError and useQuery hooks, and is often thrown during manual client.query calls. This will contain both a NetworkError field and any GraphQLErrors. See https://www.apollographql.com/docs/react/data/error-handling/ for more information.

ReadonlyArray<GraphQLFormattedError>

⚠️ Deprecated

errors will no longer available on the result in Apollo Client 4.0. This value is safe to use in Apollo Client 3.x.

Recommended now

No action needed

When upgrading

errors has been consolidated to the error property. You will need to read any errors on the error property on the resolved value instead.

boolean

⚠️ Deprecated

loading will no longer available on the result in Apollo Client 4.0. This value is always true when the resolved and can safely ignored.

NetworkStatus

⚠️ Deprecated

loading will no longer available on the result in Apollo Client 4.0. This value is always NetworkStatus.ready or NetworkStatus.error. To determine if the result contains an error, read from the error or errors property instead.

⚠️ Deprecated

partial will no longer available on the result in Apollo Client 4.0. This value is always false if there is a data value since the result never contains partial cache data.

A function that helps you fetch the next set of results for a paginated list field.

Signature

TypeScript
1fetchMore<TFetchData, TFetchVars>(
2  fetchMoreOptions: FetchMoreQueryOptions<TFetchVars, TFetchData> & {
3        updateQuery?: (previousQueryResult: Unmasked<TData>, options: {
4            fetchMoreResult: Unmasked<TFetchData>;
5            variables: TFetchVars;
6        }) => Unmasked<TData>;
7    }
8): Promise<InteropApolloQueryResult<MaybeMasked<TFetchData>>>

Parameters

Name / Type
Description
fetchMoreOptions
FetchMoreQueryOptions<TFetchVars, TFetchData> & { updateQuery?: (previousQueryResult: Unmasked<TData>, options: { fetchMoreResult: Unmasked<TFetchData>; variables: TFetchVars; }) => Unmasked<TData>; }

Result

Promise<InteropApolloQueryResult<MaybeMasked<TFetchData>>>
Show/hide child attributes
Operation data
MaybeMasked<TFetchData>

An object containing the result of your GraphQL query after it completes.

This value might be undefined if a query results in one or more errors (depending on the query's errorPolicy).

Other
ApolloError

The single Error object that is passed to onError and useQuery hooks, and is often thrown during manual client.query calls. This will contain both a NetworkError field and any GraphQLErrors. See https://www.apollographql.com/docs/react/data/error-handling/ for more information.

ReadonlyArray<GraphQLFormattedError>

⚠️ Deprecated

errors will no longer available on the result in Apollo Client 4.0. This value is safe to use in Apollo Client 3.x.

Recommended now

No action needed

When upgrading

errors has been consolidated to the error property. You will need to read any errors on the error property on the resolved value instead.

boolean

⚠️ Deprecated

loading will no longer available on the result in Apollo Client 4.0. This value is always true when the resolved and can safely ignored.

NetworkStatus

⚠️ Deprecated

loading will no longer available on the result in Apollo Client 4.0. This value is always NetworkStatus.ready or NetworkStatus.error. To determine if the result contains an error, read from the error or errors property instead.

⚠️ Deprecated

partial will no longer available on the result in Apollo Client 4.0. This value is always false if there is a data value since the result never contains partial cache data.

A function that enables you to update the query's cached result without executing a followup GraphQL operation.

See using updateQuery and updateFragment for additional information.

Signature

TypeScript
1updateQuery(
2  mapFn: UpdateQueryMapFn<TData, TVariables>
3): void

Parameters

Name / Type
Description
mapFn
UpdateQueryMapFn<TData, TVariables>

A function that instructs the query to begin re-executing at a specified interval (in milliseconds).

Signature

TypeScript
1startPolling(
2  pollInterval: number
3): void

Parameters

Name / Type
Description

A function that instructs the query to stop polling after a previous call to startPolling.

Signature

TypeScript
1stopPolling(): void

A function that enables you to execute a subscription, usually to subscribe to specific fields that were included in the query.

This function returns another function that you can call to terminate the subscription.

Signature

TypeScript
1subscribeToMore<TSubscriptionData, TSubscriptionVariables>(
2  options: SubscribeToMoreOptions<TData, TSubscriptionVariables, TSubscriptionData, TVariables>
3): () => void

Parameters

Name / Type
Description
options
SubscribeToMoreOptions<TData, TSubscriptionVariables, TSubscriptionData, TVariables>

Result

() => void

Types

The current status of a query’s execution in our system.

Enumeration Members

No request is in flight for this query, but one or more errors were detected.

Indicates that fetchMore was called on this query and that the query created is currently in flight.

The query has never been run before and the query is now currently running. A query will still have this network status even if a partial data result was returned from the cache, but a query was dispatched anyway.

Indicates that a polling query is currently in flight. So for example if you are polling a query every 10 seconds then the network status will switch to poll every 10 seconds whenever a poll request has been sent but not resolved.

No request is in flight for this query, and no errors happened. Everything is OK.

Similar to the setVariables network status. It means that refetch was called on a query and the refetch request is currently in flight.

If setVariables was called and a query was fired because of that then the network status will be setVariables until the result of that query comes back.

Properties
Name / Type
Description
Operation data

An object containing the result of your GraphQL query after it completes.

This value might be undefined if a query results in one or more errors (depending on the query's errorPolicy).

Other
ApolloError

The single Error object that is passed to onError and useQuery hooks, and is often thrown during manual client.query calls. This will contain both a NetworkError field and any GraphQLErrors. See https://www.apollographql.com/docs/react/data/error-handling/ for more information.

ReadonlyArray<GraphQLFormattedError>

A list of any errors that occurred during server-side execution of a GraphQL operation. See https://www.apollographql.com/docs/react/data/error-handling/ for more information.

boolean
NetworkStatus
Feedback

Edit on GitHub

Ask Community