1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.TypeScriptDocumentsVisitor = exports.plugin = void 0;
|
4 | const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers");
|
5 | const visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common");
|
6 | const graphql_1 = require("graphql");
|
7 | const visitor_js_1 = require("./visitor.js");
|
8 | Object.defineProperty(exports, "TypeScriptDocumentsVisitor", { enumerable: true, get: function () { return visitor_js_1.TypeScriptDocumentsVisitor; } });
|
9 | const plugin = (schema, rawDocuments, config) => {
|
10 | const documents = config.flattenGeneratedTypes
|
11 | ? (0, visitor_plugin_common_1.optimizeOperations)(schema, rawDocuments, {
|
12 | includeFragments: config.flattenGeneratedTypesIncludeFragments,
|
13 | })
|
14 | : rawDocuments;
|
15 | const allAst = (0, graphql_1.concatAST)(documents.map(v => v.document));
|
16 | const allFragments = [
|
17 | ...allAst.definitions.filter(d => d.kind === graphql_1.Kind.FRAGMENT_DEFINITION).map(fragmentDef => ({
|
18 | node: fragmentDef,
|
19 | name: fragmentDef.name.value,
|
20 | onType: fragmentDef.typeCondition.name.value,
|
21 | isExternal: false,
|
22 | })),
|
23 | ...(config.externalFragments || []),
|
24 | ];
|
25 | const visitor = new visitor_js_1.TypeScriptDocumentsVisitor(schema, config, allFragments);
|
26 | const visitorResult = (0, plugin_helpers_1.oldVisit)(allAst, {
|
27 | leave: visitor,
|
28 | });
|
29 | let content = visitorResult.definitions.join('\n');
|
30 | if (config.addOperationExport) {
|
31 | const exportConsts = [];
|
32 | allAst.definitions.forEach(d => {
|
33 | if ('name' in d) {
|
34 | exportConsts.push(`export declare const ${d.name.value}: import("graphql").DocumentNode;`);
|
35 | }
|
36 | });
|
37 | content = visitorResult.definitions.concat(exportConsts).join('\n');
|
38 | }
|
39 | if (config.globalNamespace) {
|
40 | content = `
|
41 | declare global {
|
42 | ${content}
|
43 | }`;
|
44 | }
|
45 | return {
|
46 | prepend: [...visitor.getImports(), ...visitor.getGlobalDeclarations(visitor.config.noExport)],
|
47 | content,
|
48 | };
|
49 | };
|
50 | exports.plugin = plugin;
|