UNPKG

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