TypeScript Msw
TypeScript-MSW - Mock GraphQL requests with MSW.
- Version
- 4.0.1
- Weekly downloads
- 153k
- License
- MIT
- Updated
- Apr 19, 2026
Installation
npm i -D @graphql-codegen/typescript-msw
yarn add -D @graphql-codegen/typescript-msw
pnpm add -D @graphql-codegen/typescript-msw
bun add -D @graphql-codegen/typescript-msw
Config API Reference
link
type: object
GraphQL endpoint to use when working with multiple backends.
MSW is a powerful tool for mocking HTTP requests in your tests and development environment. This plugin generates typed mock function wrappers for your GraphQL operations.
The generated functions are named with the pattern: mock<OperationName><OperationType>[LinkName].
As an example, a query with the name getUser would generate a mock function mockGetUserQuery. If
you have links configured for multiple endpoints, the link name will be appended to the end. A link
configured for the Stripe API, for example, would generate a mock name mockAdminMutationStripe.
See below for more examples.
Configuration
To use this plugin, you need the typescript plugin and typescript-operations in your
configuration.
If you are using the client
you can simply add the typescript-msw plugin to your configuration:
Usage
The examples below assume the following GraphQL schema with a getUser query and a setEmail
mutation.
Mocking a query response
Mocking an error response
Accessing other resolver argument properties
The resolver function receives an object with a number of properties. The most up to date
documentation is going to be in the
msw docs site.
Migrating from MSW 1.x to 2.x
MSW 2.x introduced a number of breaking changes. The types from msw are not compatible between the
two versions so to upgrade, you will have to make some changes.
Updating the resolver function
You will need to update callback signatures to use the new resolver argument. In addition, the
returned result must be a Response object.
After:
Replacing the res.once method
In addition to the changes above, since the res object does not get passed to the resolver, you
will need to adjust. To achieve the same behavior, you can pass a second “options” parameter to the
mock function.