interface IApolloClient<T> {
    cache: ApolloCache<T>;
    defaultOptions: DefaultOptions;
    disableNetworkFetches: boolean;
    getMemoryInternals?: (() => {
        limits: {
            [k: string]: number;
        };
        sizes: {
            addTypenameDocumentTransform?: {
                cache: number;
            }[];
            cache?: {
                fragmentQueryDocuments: number;
            };
            canonicalStringify: number;
            fragmentRegistry?: {
                findFragmentSpreads: number;
                lookup: number;
                transform: number;
            };
            inMemoryCache?: {
                executeSelectionSet: number;
                executeSubSelectedArray: number;
                maybeBroadcastWatch: number;
            };
            links: unknown[];
            parser: number;
            print: number;
            queryManager: {
                documentTransforms: {
                    cache: number;
                }[];
                getDocumentInfo: number;
            };
        };
    });
    jwt_token?: string;
    link: ApolloLink;
    path?: string;
    queryDeduplication: boolean;
    ssl?: boolean;
    typeDefs:
        | string
        | string[]
        | DocumentNode
        | DocumentNode[];
    version: string;
    get defaultContext(): Partial<DefaultContext>;
    get documentTransform(): DocumentTransform;
    __actionHookForDevTools(cb: (() => any)): void;
    __requestRaw(payload: GraphQLRequest<Record<string, any>>): Observable<ExecutionResult<{
        [key: string]: any;
    }, {
        [key: string]: any;
    }>>;
    addResolvers(resolvers: Resolvers | Resolvers[]): void;
    clearStore(): Promise<any[]>;
    extract(optimistic?: boolean): T;
    getObservableQueries(include?: RefetchQueriesInclude): Map<string, ObservableQuery<any, OperationVariables>>;
    getResolvers(): Resolvers;
    mutate<TData, TVariables, TContext, TCache>(options: MutationOptions<TData, TVariables, TContext, ApolloCache<any>>): Promise<FetchResult<TData>>;
    onClearStore(cb: (() => Promise<any>)): (() => void);
    onResetStore(cb: (() => Promise<any>)): (() => void);
    query<T, TVariables>(options: QueryOptions<TVariables, T>): Promise<ApolloQueryResult<T>>;
    reFetchObservableQueries(includeStandby?: boolean): Promise<ApolloQueryResult<any>[]>;
    readFragment<T, TVariables>(options: Fragment<TVariables, T>, optimistic?: boolean): T;
    readQuery<T, TVariables>(options: Query<TVariables, T>, optimistic?: boolean): T;
    refetchQueries<TCache, TResult>(options: RefetchQueriesOptions<TCache, TResult>): RefetchQueriesResult<TResult>;
    resetStore(): Promise<ApolloQueryResult<any>[]>;
    restore(serializedState: T): ApolloCache<T>;
    setLink(newLink: ApolloLink): void;
    setLocalStateFragmentMatcher(fragmentMatcher: FragmentMatcher): void;
    setResolvers(resolvers: Resolvers | Resolvers[]): void;
    stop(): void;
    subscribe<T, TVariables>(options: SubscriptionOptions<TVariables, T>): Observable<FetchResult<T>>;
    watchFragment<TFragmentData, TVariables>(options: WatchFragmentOptions<TFragmentData, TVariables>): Observable<WatchFragmentResult<TFragmentData>>;
    watchQuery<T, TVariables>(options: WatchQueryOptions<TVariables, T>): ObservableQuery<T, TVariables>;
    writeFragment<TData, TVariables>(options: WriteFragmentOptions<TData, TVariables>): Reference;
    writeQuery<TData, TVariables>(options: WriteQueryOptions<TData, TVariables>): Reference;
}

Type Parameters

  • T

Hierarchy

  • ApolloClient<T>
    • IApolloClient

Properties

cache: ApolloCache<T>
defaultOptions: DefaultOptions
disableNetworkFetches: boolean
getMemoryInternals?: (() => {
    limits: {
        [k: string]: number;
    };
    sizes: {
        addTypenameDocumentTransform?: {
            cache: number;
        }[];
        cache?: {
            fragmentQueryDocuments: number;
        };
        canonicalStringify: number;
        fragmentRegistry?: {
            findFragmentSpreads: number;
            lookup: number;
            transform: number;
        };
        inMemoryCache?: {
            executeSelectionSet: number;
            executeSubSelectedArray: number;
            maybeBroadcastWatch: number;
        };
        links: unknown[];
        parser: number;
        print: number;
        queryManager: {
            documentTransforms: {
                cache: number;
            }[];
            getDocumentInfo: number;
        };
    };
})

This is not a stable API - it is used in development builds to expose information to the DevTools. Use at your own risk! For more details, see Memory Management

console.log(client.getMemoryInternals())

Logs output in the following JSON format:

{
limits: {
parser: 1000,
canonicalStringify: 1000,
print: 2000,
'documentTransform.cache': 2000,
'queryManager.getDocumentInfo': 2000,
'PersistedQueryLink.persistedQueryHashes': 2000,
'fragmentRegistry.transform': 2000,
'fragmentRegistry.lookup': 1000,
'fragmentRegistry.findFragmentSpreads': 4000,
'cache.fragmentQueryDocuments': 1000,
'removeTypenameFromVariables.getVariableDefinitions': 2000,
'inMemoryCache.maybeBroadcastWatch': 5000,
'inMemoryCache.executeSelectionSet': 10000,
'inMemoryCache.executeSubSelectedArray': 5000
},
sizes: {
parser: 26,
canonicalStringify: 4,
print: 14,
addTypenameDocumentTransform: [
{
cache: 14,
},
],
queryManager: {
getDocumentInfo: 14,
documentTransforms: [
{
cache: 14,
},
{
cache: 14,
},
],
},
fragmentRegistry: {
findFragmentSpreads: 34,
lookup: 20,
transform: 14,
},
cache: {
fragmentQueryDocuments: 22,
},
inMemoryCache: {
executeSelectionSet: 4345,
executeSubSelectedArray: 1206,
maybeBroadcastWatch: 32,
},
links: [
{
PersistedQueryLink: {
persistedQueryHashes: 14,
},
},
{
removeTypenameFromVariables: {
getVariableDefinitions: 14,
},
},
],
},
}
jwt_token?: string
link: ApolloLink
path?: string
queryDeduplication: boolean
ssl?: boolean
typeDefs:
    | string
    | string[]
    | DocumentNode
    | DocumentNode[]
version: string

Accessors

  • get defaultContext(): Partial<DefaultContext>
  • Returns Partial<DefaultContext>

  • get documentTransform(): DocumentTransform
  • The DocumentTransform used to modify GraphQL documents before a request is made. If a custom DocumentTransform is not provided, this will be the default document transform.

    Returns DocumentTransform

