@apollo/client 4.3.7 → 4.4.0-alpha.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.
Files changed (91) hide show
  1. package/CHANGELOG.md +40 -0
  2. package/__cjs/cache/inmemory/policies.cjs +2 -2
  3. package/__cjs/cache/inmemory/policies.cjs.map +1 -1
  4. package/__cjs/cache/inmemory/policies.d.cts +1 -1
  5. package/__cjs/cache/inmemory/readFromStore.cjs +43 -5
  6. package/__cjs/cache/inmemory/readFromStore.cjs.map +1 -1
  7. package/__cjs/cache/inmemory/types.d.cts +2 -0
  8. package/__cjs/cache/inmemory/writeToStore.cjs +40 -13
  9. package/__cjs/cache/inmemory/writeToStore.cjs.map +1 -1
  10. package/__cjs/cache/inmemory/writeToStore.d.cts +4 -1
  11. package/__cjs/local-state/LocalState.cjs +12 -1
  12. package/__cjs/local-state/LocalState.cjs.map +1 -1
  13. package/__cjs/utilities/internal/argumentsObjectFromField.cjs.map +1 -1
  14. package/__cjs/utilities/internal/argumentsObjectFromField.d.cts +2 -2
  15. package/__cjs/utilities/internal/createFragmentArgumentNode.cjs +13 -0
  16. package/__cjs/utilities/internal/createFragmentArgumentNode.cjs.map +1 -0
  17. package/__cjs/utilities/internal/createFragmentArgumentNode.d.cts +3 -0
  18. package/__cjs/utilities/internal/createFragmentSpreadNode.cjs +17 -0
  19. package/__cjs/utilities/internal/createFragmentSpreadNode.cjs.map +1 -0
  20. package/__cjs/utilities/internal/createFragmentSpreadNode.d.cts +5 -0
  21. package/__cjs/utilities/internal/createNameNode.cjs +8 -0
  22. package/__cjs/utilities/internal/createNameNode.cjs.map +1 -0
  23. package/__cjs/utilities/internal/createNameNode.d.cts +3 -0
  24. package/__cjs/utilities/internal/createVariableNode.cjs +9 -0
  25. package/__cjs/utilities/internal/createVariableNode.cjs.map +1 -0
  26. package/__cjs/utilities/internal/createVariableNode.d.cts +3 -0
  27. package/__cjs/utilities/internal/equalByQuery.cjs +14 -1
  28. package/__cjs/utilities/internal/equalByQuery.cjs.map +1 -1
  29. package/__cjs/utilities/internal/getDefaultValues.cjs.map +1 -1
  30. package/__cjs/utilities/internal/getDefaultValues.d.cts +2 -2
  31. package/__cjs/utilities/internal/getFragmentQueryDocument.cjs +10 -7
  32. package/__cjs/utilities/internal/getFragmentQueryDocument.cjs.map +1 -1
  33. package/__cjs/utilities/internal/getFragmentVariables.cjs +34 -0
  34. package/__cjs/utilities/internal/getFragmentVariables.cjs.map +1 -0
  35. package/__cjs/utilities/internal/getFragmentVariables.d.cts +8 -0
  36. package/__cjs/utilities/internal/index.cjs +4 -2
  37. package/__cjs/utilities/internal/index.cjs.map +1 -1
  38. package/__cjs/utilities/internal/index.d.cts +1 -0
  39. package/__cjs/utilities/internal/removeDirectivesFromDocument.cjs +120 -61
  40. package/__cjs/utilities/internal/removeDirectivesFromDocument.cjs.map +1 -1
  41. package/__cjs/utilities/internal/types/Mutable.cjs +3 -0
  42. package/__cjs/utilities/internal/types/Mutable.cjs.map +1 -0
  43. package/__cjs/utilities/internal/types/Mutable.d.cts +4 -0
  44. package/__cjs/version.cjs +1 -1
  45. package/__cjs/version.cjs.map +1 -1
  46. package/cache/inmemory/policies.d.ts +1 -1
  47. package/cache/inmemory/policies.js +2 -2
  48. package/cache/inmemory/policies.js.map +1 -1
  49. package/cache/inmemory/readFromStore.js +44 -6
  50. package/cache/inmemory/readFromStore.js.map +1 -1
  51. package/cache/inmemory/types.d.ts +2 -0
  52. package/cache/inmemory/types.js.map +1 -1
  53. package/cache/inmemory/writeToStore.d.ts +4 -1
  54. package/cache/inmemory/writeToStore.js +41 -14
  55. package/cache/inmemory/writeToStore.js.map +1 -1
  56. package/local-state/LocalState.js +13 -2
  57. package/local-state/LocalState.js.map +1 -1
  58. package/package.json +2 -2
  59. package/utilities/internal/argumentsObjectFromField.d.ts +2 -2
  60. package/utilities/internal/argumentsObjectFromField.js.map +1 -1
  61. package/utilities/internal/createFragmentArgumentNode.d.ts +3 -0
  62. package/utilities/internal/createFragmentArgumentNode.js +10 -0
  63. package/utilities/internal/createFragmentArgumentNode.js.map +1 -0
  64. package/utilities/internal/createFragmentSpreadNode.d.ts +5 -0
  65. package/utilities/internal/createFragmentSpreadNode.js +14 -0
  66. package/utilities/internal/createFragmentSpreadNode.js.map +1 -0
  67. package/utilities/internal/createNameNode.d.ts +3 -0
  68. package/utilities/internal/createNameNode.js +5 -0
  69. package/utilities/internal/createNameNode.js.map +1 -0
  70. package/utilities/internal/createVariableNode.d.ts +3 -0
  71. package/utilities/internal/createVariableNode.js +6 -0
  72. package/utilities/internal/createVariableNode.js.map +1 -0
  73. package/utilities/internal/equalByQuery.js +14 -1
  74. package/utilities/internal/equalByQuery.js.map +1 -1
  75. package/utilities/internal/getDefaultValues.d.ts +2 -2
  76. package/utilities/internal/getDefaultValues.js.map +1 -1
  77. package/utilities/internal/getFragmentQueryDocument.js +10 -7
  78. package/utilities/internal/getFragmentQueryDocument.js.map +1 -1
  79. package/utilities/internal/getFragmentVariables.d.ts +8 -0
  80. package/utilities/internal/getFragmentVariables.js +31 -0
  81. package/utilities/internal/getFragmentVariables.js.map +1 -0
  82. package/utilities/internal/index.d.ts +1 -0
  83. package/utilities/internal/index.js +1 -0
  84. package/utilities/internal/index.js.map +1 -1
  85. package/utilities/internal/removeDirectivesFromDocument.js +120 -61
  86. package/utilities/internal/removeDirectivesFromDocument.js.map +1 -1
  87. package/utilities/internal/types/Mutable.d.ts +4 -0
  88. package/utilities/internal/types/Mutable.js +2 -0
  89. package/utilities/internal/types/Mutable.js.map +1 -0
  90. package/version.js +1 -1
  91. package/version.js.map +1 -1
