• Plugins
  • TypeScript
  • msw
Package nameWeekly DownloadsVersionLicenseUpdated
@graphql-codegen/typescript-mswDownloadsVersionLicenseSep 26th, 2022

Installation

yarn add -D @graphql-codegen/typescript-msw
⚠️

Usage Requirements In order to use this GraphQL Codegen plugin, please make sure that you have GraphQL operations (query / mutation / subscription and fragment) set as documents: … in your codegen.yml.

Without loading your GraphQL operations (query, mutation, subscription and fragment), you won't see any change in the generated output.

💡
Make sure you have typescript plugin and typescript-operations as well in your configuration:

This plugin generates msw (https://github.com/mswjs/msw) mock handlers with TypeScript typings.

Config API Reference

link

type: object

GraphQL endpoint to use when working with multiple backends.

Usage Examples

  config:
    link:
      name: stripe
      endpoint: https://api.stripe.com/graphql

Usage

Input:

query GetUser($id: ID!) {
  getUser(id: $id) {
    name
    id
  }
}

Usage:

import { mockGetUserQuery } from './generated'
 
const worker = setupWorker(
  mockGetUserQuery((req, res, ctx) => {
    const { id } = req.variables
 
    return res(
      ctx.data({
        getUser: { name: 'John Doe', id }
      })
    )
  })
)
 
worker.start()

The generated functions are named mock<OperationName><OperationType>[LinkName]. E.g., mockGetUserQuery, and mockAdminMutationStripe.

Last updated on August 10, 2022