UNPKG

simpl-to-graphql-schema

Version:
78 lines (67 loc) 2.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var _getFieldsKey = require("./utils/getFieldsKey"); var _getSchema = require("./utils/getSchema"); var SchemaBridge = { schema: function schema(_ref) { var _schema = _ref.schema, name = _ref.name, _ref$options = _ref.options, options = _ref$options === undefined ? {} : _ref$options; var fields = options.fields, _options$scalar = options.scalar, scalar = _options$scalar === undefined ? [] : _options$scalar, except = options.except, custom = options.custom, _options$additional = options.additional, additional = _options$additional === undefined ? [] : _options$additional, _options$print = options.print, print = _options$print === undefined ? false : _options$print; var GqlSchemaContent = void 0; var GraphqlObjsSchema = void 0; // Get fields name var fieldName = (0, _getFieldsKey.getFieldsKey)({ schema: _schema, fields: fields, except: except }); // Get new type definitions GraphqlObjsSchema = fieldName.objectKeys.map(function (k) { return (0, _getSchema.getObjectSchema)(_schema, k, name, custom); }); GraphqlObjsSchema = GraphqlObjsSchema.length ? GraphqlObjsSchema.reduce(function (a, b) { return "" + a + b; }) : ""; // Get GraphQl principal Object content GqlSchemaContent = fieldName.keys.map(function (key) { return (0, _getSchema.getFieldSchema)(_schema, key, name, custom); }); GqlSchemaContent = GqlSchemaContent.reduce(function (a, b) { return "" + a + b; }); var scalars = ""; if (options && scalar && scalar.length > 0) { scalars = scalar.map(function (value) { return "scalar " + value; }); scalars = scalars.reduce(function (a, b) { return a + "\n\t" + b; }); } var toAdd = ""; if (options && additional && additional.length > 0) { toAdd = additional.map(function (value) { return "" + value; }); toAdd = toAdd.reduce(function (a, b) { return a + "\n\t" + b; }); } var toReturn = "\n" + scalars + "\n" + GraphqlObjsSchema + "\ntype " + name + " {\n " + GqlSchemaContent + "\n " + toAdd + "\n}\n"; if (print) { console.log("--------START--------"); console.log(toReturn); console.log("---------END---------"); } return toReturn; } }; exports.default = SchemaBridge;