UNPKG

@graphql-codegen/typescript-react-offix

Version:

GraphQL Code Generator plugin for generating useOffline mutations for offix use

29 lines (28 loc) 1.25 kB
import { extname } from 'path'; import { concatAST, Kind } from 'graphql'; import { oldVisit } from '@graphql-codegen/plugin-helpers'; import { ReactApolloVisitor } from './visitor.js'; export const plugin = (schema, documents, config) => { const allAst = concatAST(documents.map(v => v.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 ReactApolloVisitor(schema, allFragments, config, documents); const visitorResult = oldVisit(allAst, { leave: visitor }); return { prepend: visitor.getImports(), content: [...visitorResult.definitions.filter(t => typeof t === 'string')].join('\n'), }; }; export const validate = async (_schema, _documents, _config, outputFile) => { if (extname(outputFile) !== '.tsx' && extname(outputFile) !== '.ts') { throw new Error(`Plugin "react-apollo" requires extension to be ".tsx" or ".ts!`); } }; export { ReactApolloVisitor };