UNPKG

5.97 kBTypeScriptView Raw
1import type { DocumentNode } from "graphql";
2import type { StoreObject, Reference, DeepPartial } from "../../utilities/index.js";
3import { Observable } from "../../utilities/index.js";
4import type { DataProxy } from "./types/DataProxy.js";
5import type { Cache } from "./types/Cache.js";
6import { getApolloCacheMemoryInternals } from "../../utilities/caching/getMemoryInternals.js";
7import type { OperationVariables, TypedDocumentNode } from "../../core/types.js";
8import type { MissingTree } from "./types/common.js";
9export type Transaction<T> = (c: ApolloCache<T>) => void;
10/**
11 * Watched fragment options.
12 */
13export interface WatchFragmentOptions<TData, TVars> {
14 /**
15 * A GraphQL fragment document parsed into an AST with the `gql`
16 * template literal.
17 *
18 * @docGroup 1. Required options
19 */
20 fragment: DocumentNode | TypedDocumentNode<TData, TVars>;
21 /**
22 * An object containing a `__typename` and primary key fields
23 * (such as `id`) identifying the entity object from which the fragment will
24 * be retrieved, or a `{ __ref: "..." }` reference, or a `string` ID
25 * (uncommon).
26 *
27 * @docGroup 1. Required options
28 */
29 from: StoreObject | Reference | string;
30 /**
31 * Any variables that the GraphQL fragment may depend on.
32 *
33 * @docGroup 2. Cache options
34 */
35 variables?: TVars;
36 /**
37 * The name of the fragment defined in the fragment document.
38 *
39 * Required if the fragment document includes more than one fragment,
40 * optional otherwise.
41 *
42 * @docGroup 2. Cache options
43 */
44 fragmentName?: string;
45 /**
46 * If `true`, `watchFragment` returns optimistic results.
47 *
48 * The default value is `true`.
49 *
50 * @docGroup 2. Cache options
51 */
52 optimistic?: boolean;
53}
54/**
55 * Watched fragment results.
56 */
57export type WatchFragmentResult<TData> = {
58 data: TData;
59 complete: true;
60 missing?: never;
61} | {
62 data: DeepPartial<TData>;
63 complete: false;
64 missing: MissingTree;
65};
66export declare abstract class ApolloCache<TSerialized> implements DataProxy {
67 readonly assumeImmutableResults: boolean;
68 abstract read<TData = any, TVariables = any>(query: Cache.ReadOptions<TVariables, TData>): TData | null;
69 abstract write<TData = any, TVariables = any>(write: Cache.WriteOptions<TData, TVariables>): Reference | undefined;
70 abstract diff<T>(query: Cache.DiffOptions): Cache.DiffResult<T>;
71 abstract watch<TData = any, TVariables = any>(watch: Cache.WatchOptions<TData, TVariables>): () => void;
72 abstract reset(options?: Cache.ResetOptions): Promise<void>;
73 abstract evict(options: Cache.EvictOptions): boolean;
74 /**
75 * Replaces existing state in the cache (if any) with the values expressed by
76 * `serializedState`.
77 *
78 * Called when hydrating a cache (server side rendering, or offline storage),
79 * and also (potentially) during hot reloads.
80 */
81 abstract restore(serializedState: TSerialized): ApolloCache<TSerialized>;
82 /**
83 * Exposes the cache's complete state, in a serializable format for later restoration.
84 */
85 abstract extract(optimistic?: boolean): TSerialized;
86 abstract removeOptimistic(id: string): void;
87 batch<U>(options: Cache.BatchOptions<this, U>): U;
88 abstract performTransaction(transaction: Transaction<TSerialized>, optimisticId?: string | null): void;
89 recordOptimisticTransaction(transaction: Transaction<TSerialized>, optimisticId: string): void;
90 transformDocument(document: DocumentNode): DocumentNode;
91 transformForLink(document: DocumentNode): DocumentNode;
92 identify(object: StoreObject | Reference): string | undefined;
93 gc(): string[];
94 modify<Entity extends Record<string, any> = Record<string, any>>(options: Cache.ModifyOptions<Entity>): boolean;
95 readQuery<QueryType, TVariables = any>(options: Cache.ReadQueryOptions<QueryType, TVariables>, optimistic?: boolean): QueryType | null;
96 /**
97 * 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.
98 *
99 * 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`.
100 *
101 * @param options - An object of type `WatchFragmentOptions` that allows the cache to identify the fragment and optionally specify whether to react to optimistic updates.
102 *
103 * @since
104 *
105 * 3.10.0
106 */
107 watchFragment<TData = any, TVars = OperationVariables>(options: WatchFragmentOptions<TData, TVars>): Observable<WatchFragmentResult<TData>>;
108 private getFragmentDoc;
109 readFragment<FragmentType, TVariables = any>(options: Cache.ReadFragmentOptions<FragmentType, TVariables>, optimistic?: boolean): FragmentType | null;
110 writeQuery<TData = any, TVariables = any>({ id, data, ...options }: Cache.WriteQueryOptions<TData, TVariables>): Reference | undefined;
111 writeFragment<TData = any, TVariables = any>({ id, data, fragment, fragmentName, ...options }: Cache.WriteFragmentOptions<TData, TVariables>): Reference | undefined;
112 updateQuery<TData = any, TVariables = any>(options: Cache.UpdateQueryOptions<TData, TVariables>, update: (data: TData | null) => TData | null | void): TData | null;
113 updateFragment<TData = any, TVariables = any>(options: Cache.UpdateFragmentOptions<TData, TVariables>, update: (data: TData | null) => TData | null | void): TData | null;
114 /**
115 * @experimental
116 * @internal
117 * This is not a stable API - it is used in development builds to expose
118 * information to the DevTools.
119 * Use at your own risk!
120 */
121 getMemoryInternals?: typeof getApolloCacheMemoryInternals;
122}
123//# sourceMappingURL=cache.d.ts.map
\No newline at end of file