UNPKG

simple-graphql

Version:

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

119 lines 5.02 kB
"use strict"; 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 unionInputType_1 = __importDefault(require("../build/type/unionInputType")); const toGraphQLInputFieldConfigMap = function (name, fields, context) { const toTypeName = (name, path) => { return (name + path .split('.') .map((v) => StringHelper_1.default.toInitialUpperCase(v)) .join('')); }; const inputFieldConfig = (typeName) => { const typeConfig = context.typeConfig(typeName); if (!typeConfig) { throw new Error(`Type "${typeName}" has not register.`); } if (typeConfig.inputType) { return { type: typeConfig.inputType }; } else { return null; } }; const convert = (name, path, field) => { const makeNonNull = function (config) { var _a, _b, _c, _d, _e; if (config == null) { return null; } config.description = (_a = field.metadata) === null || _a === void 0 ? void 0 : _a.description; if (typeof ((_c = (_b = field.metadata) === null || _b === void 0 ? void 0 : _b.graphql) === null || _c === void 0 ? void 0 : _c.defaultValue) !== 'function') { config.defaultValue = (_e = (_d = field.metadata) === null || _d === void 0 ? void 0 : _d.graphql) === null || _e === void 0 ? void 0 : _e.defaultValue; } if (field.nullable === false && !(config.type instanceof graphql_1.GraphQLNonNull)) { config.type = new graphql_1.GraphQLNonNull(config.type); } return config; }; if (field.type) { return makeNonNull(inputFieldConfig(field.type)); } else if (field.enum) { return makeNonNull({ type: new graphql_1.GraphQLEnumType({ name: StringHelper_1.default.toInitialUpperCase(toTypeName(name, path)) + 'Input', 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(inputFieldConfig(`[${field.elements.type}]`)); } const subField = convert(name, path, field.elements); if (subField) { return makeNonNull({ type: new graphql_1.GraphQLList(subField.type) }); } } if (field.properties) { if (lodash_1.default.keys(field.properties).length > 0) { return makeNonNull({ type: new graphql_1.GraphQLInputObjectType({ name: StringHelper_1.default.toInitialUpperCase(toTypeName(name, path)) + 'Input', fields: () => toGraphQLInputFieldConfigMap(toTypeName(name, path), field.properties, context) }) }); } } else if (field.values) { //TODO } else if (field.mapping) { //TODO return makeNonNull({ type: (0, unionInputType_1.default)({ name: StringHelper_1.default.toInitialUpperCase(toTypeName(name, path)) + 'Input', inputValueTypes: lodash_1.default.mapValues(field.mapping, (options, key) => { const inputType = inputFieldConfig(options.type); if (inputType) { return inputType.type; } else { throw new Error(`类型 ${options.type} 没有InputType配置`); } } //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 inputFieldConfig = convert(name, key, value); if (inputFieldConfig) { fieldMap[key] = inputFieldConfig; } } }); return fieldMap; }; exports.default = toGraphQLInputFieldConfigMap; //# sourceMappingURL=toGraphQLInputFieldConfigMap.js.map