1 | import { oldVisit } from '@graphql-codegen/plugin-helpers';
|
2 | import { concatAST, Kind } from 'graphql';
|
3 | import { extname } from 'path';
|
4 | import { GenericSdkVisitor } from './visitor.js';
|
5 | export 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 | };
|
29 | export 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 | };
|
34 | export { GenericSdkVisitor };
|