@graphql-codegen/typescript-stencil-apollo
Version:
GraphQL Code Generator plugin for generating Stencil Components based on GraphQL operations
33 lines (32 loc) • 1.27 kB
JavaScript
import { extname } from 'path';
import { concatAST, Kind } from 'graphql';
import { oldVisit } from '@graphql-codegen/plugin-helpers';
import { StencilApolloVisitor } 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 StencilApolloVisitor(schema, allFragments, config);
const visitorResult = oldVisit(allAst, { leave: visitor });
return {
prepend: visitor.getImports(),
content: [
'',
visitor.fragments,
...visitorResult.definitions.filter(t => typeof t === 'string'),
].join('\n'),
};
};
export const validate = async (schema, documents, config, outputFile) => {
if (extname(outputFile) !== '.tsx') {
throw new Error(`Plugin "stencil-apollo" requires extension to be ".tsx"!`);
}
};
export { StencilApolloVisitor };