UNPKG

@graphql-codegen/typescript-vue-apollo

Version:

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

32 lines (31 loc) 1.31 kB
import { extname } from 'path'; import { concatAST, Kind } from 'graphql'; import { oldVisit } from '@graphql-codegen/plugin-helpers'; import { VueApolloVisitor } from './visitor.js'; export const plugin = (schema, documents, config) => { const allAst = concatAST(documents.map(s => s.document)); const allFragments = [ ...allAst.definitions.filter(d => d.kind === Kind.FRAGMENT_DEFINITION).map(fragmentDef => ({ node: fragmentDef, name: fragmentDef.name.value, onType: fragmentDef.typeCondition.name.value, isExternal: false, })), ...(config.externalFragments || []), ]; const visitor = new VueApolloVisitor(schema, allFragments, config, documents); const visitorResult = oldVisit(allAst, { leave: visitor }); return { prepend: visitor.getImports(), content: [ visitor.fragments, ...visitorResult.definitions.filter((definition) => typeof definition === 'string'), ].join('\n'), }; }; export const validate = async (_schema, _documents, _config, outputFile) => { if (extname(outputFile) !== '.ts' && extname(outputFile) !== '.tsx') { throw new Error(`Plugin "vue-apollo" requires extension to be ".ts" or ".tsx"!`); } }; export { VueApolloVisitor };