UNPKG

@graphql-codegen/typescript-rtk-query

Version:

GraphQL Code Generator plugin for injecting graphql endpoints into a preconfigured RTK Query api

104 lines (102 loc) 4.66 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RTKQueryVisitor = void 0; const tslib_1 = require("tslib"); const auto_bind_1 = tslib_1.__importDefault(require("auto-bind")); const change_case_all_1 = require("change-case-all"); const visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common"); class RTKQueryVisitor extends visitor_plugin_common_1.ClientSideBaseVisitor { constructor(schema, fragments, rawConfig, documents) { super(schema, fragments, rawConfig, { documentMode: visitor_plugin_common_1.DocumentMode.string, importBaseApiFrom: (0, visitor_plugin_common_1.getConfigValue)(rawConfig.importBaseApiFrom, ''), importBaseApiAlternateName: (0, visitor_plugin_common_1.getConfigValue)(rawConfig.importBaseApiAlternateName, 'api'), addTransformResponse: (0, visitor_plugin_common_1.getConfigValue)(rawConfig.addTransformResponse, false), exportHooks: (0, visitor_plugin_common_1.getConfigValue)(rawConfig.exportHooks, false), overrideExisting: (0, visitor_plugin_common_1.getConfigValue)(rawConfig.overrideExisting, ''), }); this.rawConfig = rawConfig; this._endpoints = []; this._hooks = []; this._externalImportPrefix = this.config.importOperationTypesFrom ? `${this.config.importOperationTypesFrom}.` : ''; this._documents = documents; (0, auto_bind_1.default)(this); } get imports() { return this._imports; } get hasOperations() { return this._collectedOperations.length > 0; } getImports() { const baseImports = super.getImports(); if (!this.hasOperations) { return baseImports; } return [ ...baseImports, `import { ${this.config.importBaseApiAlternateName} } from '${this.config.importBaseApiFrom}';`, ]; } getInjectCall() { if (!this.hasOperations) { return ''; } return (` const injectedRtkApi = ${this.config.importBaseApiAlternateName}.injectEndpoints({ ${!this.config.overrideExisting ? '' : `overrideExisting: ${this.config.overrideExisting}, `}endpoints: (build) => ({${this._endpoints.join('')} }), }); export { injectedRtkApi as api }; ` + (this.config.exportHooks ? `export const { ${this._hooks.join(', ')} } = injectedRtkApi;` : '') + '\n\n'); } injectTransformResponse(Generics) { if (this.config.addTransformResponse) { const responseType = Generics.split(',')[0]; return `transformResponse: (response: ${responseType}) => response`; } return ''; } buildOperation(node, documentVariableName, operationType, operationResultType, operationVariablesTypes, hasRequiredVariables) { var _a, _b; operationResultType = this._externalImportPrefix + operationResultType; operationVariablesTypes = this._externalImportPrefix + operationVariablesTypes; const operationName = (_a = node.name) === null || _a === void 0 ? void 0 : _a.value; if (!operationName) return ''; if (operationType === 'Subscription') { // eslint-disable-next-line no-console console.warn(`Plugin "typescript-rtk-query" does not support GraphQL Subscriptions at the moment! Skipping "${(_b = node.name) === null || _b === void 0 ? void 0 : _b.value}"...`); return ''; } const Generics = `${operationResultType}, ${operationVariablesTypes}${hasRequiredVariables ? '' : ' | void'}`; const operationTypeString = operationType.toLowerCase(); const functionsString = `query: (variables) => ({ document: ${documentVariableName}, variables }) ${this.injectTransformResponse(Generics)}`.trim(); const endpointString = ` ${operationName}: build.${operationTypeString}<${Generics}>({ ${functionsString} }),`; this._endpoints.push(endpointString); if (this.config.exportHooks) { if (operationType === 'Query') { this._hooks.push(`use${(0, change_case_all_1.pascalCase)(operationName)}Query`); this._hooks.push(`useLazy${(0, change_case_all_1.pascalCase)(operationName)}Query`); } if (operationType === 'Mutation') { this._hooks.push(`use${(0, change_case_all_1.pascalCase)(operationName)}Mutation`); } } return ''; } } exports.RTKQueryVisitor = RTKQueryVisitor;