UNPKG

get-graphql-from-jsonschema

Version:

get-graphql-from-jsonschema gets a GraphQL schema from a JSON schema.

36 lines (35 loc) 1.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseType = void 0; const handleArrayType_1 = require("./handleArrayType"); const handleObjectType_1 = require("./handleObjectType"); const handleScalarType_1 = require("./handleScalarType"); const hasArrayType_1 = require("./hasArrayType"); const hasObjectType_1 = require("./hasObjectType"); const parseType = function ({ path, schema, direction }) { const graphqlTypeNames = []; const graphqlTypeDefinitions = []; const subPath = [...path, `T${0}`]; if ((0, hasArrayType_1.hasArrayType)(schema)) { return (0, handleArrayType_1.handleArrayType)({ path: subPath, schema, direction }); } if ((0, hasObjectType_1.hasObjectType)(schema)) { return (0, handleObjectType_1.handleObjectType)({ path: subPath, schema, direction }); } if (!Array.isArray(schema.type)) { return (0, handleScalarType_1.handleScalarType)({ type: schema.type }); } schema.type.forEach((type) => { const result = (0, handleScalarType_1.handleScalarType)({ type }); graphqlTypeNames.push(result.typeName); graphqlTypeDefinitions.push(...result.typeDefinitions); }); const graphqlTypeName = graphqlTypeNames. filter((name) => name.trim() !== ''). join(' | '); return { typeName: graphqlTypeName, typeDefinitions: graphqlTypeDefinitions }; }; exports.parseType = parseType;