fix(GraphQL): support application/graphql-response+json response content-type#2513
Conversation
| 'application/json', | ||
| ] | ||
|
|
||
| const wrappedResolver: typeof resolver = (info) => { |
There was a problem hiding this comment.
I'm sure there's a more elegant way to do this that I'm just not aware of....
|
Hi, @phryneas. Thanks for opening this! It would help a lot if we started from the intended usage experience. How do you see people using MSW to mock queries done via the GraphQL-over-HTTP spec? Let's design the ideal usage and then work down from there to see what implementation that would require. From my limited understanding, all that should happen is that mocked responses from |
application/graphql-response+json requests
UpdateThanks for proposing this, @phryneas. I've refactored your approach, added tests, and now they are passing. Here's an overview of what I did.
The behavior regarding the
The response content type is fully controlled by the client, never the mock. The mock is basically a server and it responds with whatever content type the client says it accepts and prefers. @phryneas, could you give this change a quick review to see if it complies with the expectations behind the new content type header? I'd be most grateful! |
application/graphql-response+json requestsapplication/graphql-response+json response content-type
kettanaito
left a comment
There was a problem hiding this comment.
This looks good to me. Would love to know what you think before releasing it.
|
It generally looks good, but as you removed the GraphQLHandler options, it's now impossible to mock an "old" server that didn't support One additional question I have after re-reading this after all that time: is it possible to mock these responses with a different response code? Where |
You can always do that by providing an explicit graphql.query('GetOldServer', () => {
return HttpResponse.json({
data: { ... }
}, {
headers: {
// Regardless of what the client accepts, use this mime type.
'content-type': 'application/json'
}
})
})We just need to add a check for explicit
This again sounds like what you should do in your handlers. MSW will not interfere if you decide to respond with a different It's the same mental model as when it comes to putting business logic in your handlers. Don't. it('handles unauthorized users', () => {
server.use(
http.get('/resource', () => {
// Any request will be unauthorized.
return new HttpResponse(null, { status: 401 })
})
)
})You don't check for what |
|
With those responses, approved ✅ |
|
Alright, pushed test + support for ignoring this logic when an explicit |
Released: v2.12.13 🎉This has been released in v2.12.13. Get these changes by running the following command: Predictable release automation by Release. |
This would add support for the GraphQL over HTTP spec: https://github.com/graphql/graphql-over-http/blob/main/spec/GraphQLOverHTTP.md#accept
The spec introduces a new mime type that future severs should be able to handle -
'application/graphql-response+json'instead of the traditional'application/json'.So right now, all tests using
HttpResponse.jsonwould need to switch over to a new constructing function.My suggestion here would be to just have
GraphQLHandlerreturn based on a list ofsupportedMimeTypesand the priority specified by the GraphQL client.Clients requesting
'application/json'would be served'application/json'while clients requesting"application/graphql-response+json, application/json;q=0.9"would be served'application/graphql-response+json'.Note that it would be possible to still always return
'application/json'and simulate a server that only speaks the old protocol by specifying asupportedMimeTypesof['application/json'].This is still lacking tests, but there is a good chance you want completely different behaviour so I'm opening this as-is for discussion.