Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support mocking GraphQL API #39

Closed
kettanaito opened this issue Feb 29, 2020 · 0 comments · Fixed by #45
Closed

Support mocking GraphQL API #39

kettanaito opened this issue Feb 29, 2020 · 0 comments · Fixed by #45
Labels

Comments

@kettanaito
Copy link
Member

kettanaito commented Feb 29, 2020

I suggest that MSW would provide a standard set of request handlers for mocking a GraphQL API.

Motivation

  • GraphQL is an adopted technology, mocking which would be beneficial for users
  • Mock-driven GraphQL development may be a good try-out for the technology, as you don't have to write any server-side code before deciding to fully adopt GraphQL

Features

  • Ability to target GraphQL operations based on type (query/mutation) and name
  • Ability to mock response data and errors according to GraphQL specification

Usage

Mocking a query

import { composeMocks, graphql } from 'msw'

const { start } = composeMocks(
  graphql.query({ operation: 'GetProjectDetail' }, (req, res, ctx) => {
    const { projectPath } = req.variables

    return res(
      ctx.data({
        data: {
          name: 'mocked-project'
        },
      }),
    )
  }),
)

start()

Mocking a mutation

import { composeMocks, graphql } from 'msw'

const { start } = composeMocks(
  graphql.mutation({ operation: 'GetProjectDetail' }, (req, res, ctx) => {
    return res(
      ctx.data({
        data: {
          name: 'mocked-project'
        },
      }),
    )
  }),
)

start()
@kettanaito kettanaito added enhancement help wanted Extra attention is needed labels Feb 29, 2020
@kettanaito kettanaito added feature and removed help wanted Extra attention is needed enhancement labels Mar 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant