1 | import { oldVisit } from '@graphql-codegen/plugin-helpers';
|
2 | import { concatAST, Kind } from 'graphql';
|
3 | import { GraphQLRequestVisitor } from './visitor.js';
|
4 | import { extname } from 'path';
|
5 | export const plugin = (schema, documents, config) => {
|
6 | const allAst = concatAST(documents.map(v => v.document));
|
7 | const allFragments = [
|
8 | ...allAst.definitions.filter(d => d.kind === Kind.FRAGMENT_DEFINITION).map(fragmentDef => ({
|
9 | node: fragmentDef,
|
10 | name: fragmentDef.name.value,
|
11 | onType: fragmentDef.typeCondition.name.value,
|
12 | isExternal: false,
|
13 | })),
|
14 | ...(config.externalFragments || []),
|
15 | ];
|
16 | const visitor = new GraphQLRequestVisitor(schema, allFragments, config);
|
17 | const visitorResult = oldVisit(allAst, { leave: visitor });
|
18 | return {
|
19 | prepend: visitor.getImports(),
|
20 | content: [
|
21 | visitor.fragments,
|
22 | ...visitorResult.definitions.filter(t => typeof t === 'string'),
|
23 | visitor.sdkContent,
|
24 | ].join('\n'),
|
25 | };
|
26 | };
|
27 | export const validate = async (schema, documents, config, outputFile) => {
|
28 | if (extname(outputFile) !== '.ts') {
|
29 | throw new Error(`Plugin "typescript-graphql-request" requires extension to be ".ts"!`);
|
30 | }
|
31 | };
|
32 | export { GraphQLRequestVisitor };
|