UNPKG

@graphql-codegen/typescript-msw

Version:

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

88 lines (87 loc) 4.28 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, type GraphQLResponseResolver, type RequestHandlerOptions } from 'msw'`, ]; } getContent() { var _a; 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 withSuffix = (_a = link === null || link === void 0 ? void 0 : link.withSuffix) !== null && _a !== void 0 ? _a : true; 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}${withSuffix ? 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 [resolver arguments](https://mswjs.io/docs/api/graphql#resolver-argument) and must always return the instruction on what to do with the intercepted request. ([see more](https://mswjs.io/docs/concepts/response-resolver#resolver-instructions)) * @param options Options object to customize the behavior of the mock. ([see more](https://mswjs.io/docs/api/graphql#handler-options)) * @see https://mswjs.io/docs/basics/response-resolver * @example * ${handlerName}( * ({ query, variables }) => {${variables && ` * const { ${variables} } = variables;`} * return HttpResponse.json({ * data: { ${selections} } * }) * }, * requestOptions * ) */ export const ${handlerName} = (resolver: GraphQLResponseResolver<${operationResultType}, ${operationVariablesTypes}>, options?: RequestHandlerOptions) => ${(link === null || link === void 0 ? void 0 : link.name) || 'graphql'}.${operationType.toLowerCase()}<${operationResultType}, ${operationVariablesTypes}>( '${node.name.value}', resolver, options )\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;