Docs
Try Apollo Studio

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

constructor(options)

(build/repo/src/core/ApolloClient.ts, line 127)

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
uri

String

The URI of the GraphQL endpoint that Apollo Client will communicate with.

One of uri or link is required. If you provide both, link takes precedence.

ApolloLink

You can provide an Apollo Link instance to serve as Apollo Client's network layer. For more information, see Advanced HTTP networking.

One of uri or link is required. If you provide both, link takes precedence.

cache

ApolloCache (usually InMemoryCache)

Required. The cache that Apollo Client should use to store query results locally. The recommended cache is InMemoryCache, which is provided by the @apollo/client package.

For more information, see Configuring the cache.

name

String

A custom name (e.g., iOS) that identifies this particular client among your set of clients. Apollo Server and Apollo Studio use this property as part of the client awareness feature.

version

String

A custom version that identifies the current version of this particular client (e.g., 1.2). Apollo Server and Apollo Studio use this property as part of the client awareness feature.

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.

ssrMode

Boolean

When using Apollo Client for server-side rendering, set this to true so that the getDataFromTree function can work effectively.

The default value is false.

ssrForceFetchDelay

Number

The time interval (in milliseconds) before Apollo Client force-fetches queries after a server-side render.

The default value is 0 (no delay).

connectToDevTools

Boolean

If true, the Apollo Client Devtools browser extension can connect to Apollo Client in your production environment. The extension can always connect in a non-production environment.

The default value is false.

queryDeduplication

Boolean

If false, Apollo Client sends every created query to the server, even if a completely identical query (identical in terms of query string, variable values, and operationName) is already in flight.

The default value is true.

defaultOptions

Object

Provide this object to set application-wide default values for options you can provide to the watchQuery, query, and mutate functions. See below for an example object.

See the example object below.

assumeImmutableResults

Boolean

If true, Apollo Client will assume results read from the cache are never mutated by application code, which enables substantial performance optimizations.

The default value is false.

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's watchQuery function. To set defaultOptions when using the useQuery hook, make sure to set them under the defaultOptions.watchQuery property.

Functions

Options
Name /
Type
Description
canonizeResults
any
context
any
errorPolicy
any
fetchPolicy
any
initialFetchPolicy
any
nextFetchPolicy
any
notifyOnNetworkStatusChange
any
partialRefetch
any
pollInterval
any
query
DocumentNode | TypedDocumentNode
refetchWritePolicy
RefetchWritePolicy
returnPartialData
any
variables
TVariables
Options
Name /
Type
Description
canonizeResults
any
context
any
errorPolicy
any
fetchPolicy
any
notifyOnNetworkStatusChange
any
partialRefetch
any
pollInterval
any
query
DocumentNode | TypedDocumentNode
returnPartialData
any
variables
TVariables
Options
Name /
Type
Description
awaitRefetchQueries
any
context
TContext
errorPolicy
any
fetchPolicy
MutationFetchPolicy
keepRootFields
any
mutation
DocumentNode | TypedDocumentNode
onQueryUpdated
(ObservableQuery<>, Cache.DiffResult<>, any) => any<>
optimisticResponse
any
refetchQueries
any
update
(TCache, Omit<FetchResult, >, any) => any<TData, TVariables, TContext, TCache>
updateQueries
[queryName:undefined]:(Record<, >, any) => Record<, ><T><TData>
variables
TVariables
Options
Name /
Type
Description
context
Record<, >
errorPolicy
any
fetchPolicy
any
query
DocumentNode | TypedDocumentNode
variables
TVariables
Arguments
Name /
Type
Description
optimistic
any
Arguments
Name /
Type
Description
optimistic
any
Options
Name /
Type
Description
fragment
DocumentNode | TypedDocumentNode
fragmentName
any
id
any
variables
TVariables
Options
Name /
Type
Description
broadcast
any
data
TData
id
any
overwrite
any
query
DocumentNode | TypedDocumentNode
variables
TVariables
Options
Name /
Type
Description
broadcast
any
data
TData
fragment
DocumentNode | TypedDocumentNode
fragmentName
any
id
any
overwrite
any
variables
TVariables
Arguments
Name /
Type
Description
cb
() => Promise<>
Arguments
Name /
Type
Description
cb
() => Promise<>
Arguments
Name /
Type
Description
includeStandby
any
Options
Name /
Type
Description
include
any
onQueryUpdated
any
optimistic
any
updateCache
(TCache) => any

Types

Properties
Name /
Type
Description
assumeImmutableResults
any
cache
ApolloCache<TCacheShape>
connectToDevTools
any
credentials
any
defaultOptions
DefaultOptions
fragmentMatcher
(any, any, any) => any
headers
Record<, >
link
ApolloLink
name
any
queryDeduplication
any
resolvers
any
ssrForceFetchDelay
any
ssrMode
any
typeDefs
any
uri
any
version
any
Properties
Name /
Type
Description
mutate
Partial<MutationOptions>
query
Partial<QueryOptions>
watchQuery
Partial<WatchQueryOptions>
Edit on GitHub
Previous
Hooks migration guide
Next
InMemoryCache