UNPKG

@graphql-codegen/client-preset

Version:
153 lines (152 loc) • 7.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.babelOptimizerPlugin = exports.preset = 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 babel_js_1 = tslib_1.__importDefault(require("./babel.js")); exports.babelOptimizerPlugin = babel_js_1.default; const fragmentMaskingPlugin = tslib_1.__importStar(require("./fragment-masking-plugin.js")); const process_sources_js_1 = require("./process-sources.js"); const isOutputFolderLike = (baseOutputDir) => baseOutputDir.endsWith('/'); exports.preset = { prepareDocuments: (outputFilePath, outputSpecificDocuments) => [...outputSpecificDocuments, `!${outputFilePath}`], buildGeneratesSection: options => { var _a, _b; if (!isOutputFolderLike(options.baseOutputDir)) { throw new Error('[client-preset] target output should be a directory, ex: "src/gql/"'); } if (options.plugins.length > 0 && Object.keys(options.plugins).some(p => p.startsWith('typescript'))) { throw new Error('[client-preset] providing typescript-based `plugins` with `preset: "client" leads to duplicated generated types'); } const reexports = []; // the `client` preset is restricting the config options inherited from `typescript`, `typescript-operations` and others. const forwardedConfig = { scalars: options.config.scalars, defaultScalarType: options.config.defaultScalarType, strictScalars: options.config.strictScalars, namingConvention: options.config.namingConvention, useTypeImports: options.config.useTypeImports, skipTypename: options.config.skipTypename, arrayInputCoercion: options.config.arrayInputCoercion, enumsAsTypes: options.config.enumsAsTypes, dedupeFragments: options.config.dedupeFragments, nonOptionalTypename: options.config.nonOptionalTypename, avoidOptionals: options.config.avoidOptionals, }; 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) !== false) { // `true` by default 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 } }, ]; const gqlArtifactFileExtension = '.ts'; reexports.push('gql'); const config = { ...options.config, inlineFragmentTypes: isMaskingFragments ? 'mask' : options.config['inlineFragmentTypes'], }; let fragmentMaskingFileGenerateConfig = null; if (isMaskingFragments === true) { const fragmentMaskingArtifactFileExtension = '.ts'; reexports.push('fragment-masking'); fragmentMaskingFileGenerateConfig = { filename: `${options.baseOutputDir}fragment-masking${fragmentMaskingArtifactFileExtension}`, pluginMap: { [`fragment-masking`]: fragmentMaskingPlugin, }, plugins: [ { [`fragment-masking`]: {}, }, ], schema: options.schema, config: { useTypeImports: options.config.useTypeImports, unmaskFunctionName: fragmentMaskingConfig.unmaskFunctionName, }, documents: [], }; } 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: [], }; } return [ { filename: `${options.baseOutputDir}graphql.ts`, plugins, pluginMap, schema: options.schema, config: { inlineFragmentTypes: isMaskingFragments ? 'mask' : options.config['inlineFragmentTypes'], ...forwardedConfig, }, documents: sources, }, { filename: `${options.baseOutputDir}gql${gqlArtifactFileExtension}`, plugins: genDtsPlugins, pluginMap, schema: options.schema, config: { ...config, gqlTagName: options.presetConfig.gqlTagName || 'graphql', }, documents: sources, }, ...(fragmentMaskingFileGenerateConfig ? [fragmentMaskingFileGenerateConfig] : []), ...(indexFileGenerateConfig ? [indexFileGenerateConfig] : []), ]; }, };