UNPKG

1.71 kBJavaScriptView Raw
1import { oldVisit } from '@graphql-codegen/plugin-helpers';
2import { concatAST, Kind } from 'graphql';
3import { InputTypeVisitor } from './input-type-visitor.js';
4import { OperationVisitor } from './operation-visitor.js';
5import { FileType } from './file-type.js';
6import { CustomTypeClassVisitor } from './custom-type-class.js';
7export const plugin = (schema, documents, config) => {
8 const allAst = concatAST(documents.map(v => v.document));
9 const allFragments = [
10 ...allAst.definitions.filter(d => d.kind === Kind.FRAGMENT_DEFINITION).map(fragmentDef => ({
11 node: fragmentDef,
12 name: fragmentDef.name.value,
13 onType: fragmentDef.typeCondition.name.value,
14 isExternal: false,
15 })),
16 ...(config.externalFragments || []),
17 ];
18 let visitor;
19 switch (config.fileType) {
20 case FileType.FRAGMENT:
21 case FileType.OPERATION: {
22 visitor = new OperationVisitor(schema, config, allFragments);
23 break;
24 }
25 case FileType.INPUT_TYPE: {
26 visitor = new InputTypeVisitor(schema, config);
27 break;
28 }
29 case FileType.CUSTOM_TYPES: {
30 visitor = new CustomTypeClassVisitor(schema, config);
31 break;
32 }
33 }
34 if (!visitor) {
35 return { content: '' };
36 }
37 const visitResult = oldVisit(allAst, visitor);
38 const additionalContent = visitor.additionalContent();
39 const imports = visitor.getImports();
40 return {
41 prepend: [`package ${visitor.getPackage()};\n`, ...imports],
42 content: '\n' + [...visitResult.definitions.filter(a => a && typeof a === 'string'), additionalContent].join('\n'),
43 };
44};