1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.ReactApolloVisitor = exports.validate = exports.plugin = void 0;
|
4 | const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers");
|
5 | const graphql_1 = require("graphql");
|
6 | const visitor_js_1 = require("./visitor.js");
|
7 | Object.defineProperty(exports, "ReactApolloVisitor", { enumerable: true, get: function () { return visitor_js_1.ReactApolloVisitor; } });
|
8 | const path_1 = require("path");
|
9 | const 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.ReactApolloVisitor(schema, allFragments, config, documents);
|
21 | const visitorResult = (0, plugin_helpers_1.oldVisit)(allAst, { leave: visitor });
|
22 | return {
|
23 | prepend: visitor.getImports(),
|
24 | content: [visitor.fragments, ...visitorResult.definitions.filter(t => typeof t === 'string')].join('\n'),
|
25 | };
|
26 | };
|
27 | exports.plugin = plugin;
|
28 | const validate = async (schema, documents, config, outputFile) => {
|
29 | if (config.withComponent === true) {
|
30 | if ((0, path_1.extname)(outputFile) !== '.tsx') {
|
31 | throw new Error(`Plugin "typescript-react-apollo" requires extension to be ".tsx" when withComponent: true is set!`);
|
32 | }
|
33 | }
|
34 | else if ((0, path_1.extname)(outputFile) !== '.ts' && (0, path_1.extname)(outputFile) !== '.tsx') {
|
35 | throw new Error(`Plugin "typescript-react-apollo" requires extension to be ".ts" or ".tsx"!`);
|
36 | }
|
37 | };
|
38 | exports.validate = validate;
|