Docs
Launch GraphOS 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): ApolloClient<TCacheShape>

(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
boolean
context
DefaultContext
errorPolicy
ErrorPolicy
fetchPolicy
WatchQueryFetchPolicy
initialFetchPolicy
WatchQueryFetchPolicy
nextFetchPolicy
any
notifyOnNetworkStatusChange
boolean
partialRefetch
boolean
pollInterval
number
query
DocumentNode | TypedDocumentNode
refetchWritePolicy
RefetchWritePolicy
returnPartialData
boolean
variables
TVariables
Options
Name /
Type
Description
canonizeResults
boolean
context
DefaultContext
errorPolicy
ErrorPolicy
fetchPolicy
FetchPolicy
notifyOnNetworkStatusChange
boolean
partialRefetch
boolean
pollInterval
number
query
DocumentNode | TypedDocumentNode
returnPartialData
boolean
variables
TVariables
Options
Name /
Type
Description
awaitRefetchQueries
boolean
context
TContext
errorPolicy
ErrorPolicy
fetchPolicy
MutationFetchPolicy
keepRootFields
boolean
mutation
DocumentNode | TypedDocumentNode
onQueryUpdated
OnQueryUpdated<any>
optimisticResponse
any
refetchQueries
any
update
MutationUpdaterFunction<TData, TVariables, TContext, TCache>
updateQueries
MutationQueryReducersMap<TData>
variables
TVariables
Options
Name /
Type
Description
context
DefaultContext
errorPolicy
ErrorPolicy
fetchPolicy
FetchPolicy
query
DocumentNode | TypedDocumentNode
variables
TVariables
Arguments
Name /
Type
Description
optimistic
boolean
Arguments
Name /
Type
Description
optimistic
boolean
Arguments
Name /
Type
Description
cb
Promise<any>
Arguments
Name /
Type
Description
cb
Promise<any>
Arguments
Name /
Type
Description
includeStandby
boolean
Options
Name /
Type
Description
include
RefetchQueriesInclude
onQueryUpdated
any
optimistic
boolean
updateCache
void
Arguments
Name /
Type
Description
include
RefetchQueriesInclude

Types

Previous
Hooks migration guide
Next
InMemoryCache
Edit on GitHubEditForumsDiscord