UNPKG

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