@apollo/client 3.4.6 → 3.4.7
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/apollo-client.cjs.js +4 -4
- package/apollo-client.cjs.js.map +1 -1
- package/apollo-core.cjs.js +4 -4
- package/apollo-core.cjs.js.map +1 -1
- package/apollo-core.cjs.min.js +1 -1
- package/cache/cache.cjs.js +3 -3
- package/cache/cache.cjs.js.map +1 -1
- package/cache/inmemory/writeToStore.d.ts +1 -1
- package/cache/inmemory/writeToStore.d.ts.map +1 -1
- package/cache/inmemory/writeToStore.js +3 -3
- package/cache/inmemory/writeToStore.js.map +1 -1
- package/core/core.cjs.js +1 -1
- package/core/core.cjs.js.map +1 -1
- package/invariantErrorCodes.js +1 -1
- package/package.json +1 -1
- package/version.js +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"writeToStore.d.ts","sourceRoot":"","sources":["../../../src/cache/inmemory/writeToStore.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAa,aAAa,EAAE,MAAM,SAAS,CAAC;AAIrE,OAAO,EAEL,WAAW,EAUX,WAAW,EACX,SAAS,EAKV,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EAAE,eAAe,EAAE,sBAAsB,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAE7E,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAC9C,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAEhD,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAGnC,MAAM,WAAW,YAAa,SAAQ,sBAAsB;IAC1D,QAAQ,CAAC,OAAO,EAAE;QAChB,CAAC,MAAM,EAAE,MAAM,GAAG,gBAAgB,EAAE,CAAC;KACtC,CAAC;IACF,QAAQ,CAAC,WAAW,CAAC,EAAE,WAAW,CAAC;IAEnC,KAAK,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,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AAC;gBADC,KAAK,EAAE,aAAa,EAC5B,MAAM,CAAC,yBAAa;IAGvB,YAAY,CAAC,KAAK,EAAE,eAAe,EAAE,EAC1C,KAAK,EACL,MAAM,EACN,MAAM,EACN,SAAS,EACT,SAAS,GACV,EAAE,KAAK,CAAC,YAAY,GAAG,SAAS,GAAG,SAAS;IAkG7C,OAAO,CAAC,mBAAmB;IAiO3B,OAAO,CAAC,iBAAiB;IA8BzB,OAAO,CAAC,WAAW;CA6FpB"}
|
|
@@ -42,7 +42,7 @@ var StoreWriter = (function () {
|
|
|
42
42
|
context.incomingById.forEach(function (_a, dataId) {
|
|
43
43
|
var fields = _a.fields, mergeTree = _a.mergeTree, selections = _a.selections;
|
|
44
44
|
var entityRef = makeReference(dataId);
|
|
45
|
-
if (mergeTree.map.size) {
|
|
45
|
+
if (mergeTree && mergeTree.map.size) {
|
|
46
46
|
var applied = _this.applyMerges(mergeTree, entityRef, fields, context);
|
|
47
47
|
if (isReference(applied)) {
|
|
48
48
|
return;
|
|
@@ -60,7 +60,7 @@ var StoreWriter = (function () {
|
|
|
60
60
|
}
|
|
61
61
|
});
|
|
62
62
|
var hasMergeFunction_1 = function (storeFieldName) {
|
|
63
|
-
var childTree = mergeTree.map.get(storeFieldName);
|
|
63
|
+
var childTree = mergeTree && mergeTree.map.get(storeFieldName);
|
|
64
64
|
return Boolean(childTree && childTree.info && childTree.info.merge);
|
|
65
65
|
};
|
|
66
66
|
Object.keys(fields).forEach(function (storeFieldName) {
|
|
@@ -169,7 +169,7 @@ var StoreWriter = (function () {
|
|
|
169
169
|
else {
|
|
170
170
|
context.incomingById.set(dataId, {
|
|
171
171
|
fields: incomingFields,
|
|
172
|
-
mergeTree: mergeTree,
|
|
172
|
+
mergeTree: mergeTreeIsEmpty(mergeTree) ? void 0 : mergeTree,
|
|
173
173
|
selections: selections,
|
|
174
174
|
});
|
|
175
175
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"writeToStore.js","sourceRoot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,CAAC;QACxB,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE;QAC5B,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC,OAAO,CAAC,UAAA,KAAK;YAChC,IAAM,QAAQ,GAAG,KAAK,CAAC,aAAa,CAAC,KAAK,EAAE,YAAY,CAAC,CAAC;YAC1D,IAAI,OAAO,QAAQ,KAAK,QAAQ;gBAC5B,CAAC,cAAc,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE;gBACtC,cAAc,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;aAC/B;QACH,CAAC,CAAC,CAAC;KACJ;IAED,SAAS,CAAC,IAAI,CAChB,+CAA6C,SAAS,oBAAe,UAAU,oFAG7E,cAAc,CAAC,MAAM;QACnB,CAAC,CAAC,oCAAoC;YAClC,cAAc,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,6CAA6C;QAChF,CAAC,CAAC,EAAE,gDAEN,WAAW,gFAGC,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,IAAI,CAAC,sBACvC,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,IAAI,CAAC,mRAMpD,CAAC,CAAC;AACH,CAAC","sourcesContent":["import { SelectionSetNode, FieldNode, SelectionNode } from 'graphql';\nimport { invariant, InvariantError } from 'ts-invariant';\nimport { equal } from '@wry/equality';\n\nimport {\n createFragmentMap,\n FragmentMap,\n getFragmentFromSelection,\n getDefaultValues,\n getFragmentDefinitions,\n getOperationDefinition,\n getTypenameFromResult,\n makeReference,\n isField,\n resultKeyNameFromField,\n StoreValue,\n StoreObject,\n Reference,\n isReference,\n shouldInclude,\n cloneDeep,\n addTypenameToDocument,\n} from '../../utilities';\n\nimport { NormalizedCache, ReadMergeModifyContext, MergeTree } from './types';\nimport { makeProcessedFieldsMerger, fieldNameFromStoreName, storeValueIsStoreObject } from './helpers';\nimport { StoreReader } from './readFromStore';\nimport { InMemoryCache } from './inMemoryCache';\nimport { EntityStore } from './entityStore';\nimport { Cache } from '../../core';\nimport { canonicalStringify } from './object-canon';\n\nexport interface WriteContext extends ReadMergeModifyContext {\n readonly written: {\n [dataId: string]: SelectionSetNode[];\n };\n readonly fragmentMap?: FragmentMap;\n // General-purpose deep-merge function for use during writes.\n merge<T>(existing: T, incoming: T): T;\n // If true, merge functions will be called with undefined existing data.\n overwrite: boolean;\n incomingById: Map<string, {\n fields: StoreObject;\n mergeTree: MergeTree;\n selections: Set<SelectionNode>;\n }>;\n clientOnly: boolean;\n};\n\ninterface ProcessSelectionSetOptions {\n dataId?: string,\n result: Record<string, any>;\n selectionSet: SelectionSetNode;\n context: WriteContext;\n mergeTree: MergeTree;\n}\n\nexport class StoreWriter {\n constructor(\n public readonly cache: InMemoryCache,\n private reader?: StoreReader,\n ) {}\n\n public writeToStore(store: NormalizedCache, {\n query,\n result,\n dataId,\n variables,\n overwrite,\n }: Cache.WriteOptions): Reference | undefined {\n const operationDefinition = getOperationDefinition(query)!;\n const merger = makeProcessedFieldsMerger();\n\n variables = {\n ...getDefaultValues(operationDefinition),\n ...variables!,\n };\n\n const context: WriteContext = {\n store,\n written: Object.create(null),\n merge<T>(existing: T, incoming: T) {\n return merger.merge(existing, incoming) as T;\n },\n variables,\n varString: canonicalStringify(variables),\n fragmentMap: createFragmentMap(getFragmentDefinitions(query)),\n overwrite: !!overwrite,\n incomingById: new Map,\n clientOnly: false,\n };\n\n const ref = this.processSelectionSet({\n result: result || Object.create(null),\n dataId,\n selectionSet: operationDefinition.selectionSet,\n mergeTree: { map: new Map },\n context,\n });\n\n if (!isReference(ref)) {\n throw new InvariantError(`Could not identify object ${JSON.stringify(result)}`);\n }\n\n // So far, the store has not been modified, so now it's time to process\n // context.incomingById and merge those incoming fields into context.store.\n context.incomingById.forEach(({ fields, mergeTree, selections }, dataId) => {\n const entityRef = makeReference(dataId);\n\n if (mergeTree.map.size) {\n const applied = this.applyMerges(mergeTree, entityRef, fields, context);\n if (isReference(applied)) {\n // Assume References returned by applyMerges have already been merged\n // into the store. See makeMergeObjectsFunction in policies.ts for an\n // example of how this can happen.\n return;\n }\n // Otherwise, applyMerges returned a StoreObject, whose fields we should\n // merge into the store (see store.merge statement below).\n fields = applied;\n }\n\n if (__DEV__ && !context.overwrite) {\n const hasSelectionSet = (storeFieldName: string) =>\n fieldsWithSelectionSets.has(fieldNameFromStoreName(storeFieldName));\n const fieldsWithSelectionSets = new Set<string>();\n selections.forEach(selection => {\n if (isField(selection) && selection.selectionSet) {\n fieldsWithSelectionSets.add(selection.name.value);\n }\n });\n\n const hasMergeFunction = (storeFieldName: string) => {\n const childTree = mergeTree.map.get(storeFieldName);\n return Boolean(childTree && childTree.info && childTree.info.merge);\n };\n\n Object.keys(fields).forEach(storeFieldName => {\n // If a merge function was defined for this field, trust that it\n // did the right thing about (not) clobbering data. If the field\n // has no selection set, it's a scalar field, so it doesn't need\n // a merge function (even if it's an object, like JSON data).\n if (hasSelectionSet(storeFieldName) &&\n !hasMergeFunction(storeFieldName)) {\n warnAboutDataLoss(\n entityRef,\n fields,\n storeFieldName,\n context.store,\n );\n }\n });\n }\n\n store.merge(dataId, fields);\n });\n\n // Any IDs written explicitly to the cache will be retained as\n // reachable root IDs for garbage collection purposes. Although this\n // logic includes root IDs like ROOT_QUERY and ROOT_MUTATION, their\n // retainment counts are effectively ignored because cache.gc() always\n // includes them in its root ID set.\n store.retain(ref.__ref);\n\n return ref;\n }\n\n private processSelectionSet({\n dataId,\n result,\n selectionSet,\n context,\n // This object allows processSelectionSet to report useful information\n // to its callers without explicitly returning that information.\n mergeTree,\n }: ProcessSelectionSetOptions): StoreObject | Reference {\n const { policies } = this.cache;\n\n // Identify the result object, even if dataId was already provided,\n // since we always need keyObject below.\n const [id, keyObject] = policies.identify(\n result, selectionSet, context.fragmentMap);\n\n // If dataId was not provided, fall back to the id just generated by\n // policies.identify.\n dataId = dataId || id;\n\n if (\"string\" === typeof dataId) {\n // Avoid processing the same entity object using the same selection\n // set more than once. We use an array instead of a Set since most\n // entity IDs will be written using only one selection set, so the\n // size of this array is likely to be very small, meaning indexOf is\n // likely to be faster than Set.prototype.has.\n const sets = context.written[dataId] || (context.written[dataId] = []);\n const ref = makeReference(dataId);\n if (sets.indexOf(selectionSet) >= 0) return ref;\n sets.push(selectionSet);\n\n // If we're about to write a result object into the store, but we\n // happen to know that the exact same (===) result object would be\n // returned if we were to reread the result with the same inputs,\n // then we can skip the rest of the processSelectionSet work for\n // this object, and immediately return a Reference to it.\n if (this.reader && this.reader.isFresh(\n result,\n ref,\n selectionSet,\n context,\n )) {\n return ref;\n }\n }\n\n // This variable will be repeatedly updated using context.merge to\n // accumulate all fields that need to be written into the store.\n let incomingFields: StoreObject = Object.create(null);\n\n // Write any key fields that were used during identification, even if\n // they were not mentioned in the original query.\n if (keyObject) {\n incomingFields = context.merge(incomingFields, keyObject);\n }\n\n // If typename was not passed in, infer it. Note that typename is\n // always passed in for tricky-to-infer cases such as \"Query\" for\n // ROOT_QUERY.\n const typename: string | undefined =\n (dataId && policies.rootTypenamesById[dataId]) ||\n getTypenameFromResult(result, selectionSet, context.fragmentMap) ||\n (dataId && context.store.get(dataId, \"__typename\") as string);\n\n if (\"string\" === typeof typename) {\n incomingFields.__typename = typename;\n }\n\n const selections = new Set(selectionSet.selections);\n\n selections.forEach(selection => {\n if (!shouldInclude(selection, context.variables)) return;\n\n if (isField(selection)) {\n const resultFieldKey = resultKeyNameFromField(selection);\n const value = result[resultFieldKey];\n\n const wasClientOnly = context.clientOnly;\n context.clientOnly = wasClientOnly || !!(\n selection.directives &&\n selection.directives.some(d => d.name.value === \"client\")\n );\n\n if (value !== void 0) {\n const storeFieldName = policies.getStoreFieldName({\n typename,\n fieldName: selection.name.value,\n field: selection,\n variables: context.variables,\n });\n\n const childTree = getChildMergeTree(mergeTree, storeFieldName);\n\n let incomingValue =\n this.processFieldValue(value, selection, context, childTree);\n\n // To determine if this field holds a child object with a merge\n // function defined in its type policy (see PR #7070), we need to\n // figure out the child object's __typename.\n let childTypename: string | undefined;\n\n // The field's value can be an object that has a __typename only if\n // the field has a selection set. Otherwise incomingValue is scalar.\n if (selection.selectionSet) {\n // We attempt to find the child __typename first in context.store,\n // but the child object may not exist in the store yet, likely\n // because it's being written for the first time, during this very\n // call to writeToStore. Note: if incomingValue is a non-normalized\n // StoreObject (not a Reference), getFieldValue will read from that\n // object's properties to find its __typename.\n childTypename = context.store.getFieldValue<string>(\n incomingValue as StoreObject | Reference,\n \"__typename\",\n );\n\n // If the child object is being written for the first time, but\n // incomingValue is a Reference, then the entity that Reference\n // identifies should have an entry in context.incomingById, which\n // likely contains a __typename field we can use. After all, how\n // could we know the object's ID if it had no __typename? If we\n // wrote data into context.store as each processSelectionSet call\n // finished processing an entity object, the child object would\n // already be in context.store, so we wouldn't need this extra\n // check, but holding all context.store.merge calls until after\n // we've finished all processSelectionSet work is cleaner and solves\n // other problems, such as issue #8370.\n if (!childTypename && isReference(incomingValue)) {\n const info = context.incomingById.get(incomingValue.__ref);\n childTypename = info && info.fields.__typename;\n }\n }\n\n const merge = policies.getMergeFunction(\n typename,\n selection.name.value,\n childTypename,\n );\n\n if (merge) {\n childTree.info = {\n // TODO Check compatibility against any existing\n // childTree.field?\n field: selection,\n typename,\n merge,\n };\n } else {\n maybeRecycleChildMergeTree(mergeTree, storeFieldName);\n }\n\n incomingFields = context.merge(incomingFields, {\n [storeFieldName]: incomingValue,\n });\n\n } else if (\n !context.clientOnly &&\n !addTypenameToDocument.added(selection)\n ) {\n invariant.error(`Missing field '${\n resultKeyNameFromField(selection)\n }' while writing result ${\n JSON.stringify(result, null, 2)\n }`.substring(0, 1000));\n }\n\n context.clientOnly = wasClientOnly;\n\n } else {\n // This is not a field, so it must be a fragment, either inline or named\n const fragment = getFragmentFromSelection(\n selection,\n context.fragmentMap,\n );\n\n if (fragment &&\n // By passing result and context.variables, we enable\n // policies.fragmentMatches to bend the rules when typename is\n // not a known subtype of the fragment type condition, but the\n // result object contains all the keys requested by the\n // fragment, which strongly suggests the fragment probably\n // matched. This fuzzy matching behavior must be enabled by\n // including a regular expression string (such as \".*\" or\n // \"Prefix.*\" or \".*Suffix\") in the possibleTypes array for\n // specific supertypes; otherwise, all matching remains exact.\n // Fuzzy matches are remembered by the Policies object and\n // later used when reading from the cache. Since there is no\n // incoming result object to check when reading, reading does\n // not involve the same fuzzy inference, so the StoreReader\n // class calls policies.fragmentMatches without passing result\n // or context.variables. The flexibility of fuzzy matching\n // allows existing clients to accommodate previously unknown\n // __typename strings produced by server/schema changes, which\n // would otherwise be breaking changes.\n policies.fragmentMatches(fragment, typename, result, context.variables)) {\n fragment.selectionSet.selections.forEach(selections.add, selections);\n }\n }\n });\n\n if (\"string\" === typeof dataId) {\n const previous = context.incomingById.get(dataId);\n if (previous) {\n previous.fields = context.merge(previous.fields, incomingFields);\n previous.mergeTree = mergeMergeTrees(previous.mergeTree, mergeTree);\n // Add all previous SelectionNode objects, rather than creating a new\n // Set, since the original unmerged selections Set is not going to be\n // needed again (only the merged Set).\n previous.selections.forEach(selections.add, selections);\n previous.selections = selections;\n } else {\n context.incomingById.set(dataId, {\n fields: incomingFields,\n mergeTree,\n selections,\n });\n }\n return makeReference(dataId);\n }\n\n return incomingFields;\n }\n\n private processFieldValue(\n value: any,\n field: FieldNode,\n context: WriteContext,\n mergeTree: MergeTree,\n ): StoreValue {\n if (!field.selectionSet || value === null) {\n // In development, we need to clone scalar values so that they can be\n // safely frozen with maybeDeepFreeze in readFromStore.ts. In production,\n // it's cheaper to store the scalar values directly in the cache.\n return __DEV__ ? cloneDeep(value) : value;\n }\n\n if (Array.isArray(value)) {\n return value.map((item, i) => {\n const value = this.processFieldValue(\n item, field, context, getChildMergeTree(mergeTree, i));\n maybeRecycleChildMergeTree(mergeTree, i);\n return value;\n });\n }\n\n return this.processSelectionSet({\n result: value,\n selectionSet: field.selectionSet,\n context,\n mergeTree,\n });\n }\n\n private applyMerges<T extends StoreValue>(\n mergeTree: MergeTree,\n existing: StoreValue,\n incoming: T,\n context: WriteContext,\n getStorageArgs?: Parameters<EntityStore[\"getStorage\"]>,\n ): T | Reference {\n if (mergeTree.map.size && !isReference(incoming)) {\n const e: StoreObject | Reference | undefined = (\n // Items in the same position in different arrays are not\n // necessarily related to each other, so when incoming is an array\n // we process its elements as if there was no existing data.\n !Array.isArray(incoming) &&\n // Likewise, existing must be either a Reference or a StoreObject\n // in order for its fields to be safe to merge with the fields of\n // the incoming object.\n (isReference(existing) || storeValueIsStoreObject(existing))\n ) ? existing : void 0;\n\n // This narrowing is implied by mergeTree.map.size > 0 and\n // !isReference(incoming), though TypeScript understandably cannot\n // hope to infer this type.\n const i = incoming as StoreObject | StoreValue[];\n\n // The options.storage objects provided to read and merge functions\n // are derived from the identity of the parent object plus a\n // sequence of storeFieldName strings/numbers identifying the nested\n // field name path of each field value to be merged.\n if (e && !getStorageArgs) {\n getStorageArgs = [isReference(e) ? e.__ref : e];\n }\n\n // It's possible that applying merge functions to this subtree will\n // not change the incoming data, so this variable tracks the fields\n // that did change, so we can create a new incoming object when (and\n // only when) at least one incoming field has changed. We use a Map\n // to preserve the type of numeric keys.\n let changedFields: Map<string | number, StoreValue> | undefined;\n\n const getValue = (\n from: typeof e | typeof i,\n name: string | number,\n ): StoreValue => {\n return Array.isArray(from)\n ? (typeof name === \"number\" ? from[name] : void 0)\n : context.store.getFieldValue(from, String(name))\n };\n\n mergeTree.map.forEach((childTree, storeFieldName) => {\n const eVal = getValue(e, storeFieldName);\n const iVal = getValue(i, storeFieldName);\n // If we have no incoming data, leave any existing data untouched.\n if (void 0 === iVal) return;\n if (getStorageArgs) {\n getStorageArgs.push(storeFieldName);\n }\n const aVal = this.applyMerges(\n childTree,\n eVal,\n iVal,\n context,\n getStorageArgs,\n );\n if (aVal !== iVal) {\n changedFields = changedFields || new Map;\n changedFields.set(storeFieldName, aVal);\n }\n if (getStorageArgs) {\n invariant(getStorageArgs.pop() === storeFieldName);\n }\n });\n\n if (changedFields) {\n // Shallow clone i so we can add changed fields to it.\n incoming = (Array.isArray(i) ? i.slice(0) : { ...i }) as T;\n changedFields.forEach((value, name) => {\n (incoming as any)[name] = value;\n });\n }\n }\n\n if (mergeTree.info) {\n return this.cache.policies.runMergeFunction(\n existing,\n incoming,\n mergeTree.info,\n context,\n getStorageArgs && context.store.getStorage(...getStorageArgs),\n );\n }\n\n return incoming;\n }\n}\n\nconst emptyMergeTreePool: MergeTree[] = [];\n\nfunction getChildMergeTree(\n { map }: MergeTree,\n name: string | number,\n): MergeTree {\n if (!map.has(name)) {\n map.set(name, emptyMergeTreePool.pop() || { map: new Map });\n }\n return map.get(name)!;\n}\n\nfunction mergeMergeTrees(\n left: MergeTree | undefined,\n right: MergeTree | undefined,\n): MergeTree {\n if (left === right || !right || mergeTreeIsEmpty(right)) return left!;\n if (!left || mergeTreeIsEmpty(left)) return right;\n\n const info = left.info && right.info ? {\n ...left.info,\n ...right.info,\n } : left.info || right.info;\n\n const needToMergeMaps = left.map.size && right.map.size;\n const map = needToMergeMaps ? new Map :\n left.map.size ? left.map : right.map;\n\n const merged = { info, map };\n\n if (needToMergeMaps) {\n const remainingRightKeys = new Set(right.map.keys());\n\n left.map.forEach((leftTree, key) => {\n merged.map.set(\n key,\n mergeMergeTrees(leftTree, right.map.get(key)),\n );\n remainingRightKeys.delete(key);\n });\n\n remainingRightKeys.forEach(key => {\n merged.map.set(\n key,\n mergeMergeTrees(\n right.map.get(key),\n left.map.get(key),\n ),\n );\n });\n }\n\n return merged;\n}\n\nfunction mergeTreeIsEmpty(tree: MergeTree | undefined): boolean {\n return !tree || !(tree.info || tree.map.size);\n}\n\nfunction maybeRecycleChildMergeTree(\n { map }: MergeTree,\n name: string | number,\n) {\n const childTree = map.get(name);\n if (childTree && mergeTreeIsEmpty(childTree)) {\n emptyMergeTreePool.push(childTree);\n map.delete(name);\n }\n}\n\nconst warnings = new Set<string>();\n\n// Note that this function is unused in production, and thus should be\n// pruned by any well-configured minifier.\nfunction warnAboutDataLoss(\n existingRef: Reference,\n incomingObj: StoreObject,\n storeFieldName: string,\n store: NormalizedCache,\n) {\n const getChild = (objOrRef: StoreObject | Reference): StoreObject | false => {\n const child = store.getFieldValue<StoreObject>(objOrRef, storeFieldName);\n return typeof child === \"object\" && child;\n };\n\n const existing = getChild(existingRef);\n if (!existing) return;\n\n const incoming = getChild(incomingObj);\n if (!incoming) return;\n\n // It's always safe to replace a reference, since it refers to data\n // safely stored elsewhere.\n if (isReference(existing)) return;\n\n // If the values are structurally equivalent, we do not need to worry\n // about incoming replacing existing.\n if (equal(existing, incoming)) return;\n\n // If we're replacing every key of the existing object, then the\n // existing data would be overwritten even if the objects were\n // normalized, so warning would not be helpful here.\n if (Object.keys(existing).every(\n key => store.getFieldValue(incoming, key) !== void 0)) {\n return;\n }\n\n const parentType =\n store.getFieldValue<string>(existingRef, \"__typename\") ||\n store.getFieldValue<string>(incomingObj, \"__typename\");\n const fieldName = fieldNameFromStoreName(storeFieldName);\n const typeDotName = `${parentType}.${fieldName}`;\n // Avoid warning more than once for the same type and field name.\n if (warnings.has(typeDotName)) return;\n warnings.add(typeDotName);\n\n const childTypenames: string[] = [];\n // Arrays do not have __typename fields, and always need a custom merge\n // function, even if their elements are normalized entities.\n if (!Array.isArray(existing) &&\n !Array.isArray(incoming)) {\n [existing, incoming].forEach(child => {\n const typename = store.getFieldValue(child, \"__typename\");\n if (typeof typename === \"string\" &&\n !childTypenames.includes(typename)) {\n childTypenames.push(typename);\n }\n });\n }\n\n invariant.warn(\n`Cache data may be lost when replacing the ${fieldName} field of a ${parentType} object.\n\nTo address this problem (which is not a bug in Apollo Client), ${\n childTypenames.length\n ? \"either ensure all objects of type \" +\n childTypenames.join(\" and \") + \" have an ID or a custom merge function, or \"\n : \"\"\n}define a custom merge function for the ${\n typeDotName\n} field, so InMemoryCache can safely merge these objects:\n\n existing: ${JSON.stringify(existing).slice(0, 1000)}\n incoming: ${JSON.stringify(incoming).slice(0, 1000)}\n\nFor more information about these options, please refer to the documentation:\n\n * Ensuring entity objects have IDs: https://go.apollo.dev/c/generating-unique-identifiers\n * Defining custom merge functions: https://go.apollo.dev/c/merging-non-normalized-objects\n`);\n}\n"]}
|
|
1
|
+
{"version":3,"file":"writeToStore.js","sourceRoot":"","sources":["../../../src/cache/inmemory/writeToStore.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,SAAS,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AACzD,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAEtC,OAAO,EACL,iBAAiB,EAEjB,wBAAwB,EACxB,gBAAgB,EAChB,sBAAsB,EACtB,sBAAsB,EACtB,qBAAqB,EACrB,aAAa,EACb,OAAO,EACP,sBAAsB,EAItB,WAAW,EACX,aAAa,EACb,SAAS,EACT,qBAAqB,GACtB,MAAM,iBAAiB,CAAC;AAGzB,OAAO,EAAE,yBAAyB,EAAE,sBAAsB,EAAE,uBAAuB,EAAE,MAAM,WAAW,CAAC;AAKvG,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AAiBnD,CAAC;AAUF;IACE,qBACkB,KAAoB,EAC5B,MAAoB;QADZ,UAAK,GAAL,KAAK,CAAe;QAC5B,WAAM,GAAN,MAAM,CAAc;IAC3B,CAAC;IAEG,kCAAY,GAAnB,UAAoB,KAAsB,EAAE,EAMvB;QANrB,iBAsGC;YArGC,KAAK,WAAA,EACL,MAAM,YAAA,EACN,MAAM,YAAA,EACN,SAAS,eAAA,EACT,SAAS,eAAA;QAET,IAAM,mBAAmB,GAAG,sBAAsB,CAAC,KAAK,CAAE,CAAC;QAC3D,IAAM,MAAM,GAAG,yBAAyB,EAAE,CAAC;QAE3C,SAAS,yBACJ,gBAAgB,CAAC,mBAAmB,CAAC,GACrC,SAAU,CACd,CAAC;QAEF,IAAM,OAAO,GAAiB;YAC5B,KAAK,OAAA;YACL,OAAO,EAAE,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC;YAC5B,KAAK,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BAAsB,CAAC,cAAc,CAAC,CAAC;IACzD,IAAM,WAAW,GAAM,UAAU,SAAI,SAAW,CAAC;IAEjD,IAAI,QAAQ,CAAC,GAAG,CAAC,WAAW,CAAC;QAAE,OAAO;IACtC,QAAQ,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC;IAE1B,IAAM,cAAc,GAAa,EAAE,CAAC;IAGpC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC;QACxB,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE;QAC5B,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC,OAAO,CAAC,UAAA,KAAK;YAChC,IAAM,QAAQ,GAAG,KAAK,CAAC,aAAa,CAAC,KAAK,EAAE,YAAY,CAAC,CAAC;YAC1D,IAAI,OAAO,QAAQ,KAAK,QAAQ;gBAC5B,CAAC,cAAc,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE;gBACtC,cAAc,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;aAC/B;QACH,CAAC,CAAC,CAAC;KACJ;IAED,SAAS,CAAC,IAAI,CAChB,+CAA6C,SAAS,oBAAe,UAAU,oFAG7E,cAAc,CAAC,MAAM;QACnB,CAAC,CAAC,oCAAoC;YAClC,cAAc,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,6CAA6C;QAChF,CAAC,CAAC,EAAE,gDAEN,WAAW,gFAGC,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,IAAI,CAAC,sBACvC,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,IAAI,CAAC,mRAMpD,CAAC,CAAC;AACH,CAAC","sourcesContent":["import { SelectionSetNode, FieldNode, SelectionNode } from 'graphql';\nimport { invariant, InvariantError } from 'ts-invariant';\nimport { equal } from '@wry/equality';\n\nimport {\n createFragmentMap,\n FragmentMap,\n getFragmentFromSelection,\n getDefaultValues,\n getFragmentDefinitions,\n getOperationDefinition,\n getTypenameFromResult,\n makeReference,\n isField,\n resultKeyNameFromField,\n StoreValue,\n StoreObject,\n Reference,\n isReference,\n shouldInclude,\n cloneDeep,\n addTypenameToDocument,\n} from '../../utilities';\n\nimport { NormalizedCache, ReadMergeModifyContext, MergeTree } from './types';\nimport { makeProcessedFieldsMerger, fieldNameFromStoreName, storeValueIsStoreObject } from './helpers';\nimport { StoreReader } from './readFromStore';\nimport { InMemoryCache } from './inMemoryCache';\nimport { EntityStore } from './entityStore';\nimport { Cache } from '../../core';\nimport { canonicalStringify } from './object-canon';\n\nexport interface WriteContext extends ReadMergeModifyContext {\n readonly written: {\n [dataId: string]: SelectionSetNode[];\n };\n readonly fragmentMap?: FragmentMap;\n // General-purpose deep-merge function for use during writes.\n merge<T>(existing: T, incoming: T): T;\n // If true, merge functions will be called with undefined existing data.\n overwrite: boolean;\n incomingById: Map<string, {\n fields: StoreObject;\n mergeTree?: MergeTree;\n selections: Set<SelectionNode>;\n }>;\n clientOnly: boolean;\n};\n\ninterface ProcessSelectionSetOptions {\n dataId?: string,\n result: Record<string, any>;\n selectionSet: SelectionSetNode;\n context: WriteContext;\n mergeTree: MergeTree;\n}\n\nexport class StoreWriter {\n constructor(\n public readonly cache: InMemoryCache,\n private reader?: StoreReader,\n ) {}\n\n public writeToStore(store: NormalizedCache, {\n query,\n result,\n dataId,\n variables,\n overwrite,\n }: Cache.WriteOptions): Reference | undefined {\n const operationDefinition = getOperationDefinition(query)!;\n const merger = makeProcessedFieldsMerger();\n\n variables = {\n ...getDefaultValues(operationDefinition),\n ...variables!,\n };\n\n const context: WriteContext = {\n store,\n written: Object.create(null),\n merge<T>(existing: T, incoming: T) {\n return merger.merge(existing, incoming) as T;\n },\n variables,\n varString: canonicalStringify(variables),\n fragmentMap: createFragmentMap(getFragmentDefinitions(query)),\n overwrite: !!overwrite,\n incomingById: new Map,\n clientOnly: false,\n };\n\n const ref = this.processSelectionSet({\n result: result || Object.create(null),\n dataId,\n selectionSet: operationDefinition.selectionSet,\n mergeTree: { map: new Map },\n context,\n });\n\n if (!isReference(ref)) {\n throw new InvariantError(`Could not identify object ${JSON.stringify(result)}`);\n }\n\n // So far, the store has not been modified, so now it's time to process\n // context.incomingById and merge those incoming fields into context.store.\n context.incomingById.forEach(({ fields, mergeTree, selections }, dataId) => {\n const entityRef = makeReference(dataId);\n\n if (mergeTree && mergeTree.map.size) {\n const applied = this.applyMerges(mergeTree, entityRef, fields, context);\n if (isReference(applied)) {\n // Assume References returned by applyMerges have already been merged\n // into the store. See makeMergeObjectsFunction in policies.ts for an\n // example of how this can happen.\n return;\n }\n // Otherwise, applyMerges returned a StoreObject, whose fields we should\n // merge into the store (see store.merge statement below).\n fields = applied;\n }\n\n if (__DEV__ && !context.overwrite) {\n const hasSelectionSet = (storeFieldName: string) =>\n fieldsWithSelectionSets.has(fieldNameFromStoreName(storeFieldName));\n const fieldsWithSelectionSets = new Set<string>();\n selections.forEach(selection => {\n if (isField(selection) && selection.selectionSet) {\n fieldsWithSelectionSets.add(selection.name.value);\n }\n });\n\n const hasMergeFunction = (storeFieldName: string) => {\n const childTree = mergeTree && mergeTree.map.get(storeFieldName);\n return Boolean(childTree && childTree.info && childTree.info.merge);\n };\n\n Object.keys(fields).forEach(storeFieldName => {\n // If a merge function was defined for this field, trust that it\n // did the right thing about (not) clobbering data. If the field\n // has no selection set, it's a scalar field, so it doesn't need\n // a merge function (even if it's an object, like JSON data).\n if (hasSelectionSet(storeFieldName) &&\n !hasMergeFunction(storeFieldName)) {\n warnAboutDataLoss(\n entityRef,\n fields,\n storeFieldName,\n context.store,\n );\n }\n });\n }\n\n store.merge(dataId, fields);\n });\n\n // Any IDs written explicitly to the cache will be retained as\n // reachable root IDs for garbage collection purposes. Although this\n // logic includes root IDs like ROOT_QUERY and ROOT_MUTATION, their\n // retainment counts are effectively ignored because cache.gc() always\n // includes them in its root ID set.\n store.retain(ref.__ref);\n\n return ref;\n }\n\n private processSelectionSet({\n dataId,\n result,\n selectionSet,\n context,\n // This object allows processSelectionSet to report useful information\n // to its callers without explicitly returning that information.\n mergeTree,\n }: ProcessSelectionSetOptions): StoreObject | Reference {\n const { policies } = this.cache;\n\n // Identify the result object, even if dataId was already provided,\n // since we always need keyObject below.\n const [id, keyObject] = policies.identify(\n result, selectionSet, context.fragmentMap);\n\n // If dataId was not provided, fall back to the id just generated by\n // policies.identify.\n dataId = dataId || id;\n\n if (\"string\" === typeof dataId) {\n // Avoid processing the same entity object using the same selection\n // set more than once. We use an array instead of a Set since most\n // entity IDs will be written using only one selection set, so the\n // size of this array is likely to be very small, meaning indexOf is\n // likely to be faster than Set.prototype.has.\n const sets = context.written[dataId] || (context.written[dataId] = []);\n const ref = makeReference(dataId);\n if (sets.indexOf(selectionSet) >= 0) return ref;\n sets.push(selectionSet);\n\n // If we're about to write a result object into the store, but we\n // happen to know that the exact same (===) result object would be\n // returned if we were to reread the result with the same inputs,\n // then we can skip the rest of the processSelectionSet work for\n // this object, and immediately return a Reference to it.\n if (this.reader && this.reader.isFresh(\n result,\n ref,\n selectionSet,\n context,\n )) {\n return ref;\n }\n }\n\n // This variable will be repeatedly updated using context.merge to\n // accumulate all fields that need to be written into the store.\n let incomingFields: StoreObject = Object.create(null);\n\n // Write any key fields that were used during identification, even if\n // they were not mentioned in the original query.\n if (keyObject) {\n incomingFields = context.merge(incomingFields, keyObject);\n }\n\n // If typename was not passed in, infer it. Note that typename is\n // always passed in for tricky-to-infer cases such as \"Query\" for\n // ROOT_QUERY.\n const typename: string | undefined =\n (dataId && policies.rootTypenamesById[dataId]) ||\n getTypenameFromResult(result, selectionSet, context.fragmentMap) ||\n (dataId && context.store.get(dataId, \"__typename\") as string);\n\n if (\"string\" === typeof typename) {\n incomingFields.__typename = typename;\n }\n\n const selections = new Set(selectionSet.selections);\n\n selections.forEach(selection => {\n if (!shouldInclude(selection, context.variables)) return;\n\n if (isField(selection)) {\n const resultFieldKey = resultKeyNameFromField(selection);\n const value = result[resultFieldKey];\n\n const wasClientOnly = context.clientOnly;\n context.clientOnly = wasClientOnly || !!(\n selection.directives &&\n selection.directives.some(d => d.name.value === \"client\")\n );\n\n if (value !== void 0) {\n const storeFieldName = policies.getStoreFieldName({\n typename,\n fieldName: selection.name.value,\n field: selection,\n variables: context.variables,\n });\n\n const childTree = getChildMergeTree(mergeTree, storeFieldName);\n\n let incomingValue =\n this.processFieldValue(value, selection, context, childTree);\n\n // To determine if this field holds a child object with a merge\n // function defined in its type policy (see PR #7070), we need to\n // figure out the child object's __typename.\n let childTypename: string | undefined;\n\n // The field's value can be an object that has a __typename only if\n // the field has a selection set. Otherwise incomingValue is scalar.\n if (selection.selectionSet) {\n // We attempt to find the child __typename first in context.store,\n // but the child object may not exist in the store yet, likely\n // because it's being written for the first time, during this very\n // call to writeToStore. Note: if incomingValue is a non-normalized\n // StoreObject (not a Reference), getFieldValue will read from that\n // object's properties to find its __typename.\n childTypename = context.store.getFieldValue<string>(\n incomingValue as StoreObject | Reference,\n \"__typename\",\n );\n\n // If the child object is being written for the first time, but\n // incomingValue is a Reference, then the entity that Reference\n // identifies should have an entry in context.incomingById, which\n // likely contains a __typename field we can use. After all, how\n // could we know the object's ID if it had no __typename? If we\n // wrote data into context.store as each processSelectionSet call\n // finished processing an entity object, the child object would\n // already be in context.store, so we wouldn't need this extra\n // check, but holding all context.store.merge calls until after\n // we've finished all processSelectionSet work is cleaner and solves\n // other problems, such as issue #8370.\n if (!childTypename && isReference(incomingValue)) {\n const info = context.incomingById.get(incomingValue.__ref);\n childTypename = info && info.fields.__typename;\n }\n }\n\n const merge = policies.getMergeFunction(\n typename,\n selection.name.value,\n childTypename,\n );\n\n if (merge) {\n childTree.info = {\n // TODO Check compatibility against any existing\n // childTree.field?\n field: selection,\n typename,\n merge,\n };\n } else {\n maybeRecycleChildMergeTree(mergeTree, storeFieldName);\n }\n\n incomingFields = context.merge(incomingFields, {\n [storeFieldName]: incomingValue,\n });\n\n } else if (\n !context.clientOnly &&\n !addTypenameToDocument.added(selection)\n ) {\n invariant.error(`Missing field '${\n resultKeyNameFromField(selection)\n }' while writing result ${\n JSON.stringify(result, null, 2)\n }`.substring(0, 1000));\n }\n\n context.clientOnly = wasClientOnly;\n\n } else {\n // This is not a field, so it must be a fragment, either inline or named\n const fragment = getFragmentFromSelection(\n selection,\n context.fragmentMap,\n );\n\n if (fragment &&\n // By passing result and context.variables, we enable\n // policies.fragmentMatches to bend the rules when typename is\n // not a known subtype of the fragment type condition, but the\n // result object contains all the keys requested by the\n // fragment, which strongly suggests the fragment probably\n // matched. This fuzzy matching behavior must be enabled by\n // including a regular expression string (such as \".*\" or\n // \"Prefix.*\" or \".*Suffix\") in the possibleTypes array for\n // specific supertypes; otherwise, all matching remains exact.\n // Fuzzy matches are remembered by the Policies object and\n // later used when reading from the cache. Since there is no\n // incoming result object to check when reading, reading does\n // not involve the same fuzzy inference, so the StoreReader\n // class calls policies.fragmentMatches without passing result\n // or context.variables. The flexibility of fuzzy matching\n // allows existing clients to accommodate previously unknown\n // __typename strings produced by server/schema changes, which\n // would otherwise be breaking changes.\n policies.fragmentMatches(fragment, typename, result, context.variables)) {\n fragment.selectionSet.selections.forEach(selections.add, selections);\n }\n }\n });\n\n if (\"string\" === typeof dataId) {\n const previous = context.incomingById.get(dataId);\n if (previous) {\n previous.fields = context.merge(previous.fields, incomingFields);\n previous.mergeTree = mergeMergeTrees(previous.mergeTree, mergeTree);\n // Add all previous SelectionNode objects, rather than creating a new\n // Set, since the original unmerged selections Set is not going to be\n // needed again (only the merged Set).\n previous.selections.forEach(selections.add, selections);\n previous.selections = selections;\n } else {\n context.incomingById.set(dataId, {\n fields: incomingFields,\n // Save a reference to mergeTree only if it is not empty, because\n // empty MergeTrees may be recycled by maybeRecycleChildMergeTree and\n // reused for entirely different parts of the result tree.\n mergeTree: mergeTreeIsEmpty(mergeTree) ? void 0 : mergeTree,\n selections,\n });\n }\n return makeReference(dataId);\n }\n\n return incomingFields;\n }\n\n private processFieldValue(\n value: any,\n field: FieldNode,\n context: WriteContext,\n mergeTree: MergeTree,\n ): StoreValue {\n if (!field.selectionSet || value === null) {\n // In development, we need to clone scalar values so that they can be\n // safely frozen with maybeDeepFreeze in readFromStore.ts. In production,\n // it's cheaper to store the scalar values directly in the cache.\n return __DEV__ ? cloneDeep(value) : value;\n }\n\n if (Array.isArray(value)) {\n return value.map((item, i) => {\n const value = this.processFieldValue(\n item, field, context, getChildMergeTree(mergeTree, i));\n maybeRecycleChildMergeTree(mergeTree, i);\n return value;\n });\n }\n\n return this.processSelectionSet({\n result: value,\n selectionSet: field.selectionSet,\n context,\n mergeTree,\n });\n }\n\n private applyMerges<T extends StoreValue>(\n mergeTree: MergeTree,\n existing: StoreValue,\n incoming: T,\n context: WriteContext,\n getStorageArgs?: Parameters<EntityStore[\"getStorage\"]>,\n ): T | Reference {\n if (mergeTree.map.size && !isReference(incoming)) {\n const e: StoreObject | Reference | undefined = (\n // Items in the same position in different arrays are not\n // necessarily related to each other, so when incoming is an array\n // we process its elements as if there was no existing data.\n !Array.isArray(incoming) &&\n // Likewise, existing must be either a Reference or a StoreObject\n // in order for its fields to be safe to merge with the fields of\n // the incoming object.\n (isReference(existing) || storeValueIsStoreObject(existing))\n ) ? existing : void 0;\n\n // This narrowing is implied by mergeTree.map.size > 0 and\n // !isReference(incoming), though TypeScript understandably cannot\n // hope to infer this type.\n const i = incoming as StoreObject | StoreValue[];\n\n // The options.storage objects provided to read and merge functions\n // are derived from the identity of the parent object plus a\n // sequence of storeFieldName strings/numbers identifying the nested\n // field name path of each field value to be merged.\n if (e && !getStorageArgs) {\n getStorageArgs = [isReference(e) ? e.__ref : e];\n }\n\n // It's possible that applying merge functions to this subtree will\n // not change the incoming data, so this variable tracks the fields\n // that did change, so we can create a new incoming object when (and\n // only when) at least one incoming field has changed. We use a Map\n // to preserve the type of numeric keys.\n let changedFields: Map<string | number, StoreValue> | undefined;\n\n const getValue = (\n from: typeof e | typeof i,\n name: string | number,\n ): StoreValue => {\n return Array.isArray(from)\n ? (typeof name === \"number\" ? from[name] : void 0)\n : context.store.getFieldValue(from, String(name))\n };\n\n mergeTree.map.forEach((childTree, storeFieldName) => {\n const eVal = getValue(e, storeFieldName);\n const iVal = getValue(i, storeFieldName);\n // If we have no incoming data, leave any existing data untouched.\n if (void 0 === iVal) return;\n if (getStorageArgs) {\n getStorageArgs.push(storeFieldName);\n }\n const aVal = this.applyMerges(\n childTree,\n eVal,\n iVal,\n context,\n getStorageArgs,\n );\n if (aVal !== iVal) {\n changedFields = changedFields || new Map;\n changedFields.set(storeFieldName, aVal);\n }\n if (getStorageArgs) {\n invariant(getStorageArgs.pop() === storeFieldName);\n }\n });\n\n if (changedFields) {\n // Shallow clone i so we can add changed fields to it.\n incoming = (Array.isArray(i) ? i.slice(0) : { ...i }) as T;\n changedFields.forEach((value, name) => {\n (incoming as any)[name] = value;\n });\n }\n }\n\n if (mergeTree.info) {\n return this.cache.policies.runMergeFunction(\n existing,\n incoming,\n mergeTree.info,\n context,\n getStorageArgs && context.store.getStorage(...getStorageArgs),\n );\n }\n\n return incoming;\n }\n}\n\nconst emptyMergeTreePool: MergeTree[] = [];\n\nfunction getChildMergeTree(\n { map }: MergeTree,\n name: string | number,\n): MergeTree {\n if (!map.has(name)) {\n map.set(name, emptyMergeTreePool.pop() || { map: new Map });\n }\n return map.get(name)!;\n}\n\nfunction mergeMergeTrees(\n left: MergeTree | undefined,\n right: MergeTree | undefined,\n): MergeTree {\n if (left === right || !right || mergeTreeIsEmpty(right)) return left!;\n if (!left || mergeTreeIsEmpty(left)) return right;\n\n const info = left.info && right.info ? {\n ...left.info,\n ...right.info,\n } : left.info || right.info;\n\n const needToMergeMaps = left.map.size && right.map.size;\n const map = needToMergeMaps ? new Map :\n left.map.size ? left.map : right.map;\n\n const merged = { info, map };\n\n if (needToMergeMaps) {\n const remainingRightKeys = new Set(right.map.keys());\n\n left.map.forEach((leftTree, key) => {\n merged.map.set(\n key,\n mergeMergeTrees(leftTree, right.map.get(key)),\n );\n remainingRightKeys.delete(key);\n });\n\n remainingRightKeys.forEach(key => {\n merged.map.set(\n key,\n mergeMergeTrees(\n right.map.get(key),\n left.map.get(key),\n ),\n );\n });\n }\n\n return merged;\n}\n\nfunction mergeTreeIsEmpty(tree: MergeTree | undefined): boolean {\n return !tree || !(tree.info || tree.map.size);\n}\n\nfunction maybeRecycleChildMergeTree(\n { map }: MergeTree,\n name: string | number,\n) {\n const childTree = map.get(name);\n if (childTree && mergeTreeIsEmpty(childTree)) {\n emptyMergeTreePool.push(childTree);\n map.delete(name);\n }\n}\n\nconst warnings = new Set<string>();\n\n// Note that this function is unused in production, and thus should be\n// pruned by any well-configured minifier.\nfunction warnAboutDataLoss(\n existingRef: Reference,\n incomingObj: StoreObject,\n storeFieldName: string,\n store: NormalizedCache,\n) {\n const getChild = (objOrRef: StoreObject | Reference): StoreObject | false => {\n const child = store.getFieldValue<StoreObject>(objOrRef, storeFieldName);\n return typeof child === \"object\" && child;\n };\n\n const existing = getChild(existingRef);\n if (!existing) return;\n\n const incoming = getChild(incomingObj);\n if (!incoming) return;\n\n // It's always safe to replace a reference, since it refers to data\n // safely stored elsewhere.\n if (isReference(existing)) return;\n\n // If the values are structurally equivalent, we do not need to worry\n // about incoming replacing existing.\n if (equal(existing, incoming)) return;\n\n // If we're replacing every key of the existing object, then the\n // existing data would be overwritten even if the objects were\n // normalized, so warning would not be helpful here.\n if (Object.keys(existing).every(\n key => store.getFieldValue(incoming, key) !== void 0)) {\n return;\n }\n\n const parentType =\n store.getFieldValue<string>(existingRef, \"__typename\") ||\n store.getFieldValue<string>(incomingObj, \"__typename\");\n const fieldName = fieldNameFromStoreName(storeFieldName);\n const typeDotName = `${parentType}.${fieldName}`;\n // Avoid warning more than once for the same type and field name.\n if (warnings.has(typeDotName)) return;\n warnings.add(typeDotName);\n\n const childTypenames: string[] = [];\n // Arrays do not have __typename fields, and always need a custom merge\n // function, even if their elements are normalized entities.\n if (!Array.isArray(existing) &&\n !Array.isArray(incoming)) {\n [existing, incoming].forEach(child => {\n const typename = store.getFieldValue(child, \"__typename\");\n if (typeof typename === \"string\" &&\n !childTypenames.includes(typename)) {\n childTypenames.push(typename);\n }\n });\n }\n\n invariant.warn(\n`Cache data may be lost when replacing the ${fieldName} field of a ${parentType} object.\n\nTo address this problem (which is not a bug in Apollo Client), ${\n childTypenames.length\n ? \"either ensure all objects of type \" +\n childTypenames.join(\" and \") + \" have an ID or a custom merge function, or \"\n : \"\"\n}define a custom merge function for the ${\n typeDotName\n} field, so InMemoryCache can safely merge these objects:\n\n existing: ${JSON.stringify(existing).slice(0, 1000)}\n incoming: ${JSON.stringify(incoming).slice(0, 1000)}\n\nFor more information about these options, please refer to the documentation:\n\n * Ensuring entity objects have IDs: https://go.apollo.dev/c/generating-unique-identifiers\n * Defining custom merge functions: https://go.apollo.dev/c/merging-non-normalized-objects\n`);\n}\n"]}
|
package/core/core.cjs.js
CHANGED