@@ -122,6 +122,7 @@ export interface MergeInfo {
122
122
  typename: string | undefined;
123
123
  merge: FieldMergeFunction;
124
124
  path: Array<string | number>;
125
+ variables: OperationVariables | undefined;
125
126
  }
126
127
  export interface MergeTree {
127
128
  info?: MergeInfo;
@@ -133,6 +134,7 @@ export interface ReadMergeModifyContext {
133
134
  varString?: string;
134
135
  extensions?: ExtensionsWithStreamInfo;
135
136
  isDeferPending?: (path: Incremental.Path, label: string | undefined) => boolean;
137
+ operationVariables?: OperationVariables;
136
138
  }
137
139
  type ListOf<T extends string> = `[${T}]`;
138
140
  type NestedScalarName<T extends string> = T | ListOf<T> | ListOf<ListOf<T>> | ListOf<ListOf<ListOf<T>>> | ListOf<ListOf<ListOf<ListOf<T>>>>;
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/cache/inmemory/types.ts"],"names":[],"mappings":"","sourcesContent":["import type { DocumentNode, FieldNode } from \"graphql\";\n\nimport type { OperationVariables } from \"@apollo/client\";\nimport type { Incremental } from \"@apollo/client/incremental\";\nimport type {\n Reference,\n StoreObject,\n StoreValue,\n} from \"@apollo/client/utilities\";\nimport type {\n ExtensionsWithStreamInfo,\n handleIncrementalSymbol,\n RemoveIndexSignature,\n StreamInfoTrie,\n} from \"@apollo/client/utilities/internal\";\n\nimport type { ApolloCache, Transaction } from \"../core/cache.js\";\nimport type {\n AllFieldsModifier,\n CanReadFunction,\n Modifiers,\n ToReferenceFunction,\n} from \"../core/types/common.js\";\n\nimport type { FieldValueGetter } from \"./entityStore.js\";\nimport type { FragmentRegistryAPI } from \"./fragmentRegistry.js\";\nimport type { InMemoryCache } from \"./inMemoryCache.js\";\nimport type {\n FieldMergeFunction,\n KeyFieldsFunction,\n PossibleTypesMap,\n StorageType,\n TypePolicies,\n} from \"./policies.js\";\n\nexport type { Reference, StoreObject, StoreValue };\n\nexport interface IdGetterObj extends Object {\n __typename?: string;\n id?: string;\n _id?: string;\n}\n\nexport declare type IdGetter = (value: IdGetterObj) => string | undefined;\n\n/**\n * This is an interface used to access, set and remove\n * StoreObjects from the cache\n */\nexport interface NormalizedCache {\n has(dataId: string): boolean;\n get(dataId: string, fieldName: string): StoreValue;\n\n // The store.merge method allows either argument to be a string ID, but\n // the other argument has to be a StoreObject. Either way, newer fields\n // always take precedence over older fields.\n merge(olderId: string, newerObject: StoreObject): void;\n merge(olderObject: StoreObject, newerId: string): void;\n\n modify<Entity extends Record<string, any>>(\n dataId: string,\n fields: Modifiers<Entity> | AllFieldsModifier<Entity>,\n exact: boolean\n ): boolean;\n delete(dataId: string, fieldName?: string): boolean;\n clear(): void;\n\n // non-Map elements:\n /**\n * returns an Object with key-value pairs matching the contents of the store\n */\n toObject(): NormalizedCacheObject;\n /**\n * replace the state of the store\n */\n replace(newData: NormalizedCacheObject): void;\n\n /**\n * Retain (or release) a given root ID to protect (or expose) it and its\n * transitive child entities from (or to) garbage collection. The current\n * retainment count is returned by both methods. Note that releasing a root\n * ID does not cause that entity to be garbage collected, but merely removes\n * it from the set of root IDs that will be considered during the next\n * mark-and-sweep collection.\n */\n retain(rootId: string): number;\n release(rootId: string): number;\n\n getFieldValue: FieldValueGetter;\n toReference: ToReferenceFunction;\n canRead: CanReadFunction;\n\n getStorage(\n idOrObj: string | StoreObject,\n ...storeFieldNames: (string | number)[]\n ): StorageType;\n}\n\n/**\n * This is a normalized representation of the Apollo query result cache. It consists of\n * a flattened representation of query result trees.\n */\nexport interface NormalizedCacheObject {\n __META?: {\n // Well-known singleton IDs like ROOT_QUERY and ROOT_MUTATION are\n // always considered to be root IDs during cache.gc garbage\n // collection, but other IDs can become roots if they are written\n // directly with cache.writeFragment or retained explicitly with\n // cache.retain. When such IDs exist, we include them in the __META\n // section so that they can survive cache.{extract,restore}.\n extraRootIds: string[];\n };\n [dataId: string]: StoreObject | undefined;\n}\n\nexport type OptimisticStoreItem = {\n id: string;\n data: NormalizedCacheObject;\n transaction: Transaction;\n};\n\nexport type ReadQueryOptions = {\n /**\n * The Apollo Client store object.\n */\n store: NormalizedCache;\n /**\n * A parsed GraphQL query document.\n */\n query: DocumentNode;\n variables?: Object;\n previousResult?: any;\n rootId?: string;\n config?: ApolloReducerConfig;\n};\n\n/** @internal */\nexport interface DiffIncrementalInfo {\n streamInfo?: StreamInfoTrie;\n isDeferPending?: (\n path: Incremental.Path,\n label: string | undefined\n ) => boolean;\n}\n\nexport interface WithHandleIncremental {\n [handleIncrementalSymbol]?: DiffIncrementalInfo | undefined;\n}\n\nexport type DiffQueryAgainstStoreOptions = ReadQueryOptions & {\n returnPartialData?: boolean;\n};\n\nexport type ApolloReducerConfig = {\n dataIdFromObject?: KeyFieldsFunction;\n};\n\nexport interface InputObjectConfig {\n fields: Record<string, string>;\n}\n\nexport interface InputObjectsOption {\n [inputObjectName: string]: InputObjectConfig;\n}\n\nexport type InMemoryCacheConfig = ApolloReducerConfig & {\n resultCaching?: boolean;\n possibleTypes?: PossibleTypesMap;\n typePolicies?: TypePolicies;\n fragments?: FragmentRegistryAPI;\n inputObjects?: InputObjectsOption;\n} & ({} extends InMemoryCache.ScalarsOption ?\n InMemoryCache.ScalarsOption extends Record<string, never> ?\n {\n scalars?: Record<\n string,\n `Scalar types must be declared in ApolloCache.Scalars before usage. See https://www.apollographql.com/docs/react/data/typescript#declaring-scalar-types.`\n >;\n }\n : { scalars?: InMemoryCache.ScalarsOption }\n : { scalars: InMemoryCache.ScalarsOption });\n\nexport interface MergeInfo {\n field: FieldNode;\n typename: string | undefined;\n merge: FieldMergeFunction;\n path: Array<string | number>;\n}\n\nexport interface MergeTree {\n info?: MergeInfo;\n map: Map<string | number, MergeTree>;\n}\n\nexport interface ReadMergeModifyContext {\n store: NormalizedCache;\n variables?: OperationVariables;\n // A JSON.stringify-serialized version of context.variables.\n varString?: string;\n extensions?: ExtensionsWithStreamInfo;\n isDeferPending?: (\n path: Incremental.Path,\n label: string | undefined\n ) => boolean;\n}\n\ntype ListOf<T extends string> = `[${T}]`;\n\ntype NestedScalarName<T extends string> =\n | T\n | ListOf<T>\n | ListOf<ListOf<T>>\n | ListOf<ListOf<ListOf<T>>>\n | ListOf<ListOf<ListOf<ListOf<T>>>>;\n\nexport type KnownScalars = RemoveIndexSignature<ApolloCache.Scalars>;\n\nexport type ScalarType =\n | NestedScalarName<keyof KnownScalars & string>\n | (string extends keyof ApolloCache.Scalars ? string & {} : never);\n"]}
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/cache/inmemory/types.ts"],"names":[],"mappings":"","sourcesContent":["import type { DocumentNode, FieldNode } from \"graphql\";\n\nimport type { OperationVariables } from \"@apollo/client\";\nimport type { Incremental } from \"@apollo/client/incremental\";\nimport type {\n Reference,\n StoreObject,\n StoreValue,\n} from \"@apollo/client/utilities\";\nimport type {\n ExtensionsWithStreamInfo,\n handleIncrementalSymbol,\n RemoveIndexSignature,\n StreamInfoTrie,\n} from \"@apollo/client/utilities/internal\";\n\nimport type { ApolloCache, Transaction } from \"../core/cache.js\";\nimport type {\n AllFieldsModifier,\n CanReadFunction,\n Modifiers,\n ToReferenceFunction,\n} from \"../core/types/common.js\";\n\nimport type { FieldValueGetter } from \"./entityStore.js\";\nimport type { FragmentRegistryAPI } from \"./fragmentRegistry.js\";\nimport type { InMemoryCache } from \"./inMemoryCache.js\";\nimport type {\n FieldMergeFunction,\n KeyFieldsFunction,\n PossibleTypesMap,\n StorageType,\n TypePolicies,\n} from \"./policies.js\";\n\nexport type { Reference, StoreObject, StoreValue };\n\nexport interface IdGetterObj extends Object {\n __typename?: string;\n id?: string;\n _id?: string;\n}\n\nexport declare type IdGetter = (value: IdGetterObj) => string | undefined;\n\n/**\n * This is an interface used to access, set and remove\n * StoreObjects from the cache\n */\nexport interface NormalizedCache {\n has(dataId: string): boolean;\n get(dataId: string, fieldName: string): StoreValue;\n\n // The store.merge method allows either argument to be a string ID, but\n // the other argument has to be a StoreObject. Either way, newer fields\n // always take precedence over older fields.\n merge(olderId: string, newerObject: StoreObject): void;\n merge(olderObject: StoreObject, newerId: string): void;\n\n modify<Entity extends Record<string, any>>(\n dataId: string,\n fields: Modifiers<Entity> | AllFieldsModifier<Entity>,\n exact: boolean\n ): boolean;\n delete(dataId: string, fieldName?: string): boolean;\n clear(): void;\n\n // non-Map elements:\n /**\n * returns an Object with key-value pairs matching the contents of the store\n */\n toObject(): NormalizedCacheObject;\n /**\n * replace the state of the store\n */\n replace(newData: NormalizedCacheObject): void;\n\n /**\n * Retain (or release) a given root ID to protect (or expose) it and its\n * transitive child entities from (or to) garbage collection. The current\n * retainment count is returned by both methods. Note that releasing a root\n * ID does not cause that entity to be garbage collected, but merely removes\n * it from the set of root IDs that will be considered during the next\n * mark-and-sweep collection.\n */\n retain(rootId: string): number;\n release(rootId: string): number;\n\n getFieldValue: FieldValueGetter;\n toReference: ToReferenceFunction;\n canRead: CanReadFunction;\n\n getStorage(\n idOrObj: string | StoreObject,\n ...storeFieldNames: (string | number)[]\n ): StorageType;\n}\n\n/**\n * This is a normalized representation of the Apollo query result cache. It consists of\n * a flattened representation of query result trees.\n */\nexport interface NormalizedCacheObject {\n __META?: {\n // Well-known singleton IDs like ROOT_QUERY and ROOT_MUTATION are\n // always considered to be root IDs during cache.gc garbage\n // collection, but other IDs can become roots if they are written\n // directly with cache.writeFragment or retained explicitly with\n // cache.retain. When such IDs exist, we include them in the __META\n // section so that they can survive cache.{extract,restore}.\n extraRootIds: string[];\n };\n [dataId: string]: StoreObject | undefined;\n}\n\nexport type OptimisticStoreItem = {\n id: string;\n data: NormalizedCacheObject;\n transaction: Transaction;\n};\n\nexport type ReadQueryOptions = {\n /**\n * The Apollo Client store object.\n */\n store: NormalizedCache;\n /**\n * A parsed GraphQL query document.\n */\n query: DocumentNode;\n variables?: Object;\n previousResult?: any;\n rootId?: string;\n config?: ApolloReducerConfig;\n};\n\n/** @internal */\nexport interface DiffIncrementalInfo {\n streamInfo?: StreamInfoTrie;\n isDeferPending?: (\n path: Incremental.Path,\n label: string | undefined\n ) => boolean;\n}\n\nexport interface WithHandleIncremental {\n [handleIncrementalSymbol]?: DiffIncrementalInfo | undefined;\n}\n\nexport type DiffQueryAgainstStoreOptions = ReadQueryOptions & {\n returnPartialData?: boolean;\n};\n\nexport type ApolloReducerConfig = {\n dataIdFromObject?: KeyFieldsFunction;\n};\n\nexport interface InputObjectConfig {\n fields: Record<string, string>;\n}\n\nexport interface InputObjectsOption {\n [inputObjectName: string]: InputObjectConfig;\n}\n\nexport type InMemoryCacheConfig = ApolloReducerConfig & {\n resultCaching?: boolean;\n possibleTypes?: PossibleTypesMap;\n typePolicies?: TypePolicies;\n fragments?: FragmentRegistryAPI;\n inputObjects?: InputObjectsOption;\n} & ({} extends InMemoryCache.ScalarsOption ?\n InMemoryCache.ScalarsOption extends Record<string, never> ?\n {\n scalars?: Record<\n string,\n `Scalar types must be declared in ApolloCache.Scalars before usage. See https://www.apollographql.com/docs/react/data/typescript#declaring-scalar-types.`\n >;\n }\n : { scalars?: InMemoryCache.ScalarsOption }\n : { scalars: InMemoryCache.ScalarsOption });\n\nexport interface MergeInfo {\n field: FieldNode;\n typename: string | undefined;\n merge: FieldMergeFunction;\n path: Array<string | number>;\n variables: OperationVariables | undefined;\n}\n\nexport interface MergeTree {\n info?: MergeInfo;\n map: Map<string | number, MergeTree>;\n}\n\nexport interface ReadMergeModifyContext {\n store: NormalizedCache;\n variables?: OperationVariables;\n // A JSON.stringify-serialized version of context.variables.\n varString?: string;\n extensions?: ExtensionsWithStreamInfo;\n isDeferPending?: (\n path: Incremental.Path,\n label: string | undefined\n ) => boolean;\n // Keep a copy of the operation variables. variables reflects the current\n // scoped variables in fragments that have arguments\n operationVariables?: OperationVariables;\n}\n\ntype ListOf<T extends string> = `[${T}]`;\n\ntype NestedScalarName<T extends string> =\n | T\n | ListOf<T>\n | ListOf<ListOf<T>>\n | ListOf<ListOf<ListOf<T>>>\n | ListOf<ListOf<ListOf<ListOf<T>>>>;\n\nexport type KnownScalars = RemoveIndexSignature<ApolloCache.Scalars>;\n\nexport type ScalarType =\n | NestedScalarName<keyof KnownScalars & string>\n | (string extends keyof ApolloCache.Scalars ? string & {} : never);\n"]}
@@ -8,7 +8,10 @@ import type { StoreReader } from "./readFromStore.js";
8
8
  import type { DiffIncrementalInfo, InMemoryCacheConfig, MergeTree, NormalizedCache, ReadMergeModifyContext } from "./types.js";
