UNPKG

1.91 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.ReactApolloVisitor = 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, "ReactApolloVisitor", { enumerable: true, get: function () { return visitor_js_1.ReactApolloVisitor; } });
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.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};
27exports.plugin = plugin;
28const 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};
38exports.validate = validate;