UNPKG

@graphql-codegen/typescript-document-nodes

Version:

GraphQL Code Generator plugin for generating TypeScript modules with embedded GraphQL document nodes

32 lines (31 loc) 1.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.validate = exports.plugin = void 0; const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers"); const graphql_1 = require("graphql"); const visitor_js_1 = require("./visitor.js"); const plugin = (schema, documents, config) => { const allAst = (0, graphql_1.concatAST)(documents.map(v => v.document)); const allFragments = [ ...allAst.definitions.filter(d => d.kind === graphql_1.Kind.FRAGMENT_DEFINITION).map(fragmentDef => ({ node: fragmentDef, name: fragmentDef.name.value, onType: fragmentDef.typeCondition.name.value, isExternal: false, })), ...(config.externalFragments || []), ]; const visitor = new visitor_js_1.TypeScriptDocumentNodesVisitor(schema, allFragments, config, documents); const visitorResult = (0, plugin_helpers_1.oldVisit)(allAst, { leave: visitor }); return { prepend: visitor.getImports(), content: [visitor.fragments, ...visitorResult.definitions.filter(t => typeof t === 'string')].join('\n'), }; }; exports.plugin = plugin; const validate = async (schema, documents, config, outputFile) => { if (!outputFile.endsWith('.ts')) { throw new Error(`Plugin "typescript-document-nodes" requires extension to be ".ts"!`); } }; exports.validate = validate;