UNPKG

@graphql-codegen/typescript-rtk-query

Version:

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

89 lines (87 loc) 3.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RTKQueryVisitor = void 0; const tslib_1 = require("tslib"); const visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common"); const change_case_all_1 = require("change-case-all"); const auto_bind_1 = tslib_1.__importDefault(require("auto-bind")); 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, ''), 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 { api } from '${this.config.importBaseApiFrom}';`]; } getInjectCall() { if (!this.hasOperations) { return ''; } return (` const injectedRtkApi = api.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'); } 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 ''; const Generics = `${operationResultType}, ${operationVariablesTypes}${hasRequiredVariables ? '' : ' | void'}`; if (operationType === 'Query') { this._endpoints.push(` ${operationName}: build.query<${Generics}>({ query: (variables) => ({ document: ${documentVariableName}, variables }) }),`); if (this.config.exportHooks) { this._hooks.push(`use${(0, change_case_all_1.pascalCase)(operationName)}Query`); this._hooks.push(`useLazy${(0, change_case_all_1.pascalCase)(operationName)}Query`); } } else if (operationType === 'Mutation') { this._endpoints.push(` ${operationName}: build.mutation<${Generics}>({ query: (variables) => ({ document: ${documentVariableName}, variables }) }),`); if (this.config.exportHooks) { this._hooks.push(`use${(0, change_case_all_1.pascalCase)(operationName)}Mutation`); } } else 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 ''; } } exports.RTKQueryVisitor = RTKQueryVisitor;