Make GraphQL handlers endpoint-first #1892
mattcosta7
started this conversation in
RFC
Replies: 1 comment
-
+1 for this change is the fact that |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Description
This is an RFC for a breaking change to handling graphql requests, where
msw
would drop the rawgraphql
handlersgraphql.query
graphql.mutation
andgraphql.operation
in favor of forcing graphql parsing to first callgraphql.link
and define handlers using the scoped methods returned from it.Background and reasoning
The two ways we can mock graphql requests today are shown below
Note, there's a subtle bug here, because all requests that contain a
GetUser
query parsable as a graphql query will trigger this handler, so the more specific handler can never be matched. In this simple example it's easy to spot his error, but in a more real world scenario, it's much less clear why this might happenHere, i'll now always intercept the global
GetUser
, even if it maybe shouldn't because a more specific handler is implemented elsewhere.By deprecating the raw
graphql
handler methods, and forcing calls tographql.link
first, we'll bind http requests to the specific endpoint the graphql server is located onPros/Cons
Pros:
Today, the raw handlers would not help a user uncover this, leading to passing tests and runtime exceptions
Cons:
Anticipated rollout plan
deprecate
the raw handlers in code and in docs, warning users that they should first callgraphql.link(endpoint)
and use the scoped handlers returned from that to define their handlers instead of the raw ones.Footnotes
Particularly on the parse issue, we had to move to logging the errors instead, which don't result in a 500 from the mock, even though it probably should. We had to do this to avoid returning 500s for cases where non-graphql requests were parsed as graphql requests - https://github.com/mswjs/msw/pull/740 ↩
https://github.com/mswjs/msw/pull/1871 ↩
Most users don't handle graphql calls direction, as frameworks extract it, but they do still need to configure an endpoint, somewhere, that requests are made to ↩
Beta Was this translation helpful? Give feedback.
All reactions