@graphql-codegen/typescript-msw
Version:
GraphQL Code Generator plugin for generating MSW mock handlers based on GraphQL operations
29 lines (28 loc) • 1.09 kB
JavaScript
import { extname } from 'path';
import { concatAST, Kind } from 'graphql';
import { oldVisit } from '@graphql-codegen/plugin-helpers';
import { MSWVisitor } 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 MSWVisitor(schema, allFragments, config);
oldVisit(allAst, { leave: visitor });
return {
prepend: visitor.getImports(),
content: visitor.getContent(),
};
};
export const validate = async (schema, documents, config, outputFile) => {
if (extname(outputFile) !== '.ts') {
throw new Error(`Plugin "typescript-msw" requires extension to be ".ts"!`);
}
};
export { MSWVisitor };