@graphql-mesh/transport-rest
Version:
40 lines (39 loc) • 1.93 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveDataByUnionInputType = void 0;
const graphql_1 = require("graphql");
const utils_1 = require("@graphql-mesh/utils");
const utils_2 = require("@graphql-tools/utils");
function resolveDataByUnionInputType(data, type, schema) {
if (data) {
if ((0, graphql_1.isListType)(type)) {
return (0, utils_2.asArray)(data).map(elem => resolveDataByUnionInputType(elem, type.ofType, schema));
}
if ((0, graphql_1.isNonNullType)(type)) {
return resolveDataByUnionInputType(data, type.ofType, schema);
}
if ((0, graphql_1.isInputObjectType)(type)) {
const typeOneOfDirectives = (0, utils_2.getDirective)(schema, type, 'oneOf');
const isOneOf = typeOneOfDirectives?.length;
const fieldMap = type.getFields();
data = (0, utils_2.asArray)(data)[0];
for (const propertyName in data) {
const fieldName = (0, utils_1.sanitizeNameForGraphQL)(propertyName);
const field = fieldMap[fieldName];
if (field) {
if (isOneOf) {
const resolvedData = resolveDataByUnionInputType(data[fieldName], field.type, schema);
return resolvedData;
}
const fieldData = data[fieldName];
data[fieldName] = undefined;
const fieldResolveRootFieldDirectives = (0, utils_2.getDirective)(schema, field, 'resolveRootField');
const realFieldName = fieldResolveRootFieldDirectives?.[0]?.field || fieldName;
data[realFieldName] = resolveDataByUnionInputType(fieldData, field.type, schema);
}
}
}
}
return data;
}
exports.resolveDataByUnionInputType = resolveDataByUnionInputType;
;