UNPKG

@graphql-codegen/typescript-vue-apollo

Version:

GraphQL Code Generator plugin for generating ready-to-use Vue-Apollo composition functions based on GraphQL operations

37 lines (36 loc) 1.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.VueApolloVisitor = exports.validate = exports.plugin = void 0; const path_1 = require("path"); const graphql_1 = require("graphql"); const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers"); const visitor_js_1 = require("./visitor.js"); Object.defineProperty(exports, "VueApolloVisitor", { enumerable: true, get: function () { return visitor_js_1.VueApolloVisitor; } }); const plugin = (schema, documents, config) => { const allAst = (0, graphql_1.concatAST)(documents.map(s => s.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.VueApolloVisitor(schema, allFragments, config, documents); const visitorResult = (0, plugin_helpers_1.oldVisit)(allAst, { leave: visitor }); return { prepend: visitor.getImports(), content: [ visitor.fragments, ...visitorResult.definitions.filter((definition) => typeof definition === 'string'), ].join('\n'), }; }; exports.plugin = plugin; const validate = async (_schema, _documents, _config, outputFile) => { if ((0, path_1.extname)(outputFile) !== '.ts' && (0, path_1.extname)(outputFile) !== '.tsx') { throw new Error(`Plugin "vue-apollo" requires extension to be ".ts" or ".tsx"!`); } }; exports.validate = validate;