UNPKG

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