@apollo/client 4.1.0-alpha.9 → 4.1.0-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +22 -0
- package/__cjs/cache/core/types/Cache.d.cts +3 -2
- package/__cjs/cache/index.cjs.map +1 -1
- package/__cjs/cache/index.d.cts +1 -1
- package/__cjs/cache/inmemory/policies.cjs +72 -22
- package/__cjs/cache/inmemory/policies.cjs.map +1 -1
- package/__cjs/cache/inmemory/policies.d.cts +31 -9
- package/__cjs/cache/inmemory/types.d.cts +3 -1
- package/__cjs/cache/inmemory/writeToStore.cjs +18 -4
- package/__cjs/cache/inmemory/writeToStore.cjs.map +1 -1
- package/__cjs/cache/inmemory/writeToStore.d.cts +0 -1
- package/__cjs/core/QueryInfo.cjs +6 -1
- package/__cjs/core/QueryInfo.cjs.map +1 -1
- package/__cjs/core/QueryInfo.d.cts +4 -3
- package/__cjs/core/QueryManager.cjs +13 -3
- package/__cjs/core/QueryManager.cjs.map +1 -1
- package/__cjs/core/QueryManager.d.cts +2 -1
- package/__cjs/incremental/handlers/graphql17Alpha9.cjs +39 -0
- package/__cjs/incremental/handlers/graphql17Alpha9.cjs.map +1 -1
- package/__cjs/incremental/handlers/graphql17Alpha9.d.cts +1 -0
- package/__cjs/incremental/types.d.cts +9 -0
- package/__cjs/react/query-preloader/createQueryPreloader.cjs +41 -1
- package/__cjs/react/query-preloader/createQueryPreloader.cjs.map +1 -1
- package/__cjs/utilities/internal/constants.cjs +6 -1
- package/__cjs/utilities/internal/constants.cjs.map +1 -1
- package/__cjs/utilities/internal/constants.d.cts +5 -0
- package/__cjs/utilities/internal/index.cjs +2 -1
- package/__cjs/utilities/internal/index.cjs.map +1 -1
- package/__cjs/utilities/internal/index.d.cts +3 -1
- package/__cjs/utilities/internal/ponyfills/FinalizationRegistry.cjs +61 -0
- package/__cjs/utilities/internal/ponyfills/FinalizationRegistry.cjs.map +1 -0
- package/__cjs/utilities/internal/ponyfills/FinalizationRegistry.d.cts +11 -0
- package/__cjs/utilities/internal/ponyfills/index.cjs +6 -0
- package/__cjs/utilities/internal/ponyfills/index.cjs.map +1 -0
- package/__cjs/utilities/internal/ponyfills/index.d.cts +3 -0
- package/__cjs/utilities/internal/ponyfills/index.react-native.cjs +6 -0
- package/__cjs/utilities/internal/ponyfills/index.react-native.cjs.map +1 -0
- package/__cjs/utilities/internal/ponyfills/index.react-native.d.cts +2 -0
- package/__cjs/utilities/internal/types/ExtensionsWithStreamDetails.cjs +3 -0
- package/__cjs/utilities/internal/types/ExtensionsWithStreamDetails.cjs.map +1 -0
- package/__cjs/utilities/internal/types/ExtensionsWithStreamDetails.d.cts +12 -0
- package/__cjs/utilities/internal/types/StreamInfoTrie.cjs +3 -0
- package/__cjs/utilities/internal/types/StreamInfoTrie.cjs.map +1 -0
- package/__cjs/utilities/internal/types/StreamInfoTrie.d.cts +16 -0
- package/__cjs/version.cjs +1 -1
- package/__cjs/version.cjs.map +1 -1
- package/cache/core/types/Cache.d.ts +3 -2
- package/cache/core/types/Cache.js.map +1 -1
- package/cache/index.d.ts +1 -1
- package/cache/index.js.map +1 -1
- package/cache/inmemory/policies.d.ts +31 -9
- package/cache/inmemory/policies.js +60 -11
- package/cache/inmemory/policies.js.map +1 -1
- package/cache/inmemory/types.d.ts +3 -1
- package/cache/inmemory/types.js.map +1 -1
- package/cache/inmemory/writeToStore.d.ts +0 -1
- package/cache/inmemory/writeToStore.js +20 -6
- package/cache/inmemory/writeToStore.js.map +1 -1
- package/core/QueryInfo.d.ts +4 -3
- package/core/QueryInfo.js +7 -2
- package/core/QueryInfo.js.map +1 -1
- package/core/QueryManager.d.ts +2 -1
- package/core/QueryManager.js +14 -4
- package/core/QueryManager.js.map +1 -1
- package/incremental/handlers/graphql17Alpha9.d.ts +1 -0
- package/incremental/handlers/graphql17Alpha9.js +40 -1
- package/incremental/handlers/graphql17Alpha9.js.map +1 -1
- package/incremental/types.d.ts +9 -0
- package/incremental/types.js.map +1 -1
- package/legacyEntryPoints/utilities/internal/ponyfills/index.d.ts +1 -0
- package/legacyEntryPoints/utilities/internal/ponyfills/index.js +1 -0
- package/legacyEntryPoints/utilities/internal/ponyfills/ponyfills.cjs +1 -0
- package/legacyEntryPoints/utilities/internal/ponyfills/ponyfills.d.cts +1 -0
- package/package.json +15 -1
- package/react/query-preloader/createQueryPreloader.js +41 -1
- package/react/query-preloader/createQueryPreloader.js.map +1 -1
- package/utilities/internal/constants.d.ts +5 -0
- package/utilities/internal/constants.js +5 -0
- package/utilities/internal/constants.js.map +1 -1
- package/utilities/internal/index.d.ts +3 -1
- package/utilities/internal/index.js +1 -1
- package/utilities/internal/index.js.map +1 -1
- package/utilities/internal/ponyfills/FinalizationRegistry.d.ts +11 -0
- package/utilities/internal/ponyfills/FinalizationRegistry.js +57 -0
- package/utilities/internal/ponyfills/FinalizationRegistry.js.map +1 -0
- package/utilities/internal/ponyfills/index.d.ts +3 -0
- package/utilities/internal/ponyfills/index.js +3 -0
- package/utilities/internal/ponyfills/index.js.map +1 -0
- package/utilities/internal/ponyfills/index.react-native.d.ts +2 -0
- package/utilities/internal/ponyfills/index.react-native.js +2 -0
- package/utilities/internal/ponyfills/index.react-native.js.map +1 -0
- package/utilities/internal/types/ExtensionsWithStreamDetails.d.ts +12 -0
- package/utilities/internal/types/ExtensionsWithStreamDetails.js +2 -0
- package/utilities/internal/types/ExtensionsWithStreamDetails.js.map +1 -0
- package/utilities/internal/types/StreamInfoTrie.d.ts +16 -0
- package/utilities/internal/types/StreamInfoTrie.js +2 -0
- package/utilities/internal/types/StreamInfoTrie.js.map +1 -0
- package/version.js +1 -1
- package/version.js.map +1 -1
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { FieldNode, FragmentDefinitionNode, InlineFragmentNode, SelectionSetNode } from "graphql";
|
|
2
2
|
import type { OperationVariables } from "@apollo/client";
|
|
3
|
+
import type { Incremental } from "@apollo/client/incremental";
|
|
3
4
|
import type { Reference, StoreObject, StoreValue } from "@apollo/client/utilities";
|
|
4
5
|
import { isReference } from "@apollo/client/utilities";
|
|
5
6
|
import type { FragmentMap } from "@apollo/client/utilities/internal";
|
|
@@ -36,10 +37,10 @@ export type KeyArgsFunction = (args: Record<string, any> | null, context: {
|
|
|
36
37
|
field: FieldNode | null;
|
|
37
38
|
variables?: Record<string, any>;
|
|
38
39
|
}) => KeySpecifier | false | ReturnType<IdGetter>;
|
|
39
|
-
export type FieldPolicy<TExisting = any, TIncoming = TExisting, TReadResult = TIncoming,
|
|
40
|
+
export type FieldPolicy<TExisting = any, TIncoming = TExisting, TReadResult = TIncoming, TReadOptions extends FieldReadFunctionOptions = FieldReadFunctionOptions, TMergeOptions extends FieldMergeFunctionOptions = FieldMergeFunctionOptions> = {
|
|
40
41
|
keyArgs?: KeySpecifier | KeyArgsFunction | false;
|
|
41
|
-
read?: FieldReadFunction<TExisting, TReadResult,
|
|
42
|
-
merge?: FieldMergeFunction<TExisting, TIncoming,
|
|
42
|
+
read?: FieldReadFunction<TExisting, TReadResult, TReadOptions>;
|
|
43
|
+
merge?: FieldMergeFunction<TExisting, TIncoming, TMergeOptions> | boolean;
|
|
43
44
|
};
|
|
44
45
|
export type StorageType = Record<string, any>;
|
|
45
46
|
export interface FieldFunctionOptions<TArgs = Record<string, any>, TVariables extends OperationVariables = Record<string, any>> {
|
|
@@ -55,15 +56,36 @@ export interface FieldFunctionOptions<TArgs = Record<string, any>, TVariables ex
|
|
|
55
56
|
readField: ReadFieldFunction;
|
|
56
57
|
canRead: CanReadFunction;
|
|
57
58
|
mergeObjects: MergeObjectsFunction;
|
|
59
|
+
}
|
|
60
|
+
export interface FieldReadFunctionOptions<TArgs = Record<string, any>, TVariables extends OperationVariables = Record<string, any>> extends FieldFunctionOptions<TArgs, TVariables> {
|
|
61
|
+
}
|
|
62
|
+
export interface FieldMergeFunctionOptions<TArgs = Record<string, any>, TVariables extends OperationVariables = Record<string, any>> extends FieldFunctionOptions<TArgs, TVariables> {
|
|
63
|
+
/**
|
|
64
|
+
* Any `extensions` provided when writing the cache.
|
|
65
|
+
*/
|
|
66
|
+
extensions: Record<string, unknown> | undefined;
|
|
67
|
+
/**
|
|
68
|
+
* Details about the field when the `@stream` directive is used. Useful with
|
|
69
|
+
* custom merge functions to determine how to merge existing cache data with
|
|
70
|
+
* the incoming stream array.
|
|
71
|
+
*
|
|
72
|
+
* This field is only available in `merge` functions when the `@stream`
|
|
73
|
+
* directive is used on the field.
|
|
74
|
+
*
|
|
75
|
+
* > [!NOTE]
|
|
76
|
+
* > This field is not available when using the `Defer20220824Handler`
|
|
77
|
+
*/
|
|
78
|
+
streamFieldInfo?: Incremental.StreamFieldInfo;
|
|
58
79
|
/**
|
|
59
|
-
*
|
|
60
|
-
*
|
|
80
|
+
* The same value as the `existing` argument, but preserves the `existing`
|
|
81
|
+
* value on refetches when `refetchWritePolicy` is `overwrite` (the default).
|
|
61
82
|
*/
|
|
62
|
-
|
|
83
|
+
existingData: unknown;
|
|
63
84
|
}
|
|
64
85
|
type MergeObjectsFunction = <T extends StoreObject | Reference>(existing: T, incoming: T) => T;
|
|
65
|
-
export type FieldReadFunction<TExisting = any, TReadResult = TExisting, TOptions extends
|
|
66
|
-
export type FieldMergeFunction<TExisting = any, TIncoming = TExisting, TOptions extends
|
|
86
|
+
export type FieldReadFunction<TExisting = any, TReadResult = TExisting, TOptions extends FieldReadFunctionOptions = FieldReadFunctionOptions> = (existing: SafeReadonly<TExisting> | undefined, options: TOptions) => TReadResult | undefined;
|
|
87
|
+
export type FieldMergeFunction<TExisting = any, TIncoming = TExisting, TOptions extends FieldMergeFunctionOptions = FieldMergeFunctionOptions> = (existing: SafeReadonly<TExisting> | undefined, incoming: SafeReadonly<TIncoming>, options: TOptions) => SafeReadonly<TExisting>;
|
|
88
|
+
export declare const defaultStreamFieldMergeFn: FieldMergeFunction<Array<any>>;
|
|
67
89
|
export type PossibleTypesMap = {
|
|
68
90
|
[supertype: string]: string[];
|
|
69
91
|
};
|
|
@@ -97,7 +119,7 @@ export declare class Policies {
|
|
|
97
119
|
readField<V = StoreValue>(options: ReadFieldOptions, context: ReadMergeModifyContext): SafeReadonly<V> | undefined;
|
|
98
120
|
getReadFunction(typename: string | undefined, fieldName: string): FieldReadFunction | undefined;
|
|
99
121
|
getMergeFunction(parentTypename: string | undefined, fieldName: string, childTypename: string | undefined): FieldMergeFunction | undefined;
|
|
100
|
-
runMergeFunction(existing: StoreValue, incoming: StoreValue, { field, typename, merge }: MergeInfo, context: WriteContext, storage?: StorageType): any;
|
|
122
|
+
runMergeFunction(existing: StoreValue, incoming: StoreValue, { field, typename, merge, path }: MergeInfo, context: WriteContext, storage?: StorageType): any;
|
|
101
123
|
}
|
|
102
124
|
export declare function normalizeReadFieldOptions(readFieldArgs: any[], objectOrReference: StoreObject | Reference | undefined, variables?: ReadMergeModifyContext["variables"]): ReadFieldOptions;
|
|
103
125
|
export {};
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
import { equal } from "@wry/equality";
|
|
1
2
|
import { disableWarningsSlot } from "@apollo/client/masking";
|
|
2
3
|
import { isReference } from "@apollo/client/utilities";
|
|
3
4
|
import { __DEV__ } from "@apollo/client/utilities/environment";
|
|
5
|
+
import { streamInfoSymbol } from "@apollo/client/utilities/internal";
|
|
4
6
|
import { argumentsObjectFromField, getStoreKeyName, isArray, isNonNullObject, storeKeyNameFromField, stringifyForDisplay, } from "@apollo/client/utilities/internal";
|
|
5
7
|
import { invariant, newInvariantError, } from "@apollo/client/utilities/invariant";
|
|
6
8
|
import { defaultDataIdFromObject, fieldNameFromStoreName, hasOwn, selectionSetMatchesResult, storeValueIsStoreObject, TypeOrFieldNameRegExp, } from "./helpers.js";
|
|
@@ -17,6 +19,20 @@ const simpleKeyArgsFn = (_args, context) => context.fieldName;
|
|
|
17
19
|
// merge:false in a field policy.
|
|
18
20
|
const mergeTrueFn = (existing, incoming, { mergeObjects }) => mergeObjects(existing, incoming);
|
|
19
21
|
const mergeFalseFn = (_, incoming) => incoming;
|
|
22
|
+
export const defaultStreamFieldMergeFn = (existing, incoming, { streamFieldInfo, existingData }) => {
|
|
23
|
+
if (!existing && !existingData) {
|
|
24
|
+
return incoming;
|
|
25
|
+
}
|
|
26
|
+
const results = [];
|
|
27
|
+
const previous = existing ?? existingData;
|
|
28
|
+
const length = streamFieldInfo?.isLastChunk ?
|
|
29
|
+
incoming.length
|
|
30
|
+
: Math.max(previous.length, incoming.length);
|
|
31
|
+
for (let i = 0; i < length; i++) {
|
|
32
|
+
results[i] = incoming[i] === undefined ? previous[i] : incoming[i];
|
|
33
|
+
}
|
|
34
|
+
return results;
|
|
35
|
+
};
|
|
20
36
|
export class Policies {
|
|
21
37
|
config;
|
|
22
38
|
typePolicies = {};
|
|
@@ -476,7 +492,9 @@ export class Policies {
|
|
|
476
492
|
}
|
|
477
493
|
return merge;
|
|
478
494
|
}
|
|
479
|
-
runMergeFunction(existing, incoming, { field, typename, merge }, context, storage) {
|
|
495
|
+
runMergeFunction(existing, incoming, { field, typename, merge, path }, context, storage) {
|
|
496
|
+
// Preserve the value in case `context.overwrite` is set.
|
|
497
|
+
const existingData = existing;
|
|
480
498
|
if (merge === mergeTrueFn) {
|
|
481
499
|
// Instead of going to the trouble of creating a full
|
|
482
500
|
// FieldFunctionOptions object and calling mergeTrueFn, we can
|
|
@@ -494,7 +512,18 @@ export class Policies {
|
|
|
494
512
|
if (context.overwrite) {
|
|
495
513
|
existing = void 0;
|
|
496
514
|
}
|
|
497
|
-
|
|
515
|
+
const streamInfo = context.extensions?.[streamInfoSymbol]
|
|
516
|
+
?.deref()
|
|
517
|
+
?.peekArray(path);
|
|
518
|
+
if (streamInfo) {
|
|
519
|
+
const { current, previous } = streamInfo;
|
|
520
|
+
if (previous &&
|
|
521
|
+
equal(previous.incoming, incoming) &&
|
|
522
|
+
equal(previous.streamFieldInfo, current)) {
|
|
523
|
+
return previous.result;
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
const result = merge(existing, incoming, makeMergeFieldFunctionOptions(this,
|
|
498
527
|
// Unlike options.readField for read functions, we do not fall
|
|
499
528
|
// back to the current object if no foreignObjOrRef is provided,
|
|
500
529
|
// because it's not clear what the current object should be for
|
|
@@ -511,7 +540,16 @@ export class Policies {
|
|
|
511
540
|
fieldName: field.name.value,
|
|
512
541
|
field,
|
|
513
542
|
variables: context.variables,
|
|
514
|
-
|
|
543
|
+
path,
|
|
544
|
+
}, context, storage || {}, existingData));
|
|
545
|
+
if (streamInfo) {
|
|
546
|
+
streamInfo.previous = {
|
|
547
|
+
incoming,
|
|
548
|
+
streamFieldInfo: streamInfo.current,
|
|
549
|
+
result,
|
|
550
|
+
};
|
|
551
|
+
}
|
|
552
|
+
return result;
|
|
515
553
|
}
|
|
516
554
|
}
|
|
517
555
|
function makeFieldFunctionOptions(policies, objectOrReference, fieldSpec, context, storage) {
|
|
@@ -519,7 +557,7 @@ function makeFieldFunctionOptions(policies, objectOrReference, fieldSpec, contex
|
|
|
519
557
|
const fieldName = fieldNameFromStoreName(storeFieldName);
|
|
520
558
|
const variables = fieldSpec.variables || context.variables;
|
|
521
559
|
const { toReference, canRead } = context.store;
|
|
522
|
-
|
|
560
|
+
return {
|
|
523
561
|
args: argsFromFieldSpecifier(fieldSpec),
|
|
524
562
|
field: fieldSpec.field || null,
|
|
525
563
|
fieldName,
|
|
@@ -535,13 +573,24 @@ function makeFieldFunctionOptions(policies, objectOrReference, fieldSpec, contex
|
|
|
535
573
|
},
|
|
536
574
|
mergeObjects: makeMergeObjectsFunction(context.store),
|
|
537
575
|
};
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
576
|
+
}
|
|
577
|
+
function makeMergeFieldFunctionOptions(policies, objectOrReference, fieldSpec, context, storage, existingData) {
|
|
578
|
+
const options = {
|
|
579
|
+
...makeFieldFunctionOptions(policies, objectOrReference, fieldSpec, context, storage),
|
|
580
|
+
extensions: context.extensions,
|
|
581
|
+
existingData,
|
|
582
|
+
};
|
|
583
|
+
const extensions = context.extensions;
|
|
584
|
+
if (extensions && streamInfoSymbol in extensions) {
|
|
585
|
+
const { [streamInfoSymbol]: streamInfo, ...otherExtensions } = extensions;
|
|
586
|
+
const streamFieldInfo = streamInfo?.deref()?.peekArray(fieldSpec.path);
|
|
587
|
+
if (streamFieldInfo) {
|
|
588
|
+
options.streamFieldInfo = streamFieldInfo.current;
|
|
589
|
+
}
|
|
590
|
+
// If the only key in `extensions` was the stream details key, we didn't
|
|
591
|
+
// receive any remote extensions, so we reset extensions back to undefined
|
|
592
|
+
options.extensions =
|
|
593
|
+
Object.keys(otherExtensions).length === 0 ? undefined : otherExtensions;
|
|
545
594
|
}
|
|
546
595
|
return options;
|
|
547
596
|
}
|