UNPKG

5.15 kBTypeScriptView Raw
1import type { InlineFragmentNode, FragmentDefinitionNode, SelectionSetNode, FieldNode } from "graphql";
2import type { FragmentMap, StoreValue, StoreObject, Reference } from "../../utilities/index.js";
3import { isReference } from "../../utilities/index.js";
4import type { IdGetter, MergeInfo, ReadMergeModifyContext } from "./types.js";
5import type { InMemoryCache } from "./inMemoryCache.js";
6import type { SafeReadonly, FieldSpecifier, ToReferenceFunction, ReadFieldFunction, ReadFieldOptions, CanReadFunction } from "../core/types/common.js";
7import type { WriteContext } from "./writeToStore.js";
8export type TypePolicies = {
9 [__typename: string]: TypePolicy;
10};
11export type KeySpecifier = ReadonlyArray<string | KeySpecifier>;
12export type KeyFieldsContext = {
13 typename: string | undefined;
14 storeObject: StoreObject;
15 readField: ReadFieldFunction;
16 selectionSet?: SelectionSetNode;
17 fragmentMap?: FragmentMap;
18 keyObject?: Record<string, any>;
19};
20export type KeyFieldsFunction = (object: Readonly<StoreObject>, context: KeyFieldsContext) => KeySpecifier | false | ReturnType<IdGetter>;
21export type TypePolicy = {
22 keyFields?: KeySpecifier | KeyFieldsFunction | false;
23 merge?: FieldMergeFunction | boolean;
24 queryType?: true;
25 mutationType?: true;
26 subscriptionType?: true;
27 fields?: {
28 [fieldName: string]: FieldPolicy<any> | FieldReadFunction<any>;
29 };
30};
31export type KeyArgsFunction = (args: Record<string, any> | null, context: {
32 typename: string;
33 fieldName: string;
34 field: FieldNode | null;
35 variables?: Record<string, any>;
36}) => KeySpecifier | false | ReturnType<IdGetter>;
37export type FieldPolicy<TExisting = any, TIncoming = TExisting, TReadResult = TIncoming, TOptions extends FieldFunctionOptions = FieldFunctionOptions> = {
38 keyArgs?: KeySpecifier | KeyArgsFunction | false;
39 read?: FieldReadFunction<TExisting, TReadResult, TOptions>;
40 merge?: FieldMergeFunction<TExisting, TIncoming, TOptions> | boolean;
41};
42export type StorageType = Record<string, any>;
43export interface FieldFunctionOptions<TArgs = Record<string, any>, TVars = Record<string, any>> {
44 args: TArgs | null;
45 fieldName: string;
46 storeFieldName: string;
47 field: FieldNode | null;
48 variables?: TVars;
49 isReference: typeof isReference;
50 toReference: ToReferenceFunction;
51 storage: StorageType;
52 cache: InMemoryCache;
53 readField: ReadFieldFunction;
54 canRead: CanReadFunction;
55 mergeObjects: MergeObjectsFunction;
56}
57type MergeObjectsFunction = <T extends StoreObject | Reference>(existing: T, incoming: T) => T;
58export type FieldReadFunction<TExisting = any, TReadResult = TExisting, TOptions extends FieldFunctionOptions = FieldFunctionOptions> = (existing: SafeReadonly<TExisting> | undefined, options: TOptions) => TReadResult | undefined;
59export type FieldMergeFunction<TExisting = any, TIncoming = TExisting, TOptions extends FieldFunctionOptions = FieldFunctionOptions> = (existing: SafeReadonly<TExisting> | undefined, incoming: SafeReadonly<TIncoming>, options: TOptions) => SafeReadonly<TExisting>;
60export type PossibleTypesMap = {
61 [supertype: string]: string[];
62};
63export declare class Policies {
64 private config;
65 private typePolicies;
66 private toBeAdded;
67 private supertypeMap;
68 private fuzzySubtypes;
69 readonly cache: InMemoryCache;
70 readonly rootIdsByTypename: Record<string, string>;
71 readonly rootTypenamesById: Record<string, string>;
72 readonly usingPossibleTypes = false;
73 constructor(config: {
74 cache: InMemoryCache;
75 dataIdFromObject?: KeyFieldsFunction;
76 possibleTypes?: PossibleTypesMap;
77 typePolicies?: TypePolicies;
78 });
79 identify(object: StoreObject, partialContext?: Partial<KeyFieldsContext>): [string?, StoreObject?];
80 addTypePolicies(typePolicies: TypePolicies): void;
81 private updateTypePolicy;
82 private setRootTypename;
83 addPossibleTypes(possibleTypes: PossibleTypesMap): void;
84 private getTypePolicy;
85 private getFieldPolicy;
86 private getSupertypeSet;
87 fragmentMatches(fragment: InlineFragmentNode | FragmentDefinitionNode, typename: string | undefined, result?: Record<string, any>, variables?: Record<string, any>): boolean;
88 hasKeyArgs(typename: string | undefined, fieldName: string): boolean;
89 getStoreFieldName(fieldSpec: FieldSpecifier): string;
90 readField<V = StoreValue>(options: ReadFieldOptions, context: ReadMergeModifyContext): SafeReadonly<V> | undefined;
91 getReadFunction(typename: string | undefined, fieldName: string): FieldReadFunction | undefined;
92 getMergeFunction(parentTypename: string | undefined, fieldName: string, childTypename: string | undefined): FieldMergeFunction | undefined;
93 runMergeFunction(existing: StoreValue, incoming: StoreValue, { field, typename, merge }: MergeInfo, context: WriteContext, storage?: StorageType): any;
94}
95export declare function normalizeReadFieldOptions(readFieldArgs: IArguments, objectOrReference: StoreObject | Reference | undefined, variables?: ReadMergeModifyContext["variables"]): ReadFieldOptions;
96export {};
97//# sourceMappingURL=policies.d.ts.map
\No newline at end of file