UNPKG

@netlify/content-engine

Version:
81 lines 3.16 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.isASTDocument = exports.typesWithoutInference = exports.reportParsingError = exports.parseTypeDef = void 0; const graphql_1 = require("graphql"); const type_builders_1 = require("./type-builders"); const extensions_1 = require("../extensions"); const reporter_1 = __importDefault(require("../../reporter")); const isASTDocument = (typeOrTypeDef) => typeof typeOrTypeDef === `object` && typeOrTypeDef.kind && graphql_1.Kind.DOCUMENT === typeOrTypeDef.kind; exports.isASTDocument = isASTDocument; /** * Parses type definition represented as an SDL string into an AST Document. * Type definitions of other formats (other than SDL) are returned as is */ const parseTypeDef = (typeOrTypeDef) => { if (typeof typeOrTypeDef === `string`) { try { return (0, graphql_1.parse)(typeOrTypeDef); } catch (error) { reportParsingError(error); } } return typeOrTypeDef; }; exports.parseTypeDef = parseTypeDef; const reportParsingError = (error) => { const { message, source, locations } = error; if (source && locations && locations.length) { const { codeFrameColumns } = require(`@babel/code-frame`); const frame = codeFrameColumns(source.body, { start: locations[0] }, { linesAbove: 5, linesBelow: 5 }); reporter_1.default.panic(`Encountered an error parsing the provided GraphQL type definitions:\n` + message + `\n\n` + frame + `\n`); } else { throw error; } }; exports.reportParsingError = reportParsingError; /** * Given a type definition, collects type names that should skip the inference process */ const typesWithoutInference = (typeNames = [], typeOrTypeDef) => { if (typeof typeOrTypeDef === `string`) { typeOrTypeDef = parseTypeDef(typeOrTypeDef); } if (isASTDocument(typeOrTypeDef)) { typeOrTypeDef.definitions.forEach((def) => { if (!def.directives) return; def.directives.forEach((directive) => { if (directive.name.value === extensions_1.dontInferExtensionName && def.name.value) { // @ts-ignore quick convert from js typeNames.push(def.name.value); } }); }); return typeNames; } // @ts-ignore quick convert from js if ((0, type_builders_1.isGatsbyType)(typeOrTypeDef) && typeOrTypeDef.config) { // @ts-ignore quick convert from js const { extensions = {}, name } = typeOrTypeDef.config; if (name && (extensions[extensions_1.dontInferExtensionName] || extensions[extensions_1.inferExtensionName] === false)) { // @ts-ignore quick convert from js typeNames.push(name); } } return typeNames; }; exports.typesWithoutInference = typesWithoutInference; //# sourceMappingURL=type-defs.js.map