Skip to content

Conversation

phryneas
Copy link
Member

This would add the upcoming v2 of msw to the Repo.

I see upsides and downsides here:

Downsides:

  • mocking operations by operation name instead of fieldname seems like a wonky api to me

Upsides:

  • this really uses HttpLink, so we can use it to reproduce errors with HttpLink

I might look into opening a PR for a graphql.schema response handler that would allow us to plug a real GraphQL schema in there.
While this API is shorter than defining a schema, it's also pretty far away from GraphQL.

Base automatically changed from pr/vite to main September 20, 2023 16:29
@kettanaito
Copy link

kettanaito commented Sep 22, 2023

Hi, @phryneas 👋 As I've mentioned on Twitter, you may want to look at this example. You can use the graphql package to resolve any intercepted GraphQL operations against a mock schema even as of now! The simplicity of it makes me think there'd be little practical benefit of introducing something like graphql.schema(), although I've thought about it myself in the past. Perhaps your use cases can prove me wrong, that'd be great.

import { graphql as executeGraphQL, buildSchema } from 'graphql'
import { graphql, HttpResponse } from 'msw'

const schema = buildSchema(`
  type Query {
    greet: String!
  }
`)

graphql.operation(({ query, variables }) => {
  const { errors, data } = await executeGraphQL({
    schema,
    source: query,
    variableValues: variables,
    // Your resolvers here.
    rootValue: {
      greet: () => 'Hello, John!'
    }
  })

  return HttpResponse.json({ errors, data })
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants