UNPKG

apollo-client

Version:
210 lines (209 loc) 10.6 kB
import { ApolloLink, FetchResult, GraphQLRequest } from 'apollo-link'; import { ExecutionResult } from 'graphql'; import { ApolloCache, DataProxy } from 'apollo-cache'; import { QueryManager } from './core/QueryManager'; import { ApolloQueryResult, OperationVariables } from './core/types'; import { ObservableQuery } from './core/ObservableQuery'; import { Observable } from './util/Observable'; import { QueryBaseOptions, QueryOptions, WatchQueryOptions, SubscriptionOptions, MutationOptions, ModifiableWatchQueryOptions, MutationBaseOptions } from './core/watchQueryOptions'; import { DataStore } from './data/store'; export interface DefaultOptions { watchQuery?: ModifiableWatchQueryOptions; query?: QueryBaseOptions; mutate?: MutationBaseOptions; } export declare type ApolloClientOptions<TCacheShape> = { link: ApolloLink; cache: ApolloCache<TCacheShape>; ssrMode?: boolean; ssrForceFetchDelay?: number; connectToDevTools?: boolean; queryDeduplication?: boolean; defaultOptions?: DefaultOptions; }; /** * This is the primary Apollo Client class. It is used to send GraphQL documents (i.e. queries * and mutations) to a GraphQL spec-compliant server over a {@link NetworkInterface} instance, * receive results from the server and cache the results in a store. It also delivers updates * to GraphQL queries through {@link Observable} instances. */ export default class ApolloClient<TCacheShape> implements DataProxy { link: ApolloLink; store: DataStore<TCacheShape>; cache: ApolloCache<TCacheShape>; queryManager: QueryManager<TCacheShape>; disableNetworkFetches: boolean; version: string; queryDeduplication: boolean; defaultOptions: DefaultOptions; private devToolsHookCb; private proxy; private ssrMode; private resetStoreCallbacks; /** * Constructs an instance of {@link ApolloClient}. * * @param link The {@link ApolloLink} over which GraphQL documents will be resolved into a response. * * @param cache The initial cache to use in the data store. * * @param ssrMode Determines whether this is being run in Server Side Rendering (SSR) mode. * * @param ssrForceFetchDelay Determines the time interval before we force fetch queries for a * server side render. * * @param queryDeduplication If set to false, a query will still be sent to the server even if a query * with identical parameters (query, variables, operationName) is already in flight. * */ constructor(options: ApolloClientOptions<TCacheShape>); /** * This watches the results of the query according to the options specified and * returns an {@link ObservableQuery}. We can subscribe to this {@link ObservableQuery} and * receive updated results through a GraphQL observer. * <p /><p /> * 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. * <p /><p /> * For example, suppose you call watchQuery on a GraphQL query that fetches an person's * first name and last name and this person has a particular object identifer, provided by * dataIdFromObject. Later, a different query fetches that same person's * first and last name and his/her first name has now changed. Then, any observers associated * with the results of the first query will be updated with a new result object. * <p /><p /> * See [here](https://medium.com/apollo-stack/the-concepts-of-graphql-bc68bd819be3#.3mb0cbcmc) for * a description of store reactivity. * */ watchQuery<T, TVariables = OperationVariables>(options: WatchQueryOptions<TVariables>): ObservableQuery<T>; /** * This resolves a single query according to the options specified and * returns a {@link Promise} which is either resolved with the resulting data * or rejected with an error. * * @param options An object of type {@link 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. */ query<T, TVariables = OperationVariables>(options: QueryOptions<TVariables>): Promise<ApolloQueryResult<T>>; /** * This resolves a single mutation according to the options specified and returns a * {@link 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: */ mutate<T, TVariables = OperationVariables>(options: MutationOptions<T, TVariables>): Promise<FetchResult<T>>; /** * This subscribes to a graphql subscription according to the options specified and returns an * {@link Observable} which either emits received data or an error. */ subscribe<T = any, TVariables = OperationVariables>(options: SubscriptionOptions<TVariables>): Observable<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`. */ readQuery<T, TVariables = OperationVariables>(options: DataProxy.Query<TVariables>): T | null; /** * 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`. */ readFragment<T, TVariables = OperationVariables>(options: DataProxy.Fragment<TVariables>): T | null; /** * 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 a * specific id returned by `dataIdFromObject` then use `writeFragment`. */ writeQuery<TData = any, TVariables = OperationVariables>(options: DataProxy.WriteQueryOptions<TData, TVariables>): void; /** * 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`. */ writeFragment<TData = any, TVariables = OperationVariables>(options: DataProxy.WriteFragmentOptions<TData, TVariables>): void; /** * Sugar for writeQuery & writeFragment * This method will construct a query from the data object passed in. * If no id is supplied, writeData will write the data to the root. * If an id is supplied, writeData will write a fragment to the object * specified by the id in the store. * * Since you aren't passing in a query to check the shape of the data, * you must pass in an object that conforms to the shape of valid GraphQL data. */ writeData<TData = any>(options: DataProxy.WriteDataOptions<TData>): void; __actionHookForDevTools(cb: () => any): void; __requestRaw(payload: GraphQLRequest): Observable<ExecutionResult>; /** * This initializes the query manager that tracks queries and the cache */ initQueryManager(): void; /** * 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. */ resetStore(): Promise<ApolloQueryResult<any>[] | null>; /** * Allows callbacks to be registered that are executed with the store is reset. * onResetStore returns an unsubscribe function for removing your registered callbacks. */ onResetStore(cb: () => Promise<any>): () => void; /** * 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. */ reFetchObservableQueries(includeStandby?: boolean): Promise<ApolloQueryResult<any>[]> | Promise<null>; /** * Exposes the cache's complete state, in a serializable format for later restoration. */ extract(optimistic?: boolean): TCacheShape; /** * 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. */ restore(serializedState: TCacheShape): ApolloCache<TCacheShape>; /** * Initializes a data proxy for this client instance if one does not already * exist and returns either a previously initialized proxy instance or the * newly initialized instance. */ private initProxy; }