Methods

  • Parameters

    • cb: (() => any)
        • (): any
        • Returns any

    Returns void

  • Parameters

    • payload: GraphQLRequest<Record<string, any>>

    Returns Observable<ExecutionResult<{
        [key: string]: any;
    }, {
        [key: string]: any;
    }>>

  • Add additional local resolvers.

    Parameters

    • resolvers: Resolvers | Resolvers[]

    Returns void

  • Remove all data from the store. Unlike resetStore, clearStore will not refetch any active queries.

    Returns Promise<any[]>

  • Exposes the cache's complete state, in a serializable format for later restoration.

    Parameters

    • Optionaloptimistic: boolean

    Returns T

  • Get all currently active ObservableQuery objects, in a Map keyed by query ID strings.

    An "active" query is one that has observers and a fetchPolicy other than "standby" or "cache-only".

    You can include all ObservableQuery objects (including the inactive ones) by passing "all" instead of "active", or you can include just a subset of active queries by passing an array of query names or DocumentNode objects.

    Parameters

    • Optionalinclude: RefetchQueriesInclude

    Returns Map<string, ObservableQuery<any, OperationVariables>>

  • Get all registered local resolvers.

    Returns Resolvers

  • 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. In some cases both data and errors might be undefined, for example when errorPolicy is set to 'ignore'.

    It takes options as an object with the following keys and values:

    Type Parameters

    • TData = any
    • TVariables extends OperationVariables = OperationVariables
    • TContext extends Record<string, any> = DefaultContext
    • TCache extends ApolloCache<any> = ApolloCache<any>

    Parameters

    Returns Promise<FetchResult<TData>>

  • 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.

    Parameters

    • cb: (() => Promise<any>)
        • (): Promise<any>
        • Returns Promise<any>

    Returns (() => void)

      • (): void
      • Returns void

  • 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.

    Parameters

    • cb: (() => Promise<any>)
        • (): Promise<any>
        • Returns Promise<any>

    Returns (() => void)

      • (): void
      • Returns void

  • 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.

    Type Parameters

    • T = any
    • TVariables extends OperationVariables = OperationVariables

    Parameters

    • options: QueryOptions<TVariables, T>

      An object of type QueryOptions that allows us to describe how this query should be treated e.g. whether it should hit the server at all or just resolve from the cache, etc.

    Returns Promise<ApolloQueryResult<T>>

  • 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.

    Parameters

    • OptionalincludeStandby: boolean

    Returns Promise<ApolloQueryResult<any>[]>

  • 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.

    Type Parameters

    • T = any
    • TVariables = OperationVariables

    Parameters

    • options: Fragment<TVariables, T>
    • Optionaloptimistic: boolean

      Set to true to allow readFragment to return optimistic results. Is false by default.

    Returns T

  • 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.

    Type Parameters

    • T = any
    • TVariables = OperationVariables

    Parameters

    • options: Query<TVariables, T>
    • Optionaloptimistic: boolean

      Set to true to allow readQuery to return optimistic results. Is false by default.

    Returns T

  • 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.

    Type Parameters

    • TCache extends ApolloCache<any> = ApolloCache<T>
    • TResult = Promise<ApolloQueryResult<any>>

    Parameters

    Returns RefetchQueriesResult<TResult>

  • 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.

    Returns Promise<ApolloQueryResult<any>[]>

  • Replaces existing state in the cache (if any) with the values expressed by serializedState.

    Called when hydrating a cache (server side rendering, or offline storage), and also (potentially) during hot reloads.

    Parameters

    • serializedState: T

    Returns ApolloCache<T>

  • Define a new ApolloLink (or link chain) that Apollo Client will use.

    Parameters

    • newLink: ApolloLink

    Returns void

  • Set a custom local state fragment matcher.

    Parameters

    • fragmentMatcher: FragmentMatcher

    Returns void

  • Set (override existing) local resolvers.

    Parameters

    • resolvers: Resolvers | Resolvers[]

    Returns void

  • Call this method to terminate any active client processes, making it safe to dispose of this ApolloClient instance.

    Returns void

  • This subscribes to a graphql subscription according to the options specified and returns an Observable which either emits received data or an error.

    Type Parameters

    • T = any
    • TVariables extends OperationVariables = OperationVariables

    Parameters

    Returns Observable<FetchResult<T>>

  • Watches the cache store of the fragment according to the options specified and returns an Observable. We can subscribe to this Observable and receive updated results through an observer when the cache store changes.

    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.

    Type Parameters

    • TFragmentData = unknown
    • TVariables = OperationVariables

    Parameters

    • options: WatchFragmentOptions<TFragmentData, TVariables>

      An object of type WatchFragmentOptions that allows the cache to identify the fragment and optionally specify whether to react to optimistic updates.

    Returns Observable<WatchFragmentResult<TFragmentData>>

    3.10.0

  • 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 an observer when the cache store changes.

    Note that this method is not an implementation of GraphQL subscriptions. Rather, it uses Apollo's store in order to reactively deliver updates to your query results.

    For example, suppose you call watchQuery on a GraphQL query that fetches a person's first and last name and this person has a particular object identifier, provided by dataIdFromObject. Later, a different query fetches that same person's first and last name and the first name has now changed. Then, any observers associated with the results of the first query will be updated with a new result object.

    Note that if the cache does not change, the subscriber will not be notified.

    See here for a description of store reactivity.

    Type Parameters

    • T = any
    • TVariables extends OperationVariables = OperationVariables

    Parameters

    Returns ObservableQuery<T, TVariables>

  • 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.

    Type Parameters

    • TData = any
    • TVariables = OperationVariables

    Parameters

    Returns Reference

  • 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.

    Type Parameters

    • TData = any
    • TVariables = OperationVariables

    Parameters

    Returns Reference