UNPKG

graphql-2-json-schema

Version:
42 lines (41 loc) 1.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.graphqlToJSONType = exports.typesMapping = void 0; const lodash_1 = require("lodash"); const typeGuards_1 = require("./typeGuards"); exports.typesMapping = { Boolean: 'boolean', String: 'string', Int: 'number', Float: 'number', ID: 'string', }; const graphqlToJSONType = (k, options = {}) => { if (typeGuards_1.isIntrospectionListTypeRef(k)) { return { type: 'array', items: exports.graphqlToJSONType(k.ofType, Object.assign(Object.assign({}, options), { isArray: true })), }; } else if (typeGuards_1.isNonNullIntrospectionType(k)) { return exports.graphqlToJSONType(k.ofType, Object.assign(Object.assign({}, options), { isNonNull: true })); } else { const name = k.name; const { isArray, isNonNull, nullableArrayItems } = options; const jsonType = {}; if (lodash_1.includes(typeGuards_1.SUPPORTED_KINDS, k.kind)) { jsonType.$ref = `#/definitions/${name}`; } else { jsonType.type = exports.typesMapping[name]; } if (nullableArrayItems && isArray && !isNonNull) { return { anyOf: [jsonType, { type: 'null' }], }; } return jsonType; } }; exports.graphqlToJSONType = graphqlToJSONType;