Schema-first GraphQL mocking #1751
Replies: 2 comments 9 replies
-
Some of the current questions I have about this proposal:
|
Beta Was this translation helpful? Give feedback.
-
This is something that we didn't really touch on in our call, but that is actually super important: So this should probably be split up in
At that point, also the offer from our side: We would be very happy to flesh out & maintain the
Yeah, graphql-codegen can generate all the types we need here.
I would keep this as a "lowest level" handler, and people could still use the existing
There isn't really a 100% standardized form for those in the first place, so we could provide a
💯! That's why I want this to resemble "real resolvers" as closely as possible - just without the need to create every last resolver, but to automock the bulk of them. |
Beta Was this translation helpful? Give feedback.
-
Although you can use the existing
graphql.operation()
handler + thegraphql
package to resolve any query against a mock schema, this approach has a number issues:In this regard, I propose to kick off this discussion to improve the schema-firsts mocking in GraphQL.
The goal
To make GraphQL testing easier in existing apps. Mocking operation-first is beneficial when prototyping as you don't have to invest in schemas/resolvers/etc. but when testing an existing apps, the operation-based approach doesn't scale that well. This is where a schema-based approach is preferable.
Existing apps would also likely have the schema defined in some shape or form, so reusing that schema could make the mocking faster and error-proof.
Initial proposal
I think it'd be good to add a new
graphql.schema
request handler.This API may act as a fallback definition for your schema (mocked or not) with the default mock data described in the second argument (essentially,
rootValue
). This API may also return a subset of thegraphql
API in MSW to allow the developer to override particular types/scalars/resolvers/operations on a per-test basis.For example, if I want for
User.name
to return a different value in a particular test, I can do this:We can have things like
.field()
,.query()
and.mutation()
(if I want to override the entire operation but still use the fallbackgraphql.schema
to resolve the fields I haven't provided in the override.Core principles
Like the rest of MSW, I want this API to resemble the way you'd actually implement GraphQL. This means nailing this tricky balance between the accessibility of the mocks and the relevance of the code you produce. The more we rely/replicate the principles present in GraphQL development (maybe even some of the APIs used), the more beneficial this API becomes to the developer in terms of their effort investment.
Beta Was this translation helpful? Give feedback.
All reactions