@graphql-tools/graphql
Version:
Fork of GraphQL.js
101 lines (100 loc) • 5.77 kB
JavaScript
;
// TODO: Add tests for me.
Object.defineProperty(exports, "__esModule", { value: true });
exports.addResolversToExistingSchema = void 0;
const index_js_1 = require("../type/index.js");
function setFieldProperties(field, propertiesObj) {
for (const propertyName in propertiesObj) {
field[propertyName] = propertiesObj[propertyName];
}
}
function addResolversToExistingSchema(schema, resolvers) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
const typeMap = schema.getTypeMap();
for (const typeName in resolvers) {
const type = schema.getType(typeName);
const resolverValue = resolvers[typeName];
if ((0, index_js_1.isScalarType)(type)) {
for (const fieldName in resolverValue) {
if (fieldName.startsWith('__')) {
type[fieldName.substring(2)] = resolverValue[fieldName];
}
else if (fieldName === 'astNode' && type.astNode != null) {
type.astNode = {
...type.astNode,
description: (_b = (_a = resolverValue === null || resolverValue === void 0 ? void 0 : resolverValue.astNode) === null || _a === void 0 ? void 0 : _a.description) !== null && _b !== void 0 ? _b : type.astNode.description,
directives: ((_c = type.astNode.directives) !== null && _c !== void 0 ? _c : []).concat((_e = (_d = resolverValue === null || resolverValue === void 0 ? void 0 : resolverValue.astNode) === null || _d === void 0 ? void 0 : _d.directives) !== null && _e !== void 0 ? _e : []),
};
}
else if (fieldName === 'extensionASTNodes' && type.extensionASTNodes != null) {
type.extensionASTNodes = type.extensionASTNodes.concat((_f = resolverValue === null || resolverValue === void 0 ? void 0 : resolverValue.extensionASTNodes) !== null && _f !== void 0 ? _f : []);
}
else if (fieldName === 'extensions' &&
type.extensions != null &&
resolverValue.extensions != null) {
type.extensions = Object.assign(Object.create(null), type.extensions, resolverValue.extensions);
}
else {
type[fieldName] = resolverValue[fieldName];
}
}
}
else if ((0, index_js_1.isEnumType)(type)) {
const config = type.toConfig();
const enumValueConfigMap = config.values;
for (const fieldName in resolverValue) {
if (fieldName.startsWith('__')) {
config[fieldName.substring(2)] = resolverValue[fieldName];
}
else if (fieldName === 'astNode' && config.astNode != null) {
config.astNode = {
...config.astNode,
description: (_h = (_g = resolverValue === null || resolverValue === void 0 ? void 0 : resolverValue.astNode) === null || _g === void 0 ? void 0 : _g.description) !== null && _h !== void 0 ? _h : config.astNode.description,
directives: ((_j = config.astNode.directives) !== null && _j !== void 0 ? _j : []).concat((_l = (_k = resolverValue === null || resolverValue === void 0 ? void 0 : resolverValue.astNode) === null || _k === void 0 ? void 0 : _k.directives) !== null && _l !== void 0 ? _l : []),
};
}
else if (fieldName === 'extensionASTNodes' && config.extensionASTNodes != null) {
config.extensionASTNodes = config.extensionASTNodes.concat((_m = resolverValue === null || resolverValue === void 0 ? void 0 : resolverValue.extensionASTNodes) !== null && _m !== void 0 ? _m : []);
}
else if (fieldName === 'extensions' &&
type.extensions != null &&
resolverValue.extensions != null) {
type.extensions = Object.assign(Object.create(null), type.extensions, resolverValue.extensions);
}
else if (enumValueConfigMap[fieldName]) {
enumValueConfigMap[fieldName].value = resolverValue[fieldName];
}
}
Object.assign(typeMap[typeName], new index_js_1.GraphQLEnumType(config));
}
else if ((0, index_js_1.isUnionType)(type)) {
for (const fieldName in resolverValue) {
if (fieldName.startsWith('__')) {
type[fieldName.substring(2)] = resolverValue[fieldName];
}
}
}
else if ((0, index_js_1.isObjectType)(type) || (0, index_js_1.isInterfaceType)(type)) {
for (const fieldName in resolverValue) {
if (fieldName.startsWith('__')) {
// this is for isTypeOf and resolveType and all the other stuff.
type[fieldName.substring(2)] = resolverValue[fieldName];
continue;
}
const fields = type.getFields();
const field = fields[fieldName];
if (field != null) {
const fieldResolve = resolverValue[fieldName];
if (typeof fieldResolve === 'function') {
// for convenience. Allows shorter syntax in resolver definition file
field.resolve = fieldResolve.bind(resolverValue);
}
else {
setFieldProperties(field, fieldResolve);
}
}
}
}
}
}
exports.addResolversToExistingSchema = addResolversToExistingSchema;