UNPKG

simple-graphql

Version:

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

88 lines 3.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const graphql_1 = require("graphql"); const typeKey = 'variant'; const valueKey = 'value'; function unionInputType(options) { return new graphql_1.GraphQLScalarType({ name: options.name, serialize: function (value) { return value; }, parseValue: function (inputValue) { if (inputValue) { const type = inputValue[typeKey]; if (!type) { throw new graphql_1.GraphQLError(`${options.name}(UnionInputType): Missing typeKey ${typeKey} property'`); } const valueType = options.inputValueTypes[type]; if (!valueType) { throw new graphql_1.GraphQLError(`${options.name}(UnionInputType): Invalid inputType ${type}'`); } if (inputValue[valueKey] == null) { return { [typeKey]: inputValue[typeKey], [valueKey]: inputValue[valueKey] }; } const value = (0, graphql_1.coerceInputValue)(inputValue[valueKey], valueType); return { [typeKey]: inputValue[typeKey], [valueKey]: value }; } }, parseLiteral: function (ast) { let type; try { if (ast.kind === 'ObjectValue') { const fields = ast.fields; for (let i = 0; i < fields.length; i++) { if (fields[i].name.value === typeKey) { const value = fields[i].value; if (value.kind === 'StringValue') { type = value.value; break; } } } } if (!type) { throw new Error(`Miss properties ${typeKey}`); } } catch (err) { throw new graphql_1.GraphQLError(`${options.name}(UnionInputType): Missing typeKey ${typeKey} property'`); } const valueType = options.inputValueTypes[type]; if (!valueType) { throw new graphql_1.GraphQLError(`${options.name}(UnionInputType): Invalid inputType ${type}'`); } const inputType = new graphql_1.GraphQLInputObjectType({ name: options.name, fields: function () { return { [typeKey]: { type: graphql_1.GraphQLString }, [valueKey]: { type: valueType } }; } }); //if (isValidLiteralValue(inputType, ast).length === 0) { return (0, graphql_1.valueFromAST)(ast, inputType); // } else { // throw new GraphQLError( // `expected ${valueKey} type ${type}, found ${_.get( // ast, // 'loc.source.body' // ).substring(_.get(ast, 'loc.start'), _.get(ast, 'loc.end'))}` // ) // } } }); } exports.default = unionInputType; //# sourceMappingURL=unionInputType.js.map