get-graphql-from-jsonschema
Version:
get-graphql-from-jsonschema gets a GraphQL schema from a JSON schema.
50 lines (49 loc) • 2.03 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseSchema = void 0;
const parseType_1 = require("./parseType");
const parseUnion_1 = require("./parseUnion");
const common_tags_1 = require("common-tags");
const toBreadcrumb_1 = require("./toBreadcrumb");
const toPascalCase_1 = require("./toPascalCase");
const errors = __importStar(require("./errors"));
const parseSchema = function ({ path, schema, direction }) {
let result;
if ('type' in schema) {
result = (0, parseType_1.parseType)({ path, schema, direction });
}
else if ('oneOf' in schema || 'anyOf' in schema) {
result = (0, parseUnion_1.parseUnion)({ path, schema, direction });
}
else {
throw new errors.SchemaInvalid(`Structure at '${(0, toBreadcrumb_1.toBreadcrumb)(path)}' not recognized.`);
}
if (result.typeName.includes('|')) {
const typeName = (0, toPascalCase_1.toPascalCase)(path);
result.typeDefinitions.push((0, common_tags_1.stripIndent) `
union ${typeName} = ${result.typeName}
`);
result.typeName = typeName;
}
return result;
};
exports.parseSchema = parseSchema;