UNPKG

@graphql-codegen/gql-tag-operations-preset

Version:
143 lines (142 loc) 6.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.preset = exports.babelPlugin = void 0; const tslib_1 = require("tslib"); const addPlugin = tslib_1.__importStar(require("@graphql-codegen/add")); const gqlTagPlugin = tslib_1.__importStar(require("@graphql-codegen/gql-tag-operations")); const typedDocumentNodePlugin = tslib_1.__importStar(require("@graphql-codegen/typed-document-node")); const typescriptPlugin = tslib_1.__importStar(require("@graphql-codegen/typescript")); const typescriptOperationPlugin = tslib_1.__importStar(require("@graphql-codegen/typescript-operations")); const visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common"); const fragmentMaskingPlugin = tslib_1.__importStar(require("./fragment-masking-plugin.js")); const process_sources_js_1 = require("./process-sources.js"); var babel_js_1 = require("./babel.js"); Object.defineProperty(exports, "babelPlugin", { enumerable: true, get: function () { return tslib_1.__importDefault(babel_js_1).default; } }); exports.preset = { buildGeneratesSection: options => { var _a, _b; // TODO: add link? // eslint-disable-next-line no-console console.warn('DEPRECATED: `gql-tag-operations-preset` is deprecated in favor of `client-preset`.'); /** when not using augmentation stuff must be re-exported. */ const reexports = []; const visitor = new visitor_plugin_common_1.ClientSideBaseVisitor(options.schemaAst, [], options.config, options.config); let fragmentMaskingConfig = null; if (typeof ((_a = options === null || options === void 0 ? void 0 : options.presetConfig) === null || _a === void 0 ? void 0 : _a.fragmentMasking) === 'object') { fragmentMaskingConfig = options.presetConfig.fragmentMasking; } else if (((_b = options === null || options === void 0 ? void 0 : options.presetConfig) === null || _b === void 0 ? void 0 : _b.fragmentMasking) === true) { fragmentMaskingConfig = {}; } const isMaskingFragments = fragmentMaskingConfig != null; const sourcesWithOperations = (0, process_sources_js_1.processSources)(options.documents, node => { if (node.kind === 'FragmentDefinition') { return visitor.getFragmentVariableName(node); } return visitor.getOperationVariableName(node); }); const sources = sourcesWithOperations.map(({ source }) => source); const pluginMap = { ...options.pluginMap, [`add`]: addPlugin, [`typescript`]: typescriptPlugin, [`typescript-operations`]: typescriptOperationPlugin, [`typed-document-node`]: typedDocumentNodePlugin, [`gen-dts`]: gqlTagPlugin, }; const plugins = [ { [`add`]: { content: `/* eslint-disable */` } }, { [`typescript`]: {} }, { [`typescript-operations`]: {} }, { [`typed-document-node`]: {} }, ...options.plugins, ]; const genDtsPlugins = [ { [`add`]: { content: `/* eslint-disable */` } }, { [`gen-dts`]: { sourcesWithOperations } }, ]; let gqlArtifactFileExtension = '.d.ts'; if (options.presetConfig.augmentedModuleName == null) { gqlArtifactFileExtension = '.ts'; reexports.push('gql'); } const config = { ...options.config, inlineFragmentTypes: isMaskingFragments ? 'mask' : options.config['inlineFragmentTypes'], }; let fragmentMaskingFileGenerateConfig = null; if (isMaskingFragments === true) { let fragmentMaskingArtifactFileExtension = '.d.ts'; if (fragmentMaskingConfig.augmentedModuleName == null) { reexports.push('fragment-masking'); fragmentMaskingArtifactFileExtension = '.ts'; } fragmentMaskingFileGenerateConfig = { filename: `${options.baseOutputDir}/fragment-masking${fragmentMaskingArtifactFileExtension}`, pluginMap: { [`fragment-masking`]: fragmentMaskingPlugin, }, plugins: [ { [`fragment-masking`]: {}, }, ], schema: options.schema, config: { useTypeImports: options.config.useTypeImports, augmentedModuleName: fragmentMaskingConfig.augmentedModuleName, unmaskFunctionName: fragmentMaskingConfig.unmaskFunctionName, }, documents: [], documentTransforms: options.documentTransforms, }; } let indexFileGenerateConfig = null; const reexportsExtension = options.config.emitLegacyCommonJSImports ? '' : '.js'; if (reexports.length) { indexFileGenerateConfig = { filename: `${options.baseOutputDir}/index.ts`, pluginMap: { [`add`]: addPlugin, }, plugins: [ { [`add`]: { content: reexports.map(moduleName => `export * from "./${moduleName}${reexportsExtension}"`).join('\n'), }, }, ], schema: options.schema, config: {}, documents: [], documentTransforms: options.documentTransforms, }; } return [ { filename: `${options.baseOutputDir}/graphql.ts`, plugins, pluginMap, schema: options.schema, config, documents: sources, documentTransforms: options.documentTransforms, }, { filename: `${options.baseOutputDir}/gql${gqlArtifactFileExtension}`, plugins: genDtsPlugins, pluginMap, schema: options.schema, config: { ...config, augmentedModuleName: options.presetConfig.augmentedModuleName, gqlTagName: options.presetConfig.gqlTagName || 'gql', }, documents: sources, documentTransforms: options.documentTransforms, }, ...(fragmentMaskingFileGenerateConfig ? [fragmentMaskingFileGenerateConfig] : []), ...(indexFileGenerateConfig ? [indexFileGenerateConfig] : []), ]; }, };