UNPKG

t-graphql

Version:

typed GraphQL - end-to-end type-safe GraphQL for TypeScript. Think tRPC, but GraphQL

172 lines 7.71 kB
"use strict"; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.generateSchemaString = generateSchemaString; exports.generateParamValue = generateParamValue; exports.generateSchemaPart = generateSchemaPart; var assert_never_1 = __importDefault(require("assert-never")); var CustomScalarType_1 = require("../CustomScalarType"); var EnumType_1 = require("../EnumType"); var EnumValueType_1 = require("../EnumValueType"); var InputObjectType_1 = require("../inputs/InputObjectType"); var ObjectType_1 = require("../outputs/ObjectType"); var UnionType_1 = require("../outputs/UnionType"); var SchemaType_1 = require("../SchemaType"); function generateSchemaString(rootType) { var hoisted = generateSchemaPart(rootType).hoisted; return Object.values(hoisted).join('\n\n'); } function generateSchemaFieldParamStringPart(params) { var hoisted = {}; var paramEntries = Object.entries(params.schema); if (paramEntries.length === 0) { return { hoisted: hoisted, inline: '' }; } return { hoisted: hoisted, inline: [ '(', paramEntries .map(function (_a) { var key = _a[0], fieldDesc = _a[1]; var _b = generateSchemaPart(fieldDesc.type), hoistedParamParts = _b.hoisted, valueString = _b.inline; Object.assign(hoisted, hoistedParamParts); var defaultValueString = typeof fieldDesc.defaultValue === 'undefined' ? { inline: '' } : generateParamValue(fieldDesc.defaultValue); return "".concat(key, ": ").concat(valueString).concat(fieldDesc.optional ? '' : '!').concat(defaultValueString ? " = ".concat(defaultValueString) : ''); }) .join(', '), ')', ].join(''), }; } function generateParamValue(value) { if (typeof value === 'string') { return "\"".concat(String(value).replace(/"/g, '\\"'), "\""); } if (typeof value === 'number' || typeof value === 'boolean') { return String(value); } if (Array.isArray(value)) { return "[".concat(value.map(function (item) { return generateParamValue(item); }).join(', '), "]"); } if (value === null) { return 'null'; } if (typeof value === 'object') { return "{".concat(Object.entries(value) .map(function (_a) { var key = _a[0], fieldValue = _a[1]; return "".concat(key, ": ").concat(generateParamValue(fieldValue)); }) .join(', '), "}"); } (0, assert_never_1.default)(value); } function generateSchemaPart(type) { var _a, _b; if (Array.isArray(type)) { var _c = generateSchemaPart(type[0]), hoisted = _c.hoisted, inline = _c.inline; return { hoisted: hoisted, inline: "[".concat(inline).concat(type[1] === null ? '' : '!', "]") }; } if (type instanceof SchemaType_1.SchemaType) { var hoisted = {}; var hoistedQueryParts = generateSchemaPart(type.Query).hoisted; Object.assign(hoisted, hoistedQueryParts); var hoistedMutationParts = generateSchemaPart(type.Mutation).hoisted; Object.assign(hoisted, hoistedMutationParts); var hoistedSubscriptionParts = generateSchemaPart(type.Subscription).hoisted; Object.assign(hoisted, hoistedSubscriptionParts); return { hoisted: hoisted, inline: '' }; } if (type instanceof EnumValueType_1.EnumValueType) { return { hoisted: {}, inline: type.value }; } if (type instanceof EnumType_1.EnumType) { return { hoisted: (_a = {}, _a[type.typename] = __spreadArray(__spreadArray([ "enum ".concat(type.typename, " {") ], type.values.map(function (enumValue) { return " ".concat(enumValue.value); }), true), [ '}', ], false).join('\n'), _a), inline: type.typename, }; } if (type instanceof UnionType_1.UnionType) { var hoisted_1 = {}; var names_1 = []; type.types.forEach(function (unionType) { var _a = generateSchemaPart(unionType), hoistedParts = _a.hoisted, inline = _a.inline; Object.assign(hoisted_1, hoistedParts); names_1.push(inline); }); hoisted_1[type.typename] = "union ".concat(type.typename, " = ").concat(names_1.join(' | ')); return { hoisted: hoisted_1, inline: type.typename }; } if (type instanceof ObjectType_1.ObjectType) { var hoisted_2 = {}; var fieldEntries = Object.entries(type.schema); if (fieldEntries.length === 0) { return { hoisted: hoisted_2, inline: type.typename }; } var schemaStringParts_1 = ["type ".concat(type.typename, " {")]; fieldEntries.forEach(function (_a) { var key = _a[0], fieldDesc = _a[1]; var _b = fieldDesc.params ? generateSchemaFieldParamStringPart(fieldDesc.params) : { hoisted: {}, inline: '' }, hoistedParamsParts = _b.hoisted, paramListString = _b.inline; Object.assign(hoisted_2, hoistedParamsParts); var _c = generateSchemaPart(fieldDesc.type), hoistedParts = _c.hoisted, valueString = _c.inline; Object.assign(hoisted_2, hoistedParts); schemaStringParts_1.push(" ".concat(key).concat(paramListString, ": ").concat(valueString).concat(fieldDesc.optional ? '' : '!')); }); schemaStringParts_1.push('}'); hoisted_2[type.typename] = schemaStringParts_1.join('\n'); return { hoisted: hoisted_2, inline: type.typename }; } if (type instanceof InputObjectType_1.InputObjectType) { var hoisted_3 = {}; var schemaStringParts_2 = ["input ".concat(type.typename, " {")]; Object.entries(type.schema).forEach(function (_a) { var key = _a[0], fieldDesc = _a[1]; var _b = generateSchemaPart(fieldDesc.type), hoistedParts = _b.hoisted, inline = _b.inline; Object.assign(hoisted_3, hoistedParts); schemaStringParts_2.push(" ".concat(key, ": ").concat(inline).concat(fieldDesc.optional ? '' : '!')); }); schemaStringParts_2.push('}'); hoisted_3[type.typename] = schemaStringParts_2.join('\n'); return { hoisted: hoisted_3, inline: type.typename }; } if (type instanceof CustomScalarType_1.CustomScalarType) { return { hoisted: (_b = {}, _b[type.typename] = "scalar ".concat(type.typename), _b), inline: type.typename, }; } // ScalarType if (type === 'String' || type === 'Int' || type === 'Float' || type === 'Boolean' || type === 'ID') { return { hoisted: {}, inline: type }; } if (typeof type === 'string') { return { hoisted: {}, inline: "\"".concat(String(type).replace(/"/g, '\\"'), "\"") }; } if (typeof type === 'number' || typeof type === 'boolean') { return { hoisted: {}, inline: String(type) }; } (0, assert_never_1.default)(type); } //# sourceMappingURL=schema-generator.js.map