Msw: Allow mocking graphql by resolver instead of query name

Created on 22 Sep 2020  路  3Comments  路  Source: mswjs/msw

Is your feature request related to a problem? Please describe.
Mocking services is about the data, but graphql in msw is mocked at a higher level than where the data is retrieved. When mocking via query or mutation name which has no defined definition on the graphql server, I find myself sending back the same data in multiple places. Even though two queries may be the exact same, they can have different names.

Describe the solution you'd like
I would like to be able to mock the data at the graphql resolver level. For example:

query GetUsers {
  users {
    id
    name
    email
  }
}

This query would have to be mocked by the name GetUsers.

graphql.query('GetUsers', usersResolver);

At another place in my app, I might have the same query, but name it GetUsersList. I would have to have another query in msw for this, even though it is the same query.

graphql.query('GetUsersList', usersResolver);

I would like to be able to mock by graphql resolver.

graphql.query('users', usersResolver);

Describe alternatives you've considered
N/A

Additional context
N/A

graphql feature

Most helpful comment

Hi, @baker-travis. Thanks for reaching out!

Consider using graphql.operation() request handler to resolve a captured GraphQL operation against any source. See the linked example for reference. Let me know if that satisfies your usage scenario.

All 3 comments

Hi, @baker-travis. Thanks for reaching out!

Consider using graphql.operation() request handler to resolve a captured GraphQL operation against any source. See the linked example for reference. Let me know if that satisfies your usage scenario.

That will work beautifully, not sure how I missed it. Thanks!

@baker-travis, we've rewritten the GraphQL API docs just today, including the new graphql.operation API. You haven't missed it, don't worry!

Was this page helpful?
0 / 5 - 0 ratings