UNPKG

@graphql-codegen/typescript-msw

Version:

GraphQL Code Generator plugin for generating MSW mock handlers based on GraphQL operations

77 lines (76 loc) 3.66 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MSWVisitor = 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 graphql_1 = require("graphql"); const visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common"); class MSWVisitor extends visitor_plugin_common_1.ClientSideBaseVisitor { constructor(schema, fragments, rawConfig) { super(schema, fragments, rawConfig, { link: (0, visitor_plugin_common_1.getConfigValue)(rawConfig.link, undefined) }); this._operationsToInclude = []; (0, auto_bind_1.default)(this); this._externalImportPrefix = this.config.importOperationTypesFrom ? `${this.config.importOperationTypesFrom}.` : ''; } getImports() { const hasOperations = this._collectedOperations.length > 0; if (!hasOperations) { return []; } return [`import { graphql, ResponseResolver, GraphQLRequest, GraphQLContext } from 'msw'`]; } getContent() { const { link } = this.config; let endpoint; if (link) { endpoint = `const ${link.name} = graphql.link('${link.endpoint}')\n`; } const suffix = (0, change_case_all_1.pascalCase)((link === null || link === void 0 ? void 0 : link.name) || ''); const operations = this._operationsToInclude.map(({ node, operationType, operationResultType, operationVariablesTypes }) => { if (operationType === 'Query' || operationType === 'Mutation') { const handlerName = `mock${(0, change_case_all_1.pascalCase)(node.name.value)}${operationType}${suffix}`; /** @ts-expect-error name DOES exist on @type{import('graphql').SelectionNode} */ const selections = node.selectionSet.selections.map(sel => sel.name.value).join(', '); const variables = node.variableDefinitions.map(def => def.variable.name.value).join(', '); return `/** * @param resolver a function that accepts a captured request and may return a mocked response. * @see https://mswjs.io/docs/basics/response-resolver * @example * ${handlerName}((req, res, ctx) => {${variables && `\n * const { ${variables} } = req.variables;`} * return res( * ctx.data({ ${selections} }) * ) * }) */ export const ${handlerName} = (resolver: ResponseResolver<GraphQLRequest<${operationVariablesTypes}>, GraphQLContext<${operationResultType}>, any>) => ${(link === null || link === void 0 ? void 0 : link.name) || 'graphql'}.${operationType.toLowerCase()}<${operationResultType}, ${operationVariablesTypes}>( '${node.name.value}', resolver )\n`; } return ''; }); return [endpoint, ...operations].join('\n'); } buildOperation(node, documentVariableName, operationType, operationResultType, operationVariablesTypes) { operationResultType = this._externalImportPrefix + operationResultType; operationVariablesTypes = this._externalImportPrefix + operationVariablesTypes; if (node.name == null) { throw new Error("Plugin 'msw' cannot generate mocks for unnamed operation.\n\n" + (0, graphql_1.print)(node)); } else { this._operationsToInclude.push({ node, documentVariableName, operationType, operationResultType, operationVariablesTypes, }); } return null; } } exports.MSWVisitor = MSWVisitor;