UNPKG

1.33 kBJavaScriptView Raw
1import { oldVisit } from '@graphql-codegen/plugin-helpers';
2import { concatAST, Kind } from 'graphql';
3import { extname } from 'path';
4import { GenericSdkVisitor } from './visitor.js';
5export const plugin = (schema, documents, config) => {
6 const allAst = concatAST(documents.reduce((prev, v) => {
7 return [...prev, v.document];
8 }, []));
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 const visitor = new GenericSdkVisitor(schema, allFragments, config);
19 const visitorResult = oldVisit(allAst, { leave: visitor });
20 return {
21 prepend: visitor.getImports(),
22 content: [
23 visitor.fragments,
24 ...visitorResult.definitions.filter(t => typeof t === 'string'),
25 visitor.sdkContent,
26 ].join('\n'),
27 };
28};
29export const validate = async (schema, documents, config, outputFile) => {
30 if (extname(outputFile) !== '.ts') {
31 throw new Error(`Plugin "typescript-generic-sdk" requires extension to be ".ts"!`);
32 }
33};
34export { GenericSdkVisitor };