UNPKG

simple-graphql

Version:

The simple way to generates GraphQL schemas and Sequelize models from your models definition.

181 lines 8.89 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const lodash_1 = __importDefault(require("lodash")); const graphql_1 = require("graphql"); const StringHelper_1 = __importDefault(require("../utils/StringHelper")); const toGraphQLInputFieldConfigMap_1 = __importDefault(require("./toGraphQLInputFieldConfigMap")); const toGraphQLFieldConfigMap = function (name, postfix, fields, context) { const toTypeName = (name, path) => { return (name + path .split('.') .map((v) => StringHelper_1.default.toInitialUpperCase(v)) .join('')); }; const fieldConfig = (fieldName, fieldPath, typeName) => { const typeConfig = context.typeConfig(typeName); if (!typeConfig) { throw new Error(`Type "${typeName}" has not register for ${fieldName}.`); } if (typeConfig.outputType) { const config = { type: typeConfig.outputType, args: (0, toGraphQLInputFieldConfigMap_1.default)(toTypeName(fieldName, fieldPath), typeConfig.additionalInput || {}, context) }; const outputResolve = typeConfig.outputResolve; if (outputResolve) { config.resolve = context.hookFieldResolve(fieldPath, { output: { type: typeName }, resolve: outputResolve }); } return config; } else { return undefined; } }; const convert = (name, path, field) => { const makeNonNull = function (config) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m; if (config == null) { return null; } if (field.nullable === false && !(config.type instanceof graphql_1.GraphQLNonNull)) { config.type = new graphql_1.GraphQLNonNull(config.type); } config.description = (_a = field.metadata) === null || _a === void 0 ? void 0 : _a.description; const finalField = { description: config.description, hookOptions: (_b = field.metadata) === null || _b === void 0 ? void 0 : _b.hookOptions, input: (_d = (_c = field.metadata) === null || _c === void 0 ? void 0 : _c.graphql) === null || _d === void 0 ? void 0 : _d.input, dependentFields: (_f = (_e = field.metadata) === null || _e === void 0 ? void 0 : _e.graphql) === null || _f === void 0 ? void 0 : _f.dependentFields, output: field, resolve: function (root, args, context, info) { return __awaiter(this, void 0, void 0, function* () { return root[info.fieldName]; }); } }; if ((_h = (_g = field.metadata) === null || _g === void 0 ? void 0 : _g.graphql) === null || _h === void 0 ? void 0 : _h.resolve) { const cusResolve = field.metadata.graphql.resolve; if (config.resolve) { const resolve = config.resolve; finalField.resolve = function (source, args, context, info, sgContext) { return __awaiter(this, void 0, void 0, function* () { return resolve(Object.assign(Object.assign({}, source), { [info.fieldName]: yield cusResolve(source, args, context, info, sgContext) }), args, context, info); }); }; } else { finalField.resolve = cusResolve; } config.resolve = context.hookFieldResolve(path, finalField); } else { if (config.resolve) { finalField.resolve = config.resolve; } config.resolve = context.hookFieldResolve(path, finalField); } if ((_k = (_j = field.metadata) === null || _j === void 0 ? void 0 : _j.graphql) === null || _k === void 0 ? void 0 : _k.input) { config.args = Object.assign(Object.assign({}, config.args), (0, toGraphQLInputFieldConfigMap_1.default)(toTypeName(name, path), (_m = (_l = field.metadata) === null || _l === void 0 ? void 0 : _l.graphql) === null || _m === void 0 ? void 0 : _m.input, context)); } return config; }; if (field.type) { return makeNonNull(fieldConfig(name, path, field.type)); } else if (field.enum) { return makeNonNull({ type: new graphql_1.GraphQLEnumType({ name: StringHelper_1.default.toInitialUpperCase(toTypeName(name, path)) + postfix, values: lodash_1.default.fromPairs([...field.enum].map((f) => [f, { value: f, description: f }])) }) }); } else if (field.elements) { if (field.elements.type && context.typeConfig(`[${field.elements.type}]`)) { return makeNonNull(fieldConfig(name, path, `[${field.elements.type}]`)); } else { const subField = convert(name, path, field.elements); if (subField) { return makeNonNull({ type: new graphql_1.GraphQLList(subField.type) }); } } } else if (field.properties) { if (lodash_1.default.keys(field.properties).length > 0) { return makeNonNull({ type: new graphql_1.GraphQLObjectType({ name: StringHelper_1.default.toInitialUpperCase(toTypeName(name, path)) + postfix, fields: () => toGraphQLFieldConfigMap(toTypeName(name, path), postfix, field.properties, context) }), resolve: context.hookFieldResolve(path, { output: field, resolve: function (root, args, context, info) { return __awaiter(this, void 0, void 0, function* () { return root[info.fieldName]; }); } }) }); } } else if (field.values) { //TODO } else if (field.mapping) { //TODO 支持嵌套定义 const unionTypes = lodash_1.default.mapValues(field.mapping, (value, key) => { var _a; return (_a = context.typeConfig(`_Union_${value.type}`)) === null || _a === void 0 ? void 0 : _a.outputType; }); return makeNonNull({ type: new graphql_1.GraphQLUnionType({ name: StringHelper_1.default.toInitialUpperCase(toTypeName(name, path)) + postfix, types: lodash_1.default.uniq(lodash_1.default.values(unionTypes)), resolveType(value) { if (value && value[field.discriminator]) { return context.typeConfig(`_Union_${field.mapping[value[field.discriminator]].type}` // TODO 支持嵌套定义 ).outputType; } } }) }); //TODO } return null; }; const fieldMap = {}; lodash_1.default.forOwn(fields, (value, key) => { var _a, _b; if ((_b = (_a = value.metadata) === null || _a === void 0 ? void 0 : _a.graphql) === null || _b === void 0 ? void 0 : _b.hidden) { // Hidden field, ignore // Have resolve method, ignore } else { const fieldConfig = convert(name, key, value); if (fieldConfig) { fieldMap[key] = fieldConfig; } } }); return fieldMap; }; exports.default = toGraphQLFieldConfigMap; //# sourceMappingURL=toGraphQLFieldConfigMap.js.map