@apollo/client
Version:
A fully-featured caching GraphQL client.
103 lines (101 loc) • 4.8 kB
JavaScript
;;
const {
__DEV__
} = require("@apollo/client/utilities/environment");
Object.defineProperty(exports, "__esModule", { value: true });
exports.coerceScalarFieldsToParsed = coerceScalarFieldsToParsed;
const environment_1 = require("@apollo/client/utilities/environment");
const invariant_1 = require("@apollo/client/utilities/invariant");
const createFragmentMap_js_1 = require("./createFragmentMap.cjs");
const getFragmentDefinitions_js_1 = require("./getFragmentDefinitions.cjs");
const getFragmentFromSelection_js_1 = require("./getFragmentFromSelection.cjs");
const getMainDefinition_js_1 = require("./getMainDefinition.cjs");
const getOperationDefinition_js_1 = require("./getOperationDefinition.cjs");
const isField_js_1 = require("./isField.cjs");
const matchScalarList_js_1 = require("./matchScalarList.cjs");
const resultKeyNameFromField_js_1 = require("./resultKeyNameFromField.cjs");
const unwrapScalarType_js_1 = require("./unwrapScalarType.cjs");
/**
* @internal
*
* @deprecated This is an internal API and should not be used directly. This can be removed or changed at any time.
*/
function coerceScalarFieldsToParsed(result, query, cache) {
const operationType = (0, getOperationDefinition_js_1.getOperationDefinition)(query)?.operation;
const fragmentMap = (0, createFragmentMap_js_1.createFragmentMap)((0, getFragmentDefinitions_js_1.getFragmentDefinitions)(query));
(0, invariant_1.invariant)(operationType, 6);
function coerceFieldArray(field, fieldValue, typename, scalarType) {
let changed = false;
const items = fieldValue.map((item) => {
const coerced = coerceField(field, item, typename, scalarType);
changed ||= coerced !== item;
return coerced;
});
return changed ? items : fieldValue;
}
function coerceField(field, fieldValue, typename, scalarType) {
if (Array.isArray(fieldValue) && !scalarType) {
return coerceFieldArray(field, fieldValue, typename, scalarType);
}
if (field.selectionSet) {
return coerceSelectionSet(field.selectionSet, fieldValue);
}
if (fieldValue === null || !typename)
return fieldValue;
if (scalarType) {
const match = (0, matchScalarList_js_1.matchScalarList)(scalarType);
if (match) {
if (Array.isArray(fieldValue)) {
return coerceFieldArray(field, fieldValue, typename, match[1]);
}
else {
if (environment_1.__DEV__) {
__DEV__ && invariant_1.invariant.warn(
7,
`${typename}.${field.name.value}`,
scalarType,
(0, unwrapScalarType_js_1.unwrapScalarType)(scalarType)
);
}
}
}
const scalar = cache.getScalar((0, unwrapScalarType_js_1.unwrapScalarType)(scalarType));
if (scalar) {
return scalar.coerceToParsed(fieldValue);
}
}
return fieldValue;
}
function coerceSelectionSet(selectionSet, data, typename) {
if (data === null || typeof data !== "object")
return data;
const result = { ...data };
let changed = false;
if (Object.hasOwn(data, "__typename")) {
typename = data.__typename;
}
const workSet = new Set(selectionSet.selections);
workSet.forEach((selection) => {
if ((0, isField_js_1.isField)(selection)) {
const resultName = (0, resultKeyNameFromField_js_1.resultKeyNameFromField)(selection);
if (!Object.hasOwn(data, resultName))
return;
const fieldValue = data[resultName];
const coerced = coerceField(selection, fieldValue, typename, typename ?
cache.getScalarTypeForField(typename, selection.name.value)
: undefined);
changed ||= coerced !== fieldValue;
result[resultName] = coerced;
}
else {
const fragment = (0, getFragmentFromSelection_js_1.getFragmentFromSelection)(selection, fragmentMap);
if (fragment && typename && cache.fragmentMatches(fragment, typename)) {
fragment.selectionSet.selections.forEach((s) => workSet.add(s));
}
}
});
return changed ? result : data;
}
return coerceSelectionSet((0, getMainDefinition_js_1.getMainDefinition)(query).selectionSet, result, cache.getRootTypename(operationType));
}
//# sourceMappingURL=coerceScalarFieldsToParsed.cjs.map