UNPKG

1.3 kBJavaScriptView Raw
1import { oldVisit } from '@graphql-codegen/plugin-helpers';
2import { concatAST, Kind } from 'graphql';
3import { GraphQLRequestVisitor } from './visitor.js';
4import { extname } from 'path';
5export 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};
27export 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};
32export { GraphQLRequestVisitor };