9
9
  export interface WriteContext extends ReadMergeModifyContext {
10
10
  readonly written: {
11
- [dataId: string]: SelectionSetNode[];
11
+ [dataId: string]: Array<{
12
+ selectionSet: SelectionSetNode;
13
+ varString: string | undefined;
14
+ }>;
12
15
  };
13
16
  readonly fragmentMap: FragmentMap;
14
17
  lookupFragment: FragmentMapFunction;
@@ -3,15 +3,16 @@ import { Trie } from "@wry/trie";
3
3
  import { Kind } from "graphql";
4
4
  import { addTypenameToDocument, canonicalStringify, isReference, } from "@apollo/client/utilities";
5
5
  import { __DEV__ } from "@apollo/client/utilities/environment";
6
- import { argumentsObjectFromField, cloneDeep, getDefaultValues, getFragmentFromSelection, getOperationDefinition, handleIncrementalSymbol, isArray, isField, isNonEmptyArray, isStreamField, makeReference, resultKeyNameFromField, shouldInclude, streamInfoSymbol, } from "@apollo/client/utilities/internal";
6
+ import { argumentsObjectFromField, cloneDeep, getDefaultValues, getFragmentFromSelection, getFragmentVariables, getOperationDefinition, handleIncrementalSymbol, isArray, isField, isNonEmptyArray, isStreamField, makeReference, resultKeyNameFromField, shouldInclude, streamInfoSymbol, } from "@apollo/client/utilities/internal";
7
7
  import { invariant, newInvariantError, } from "@apollo/client/utilities/invariant";
8
8
  import { extractFragmentContext, fieldNameFromStoreName, makeProcessedFieldsMerger, storeValueIsStoreObject, } from "./helpers.js";
9
9
  import { defaultStreamFieldMergeFn, normalizeReadFieldOptions, } from "./policies.js";
10
10
  // Since there are only four possible combinations of context.clientOnly and
11
11
  // context.deferred values, we should need at most four "flavors" of any given
12
- // WriteContext. To avoid creating multiple copies of the same context, we cache
13
- // the contexts in the context.flavors Map (shared by all flavors) according to
14
- // their clientOnly and deferred values (always in that order).
12
+ // WriteContext (per fragment argument scope). To avoid creating multiple copies
13
+ // of the same context, we cache the contexts in the context.flavors Map (shared
14
+ // by all flavors) according to their clientOnly and deferred values (always in
15
+ // that order).
15
16
  function getContextFlavor(context, clientOnly, deferred) {
16
17
  const key = `${clientOnly}${deferred}`;
17
18
  let flavored = context.flavors.get(key);
@@ -51,6 +52,7 @@ export class StoreWriter {
51
52
  },
52
53
  variables: variables,
53
54
  varString: canonicalStringify(variables),
55
+ operationVariables: variables,
54
56
  ...extractFragmentContext(query, this.fragments),
55
57
  overwrite: !!overwrite,
56
58
  incomingById: new Map(),
@@ -209,6 +211,7 @@ export class StoreWriter {
209
211
  typename,
210
212
  merge,
211
213
  path,
214
+ variables: context.variables,
212
215
  };
213
216
  }
214
217
  else if (context.extensions?.[streamInfoSymbol] &&
@@ -219,6 +222,7 @@ export class StoreWriter {
219
222
  typename,
220
223
  merge: defaultStreamFieldMergeFn,
221
224
  path,
225
+ variables: context.variables,
222
226
  };
223
227
  }
224
228
  else {
@@ -270,12 +274,17 @@ export class StoreWriter {
270
274
  // Avoid processing the same entity object using the same selection
271
275
  // set more than once. We use an array instead of a Set since most
272
276
  // entity IDs will be written using only one selection set, so the
273
- // size of this array is likely to be very small, meaning indexOf is
274
- // likely to be faster than Set.prototype.has.
277
+ // size of this array is likely to be very small, meaning a linear
278
+ // search is likely to be faster than a Map or Set lookup. The same
279
+ // selection set inside a fragment can be reached with different
280
+ // fragment arguments, so the variables are part of the check.
281
+ const { varString } = context;
275
282
  const sets = context.written[dataId] || (context.written[dataId] = []);
276
- if (sets.indexOf(selectionSet) >= 0)
283
+ if (sets.some((written) => written.selectionSet === selectionSet &&
284
+ written.varString === varString)) {
277
285
  return dataRef;
278
- sets.push(selectionSet);
286
+ }
287
+ sets.push({ selectionSet, varString });
279
288
  // If we're about to write a result object into the store, but we
280
289
  // happen to know that the exact same (===) result object would be
281
290
  // returned if we were to reread the result with the same inputs,
@@ -344,7 +353,7 @@ export class StoreWriter {
344
353
  return;
345
354
  visitedNode.visited = true;
346
355
  selectionSet.selections.forEach((selection) => {
347
- if (!shouldInclude(selection, context.variables))
356
+ if (!shouldInclude(selection, inheritedContext.variables))
348
357
  return;
349
358
  let { clientOnly, deferred } = inheritedContext;
350
359
  if (
@@ -358,7 +367,7 @@ export class StoreWriter {
358
367
  if (name === "client")
359
368
  clientOnly = true;
360
369
  if (name === "defer") {
361
- const args = argumentsObjectFromField(dir, context.variables);
370
+ const args = argumentsObjectFromField(dir, inheritedContext.variables);
362
371
  // The @defer directive takes an optional args.if boolean
363
372
  // argument, similar to @include(if: boolean). Note that
364
373
  // @defer(if: false) does not make context.deferred false, but
@@ -389,16 +398,34 @@ export class StoreWriter {
389
398
  clientOnly = clientOnly && existing.clientOnly;
390
399
  deferred = deferred && existing.deferred;
391
400
  }
392
- fieldMap.set(selection, getContextFlavor(context, clientOnly, deferred));
401
+ fieldMap.set(selection, getContextFlavor(inheritedContext, clientOnly, deferred));
393
402
  }
394
403
  else {
395
404
  const fragment = getFragmentFromSelection(selection, context.lookupFragment);
396
405
  if (!fragment && selection.kind === Kind.FRAGMENT_SPREAD) {
397
406
  throw newInvariantError(126, selection.name.value);
398
407
  }
399
- if (fragment &&
400
- policies.fragmentMatches(fragment, typename, result, context.variables)) {
401
- flatten(fragment.selectionSet, getContextFlavor(context, clientOnly, deferred));
408
+ if (!fragment)
409
+ return;
410
+ const variables = (selection.kind === Kind.FRAGMENT_SPREAD &&
411
+ fragment.kind === Kind.FRAGMENT_DEFINITION) ?
412
+ getFragmentVariables(selection, {
413
+ fragment,
414
+ variables: context.operationVariables,
415
+ parentScope: inheritedContext.variables,
416
+ })
417
+ : inheritedContext.variables;
418
+ if (policies.fragmentMatches(fragment, typename, result, variables)) {
419
+ let fragmentContext = getContextFlavor(inheritedContext, clientOnly, deferred);
420
+ if (variables !== inheritedContext.variables) {
421
+ fragmentContext = {
422
+ ...fragmentContext,
423
+ variables,
424
+ varString: canonicalStringify(variables),
425
+ flavors: new Map(),
426
+ };
427
+ }
428
+ flatten(fragment.selectionSet, fragmentContext);
402
429
  }
403
430
  }
404
431
  });