class ApolloClient
API reference
The ApolloClient
class encapsulates Apollo's core client-side API. It backs all available view-layer integrations (React, iOS, and so on).
The ApolloClient
constructor
Constructs an instance of ApolloClient.
Takes an ApolloClientOptions
parameter that supports the fields listed below.
Returns an initialized ApolloClient
object.
Example
import { ApolloClient, InMemoryCache } from '@apollo/client';
const cache = new InMemoryCache();
const client = new ApolloClient({
// Provide required constructor fields
cache: cache,
uri: 'http://localhost:4000/',
// Provide some optional constructor fields
name: 'react-web-client',
version: '1.3',
queryDeduplication: false,
defaultOptions: {
watchQuery: {
fetchPolicy: 'cache-and-network',
},
},
});
Options
Name / Type | Description |
---|---|
| The URI of the GraphQL endpoint that Apollo Client will communicate with. One of |
| You can provide an Apollo Link instance to serve as Apollo Client's network layer. For more information, see Advanced HTTP networking. One of |
| Required. The cache that Apollo Client should use to store query results locally. The recommended cache is For more information, see Configuring the cache. |
| A custom name (e.g., |
| A custom version that identifies the current version of this particular client (e.g., This is not the version of Apollo Client that you are using, but rather any version string that helps you differentiate between versions of your client. |
| When using Apollo Client for server-side rendering, set this to The default value is |
| The time interval (in milliseconds) before Apollo Client force-fetches queries after a server-side render. The default value is |
| If The default value is |
| If The default value is |
| Provide this object to set application-wide default values for options you can provide to the See the example object below. |
Example defaultOptions
object
const defaultOptions = {
watchQuery: {
fetchPolicy: 'cache-and-network',
errorPolicy: 'ignore',
},
query: {
fetchPolicy: 'network-only',
errorPolicy: 'all',
},
mutate: {
errorPolicy: 'all',
},
};
You can override any default option you specify in this object by providing a different value for the same option in individual function calls.
Note: The
useQuery
hook uses Apollo Client'swatchQuery
function. To setdefaultOptions
when using theuseQuery
hook, make sure to set them under thedefaultOptions.watchQuery
property.
Functions
This watches the cache store of the query according to the options specified and returns an ObservableQuery. We can subscribe to this ObservableQuery and receive updated results through a GraphQL observer when the cache store changes.
Options
Name / Type | Description |
---|---|
| Whether to canonize cache results before returning them. Canonization takes some extra time, but it speeds up future deep equality comparisons. Defaults to false. |
| Context to be passed to link execution chain |
| Specifies the ErrorPolicy to be used for this query |
| Specifies the FetchPolicy to be used for this query. |
| Specifies the FetchPolicy to be used after this query has completed. |
| Whether or not updates to the network status should trigger next on the observer of this query |
| If |
| The time interval (in milliseconds) on which this query should be refetched from the server. |
| A GraphQL document that consists of a single query to be sent down to the server. |
| Specifies whether a {@link NetworkStatus.refetch} operation should merge incoming field data with existing data, or overwrite the existing data. Overwriting is probably preferable, but merging is currently the default behavior, for backwards compatibility with Apollo Client 3.x. |
| Allow returning incomplete data from the cache when a larger query cannot be fully satisfied by the cache, instead of returning nothing. |
| A map going from variable name to variable value, where the variables are used within the GraphQL query. |
This resolves a single query according to the options specified and returns a Promise which is either resolved with the resulting data or rejected with an error.
Options
Name / Type | Description |
---|---|
| Whether to canonize cache results before returning them. Canonization takes some extra time, but it speeds up future deep equality comparisons. Defaults to false. |
| Context to be passed to link execution chain |
| Specifies the ErrorPolicy to be used for this query |
| Specifies the FetchPolicy to be used for this query |
| Whether or not updates to the network status should trigger next on the observer of this query |
| If |
| The time interval (in milliseconds) on which this query should be refetched from the server. |
| A GraphQL document that consists of a single query to be sent down to the server. |
| Allow returning incomplete data from the cache when a larger query cannot be fully satisfied by the cache, instead of returning nothing. |
| A map going from variable name to variable value, where the variables are used within the GraphQL query. |
This resolves a single mutation according to the options specified and returns a Promise which is either resolved with the resulting data or rejected with an error.
It takes options as an object with the following keys and values:
Options
Name / Type | Description |
---|---|
| By default, |
| The context to be passed to the link execution chain. This context will
only be used with this mutation. It will not be used with
|
| Specifies the ErrorPolicy to be used for this operation |
| Specifies the MutationFetchPolicy to be used for this query. Mutations support only 'network-only' and 'no-cache' fetchPolicy strings. If fetchPolicy is not provided, it defaults to 'network-only'. |
| To avoid retaining sensitive information from mutation root field
arguments, Apollo Client v3.4+ automatically clears any |
| A GraphQL document, often created with |
| A function that will be called for each ObservableQuery affected by this mutation, after the mutation has completed. |
| An object that represents the result of this mutation that will be optimistically stored before the server has actually returned a result. This is most often used for optimistic UI, where we want to be able to see the result of a mutation immediately, and update the UI later if any errors appear. |
| A list of query names which will be refetched once this mutation has
returned. This is often used if you have a set of queries which may be
affected by a mutation and will have to update. Rather than writing a
mutation query reducer (i.e. |
| This function will be called twice over the lifecycle of a mutation. Once
at the very beginning if an Note that since this function is intended to be used to update the
store, it cannot be used with a |
| A MutationQueryReducersMap, which is map from query names to mutation query reducers. Briefly, this map defines how to incorporate the results of the mutation into the results of queries that are currently being watched by your application. |
| An object that maps from the name of a variable as used in the mutation GraphQL document to that variable's value. |
This subscribes to a graphql subscription according to the options specified and returns an Observable which either emits received data or an error.
Options
Name / Type | Description |
---|---|
| Context object to be passed through the link execution chain. |
| Specifies the ErrorPolicy to be used for this operation |
| Specifies the FetchPolicy to be used for this subscription. |
| A GraphQL document, often created with |
| An object that maps from the name of a variable as used in the subscription GraphQL document to that variable's value. |
Tries to read some data from the store in the shape of the provided
GraphQL query without making a network request. This method will start at
the root query. To start at a specific id returned by dataIdFromObject
use readFragment
.
Arguments
Name / Type | Description |
---|---|
| Set to |
Tries to read some data from the store in the shape of the provided
GraphQL fragment without making a network request. This method will read a
GraphQL fragment from any arbitrary id that is currently cached, unlike
readQuery
which will only read from the root query.
You must pass in a GraphQL document with a single fragment or a document
with multiple fragments that represent what you are reading. If you pass
in a document with multiple fragments then you must also specify a
fragmentName
.
Arguments
Name / Type | Description |
---|---|
| Set to |
Options
Name / Type | Description |
---|---|
| A GraphQL document created using the |
| The name of the fragment in your GraphQL document to be used. If you do
not provide a |
| The root id to be used. This id should take the same form as the
value returned by your |
| Any variables that your GraphQL fragments depend on. |
Writes some data in the shape of the provided GraphQL query directly to
the store. This method will start at the root query. To start at a
specific id returned by dataIdFromObject
then use writeFragment
.
Options
Name / Type | Description |
---|---|
| Whether to notify query watchers (default: true). |
| The data you will be writing to the store. |
| The root id to be used. Defaults to "ROOT_QUERY", which is the ID of the root query object. This property makes writeQuery capable of writing data to any object in the cache. |
| When true, ignore existing field data rather than merging it with incoming data (default: false). |
| The GraphQL query shape to be used constructed using the |
| Any variables that the GraphQL query may depend on. |
Writes some data in the shape of the provided GraphQL fragment directly to
the store. This method will write to a GraphQL fragment from any arbitrary
id that is currently cached, unlike writeQuery
which will only write
from the root query.
You must pass in a GraphQL document with a single fragment or a document
with multiple fragments that represent what you are writing. If you pass
in a document with multiple fragments then you must also specify a
fragmentName
.
Options
Name / Type | Description |
---|---|
| Whether to notify query watchers (default: true). |
| The data you will be writing to the store. |
| A GraphQL document created using the |
| The name of the fragment in your GraphQL document to be used. If you do
not provide a |
| The root id to be used. This id should take the same form as the
value returned by your |
| When true, ignore existing field data rather than merging it with incoming data (default: false). |
| Any variables that your GraphQL fragments depend on. |
Resets your entire store by clearing out your cache and then re-executing all of your active queries. This makes it so that you may guarantee that there is no data left in your store from a time before you called this method.
resetStore()
is useful when your user just logged out. You’ve removed the
user session, and you now want to make sure that any references to data you
might have fetched while the user session was active is gone.
It is important to remember that resetStore()
will refetch any active
queries. This means that any components that might be mounted will execute
their queries again using your network interface. If you do not want to
re-execute any queries then you should make sure to stop watching any
active queries.
Allows callbacks to be registered that are executed when the store is
reset. onResetStore
returns an unsubscribe function that can be used
to remove registered callbacks.
Arguments
Name / Type | Description |
---|---|
|
Remove all data from the store. Unlike resetStore
, clearStore
will
not refetch any active queries.
Allows callbacks to be registered that are executed when the store is
cleared. onClearStore
returns an unsubscribe function that can be used
to remove registered callbacks.
Arguments
Name / Type | Description |
---|---|
|
Call this method to terminate any active client processes, making it safe
to dispose of this ApolloClient
instance.
Refetches all of your active queries.
reFetchObservableQueries()
is useful if you want to bring the client back to proper state in case of a network outage
It is important to remember that reFetchObservableQueries()
will refetch any active
queries. This means that any components that might be mounted will execute
their queries again using your network interface. If you do not want to
re-execute any queries then you should make sure to stop watching any
active queries.
Takes optional parameter includeStandby
which will include queries in standby-mode when refetching.
Arguments
Name / Type | Description |
---|---|
|
Refetches specified active queries. Similar to "reFetchObservableQueries()" but with a specific list of queries.
refetchQueries()
is useful for use cases to imperatively refresh a selection of queries.
It is important to remember that refetchQueries()
will refetch specified active
queries. This means that any components that might be mounted will execute
their queries again using your network interface. If you do not want to
re-execute any queries then you should make sure to stop watching any
active queries.
Options
Name / Type | Description |
---|---|
| |
| |
| |
|
Types
Properties
Name / Type | Description |
---|---|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
Properties
Name / Type | Description |
---|---|
| |
| |
|