UNPKG

graphql-sock

Version:

GraphQL Semantic Output Conversion Kit - converts a cutting edge SDL file that supports semantic nullability into a more traditional SDL file legacy tools can support.

51 lines (50 loc) 2.19 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.main = main; const promises_1 = require("node:fs/promises"); const node_util_1 = require("node:util"); const graphql_1 = require("graphql"); const index_js_1 = require("./index.js"); function main() { return __awaiter(this, arguments, void 0, function* (toStrict = false) { const { values: { input, output }, } = (0, node_util_1.parseArgs)({ options: { input: { type: "string", short: "i", }, output: { type: "string", short: "o", }, }, }); if (!input) { throw new Error("Please specify an --input schema"); } if (!output) { throw new Error("Please specify an --output location"); } const sdl = yield (0, promises_1.readFile)(input, "utf8"); const schema = (0, graphql_1.buildSchema)(sdl); const errors = (0, graphql_1.validateSchema)(schema); if (errors.length > 0) { console.dir(errors); throw new Error("Invalid schema"); } const derivedSchema = toStrict ? (0, index_js_1.semanticToStrict)(schema) : (0, index_js_1.semanticToNullable)(schema); const newSdl = (0, graphql_1.printSchema)(derivedSchema); yield (0, promises_1.writeFile)(output, newSdl + "\n"); }); }