UNPKG

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