-
-
Notifications
You must be signed in to change notification settings - Fork 551
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rest Api from Cubejs gets mistaken as GraphQL when using both in one handler #1184
Comments
Hey, @brantleyenglish. Thanks for reporting this! I've posted an update on the way to tell apart GraphQL requests reliably here: #535 (comment) This really moves the discussion (and the correct solution to this issue) to the GraphQL community. I'd stay in the loop to see how I can help. I'd love to merge a fix for this in a form of relying on the |
Wow, this is very cool 😮 !! Thank you for taking a look into my issue and suggesting the improvement. |
Looks like the URQL community is for this change. We've teamed up to update existing GraphQL server implementations to make sure they can praise |
Sadly, the GraphQL community is tentative in adopting the GraphQL over HTTP spec as of now. I'm conflicted here: I don't wish to add constant workarounds which will never work reliably because there's no reliable way to distinguish a GraphQL request. |
The only approach I see right now, until all the clients respect msw/src/utils/internal/parseGraphQLRequest.ts Lines 161 to 172 in 66c3ad8
Pros:
Cons:
Currently, there's nothing I can recommend to circumvent the runtime exception apart from adjusting your request query/body not to resemble that of GraphQL. That is an intrusive suggestion and that's why I'm not expecting you to apply it. I wanted to suggest the following thing initially:
export const handlers = [
rest.post('/cubejs-api/v1/load', (req, res, ctx) => {
req.url.searchParams.get('query')
// Make sure to handle this request!
return res(ctx.json({ hello: 'world' }))
}),
graphql.query('AnyQuery', resolver)
]
But then I've learned that request parsing happens before handlers are run (during msw/src/handlers/GraphQLHandler.ts Line 160 in 66c3ad8
Because request matching may require information otherwise unavailable in the request without an additional parsing step, such as operation type/name of a GraphQL request or path parameters of a REST request. So the suggestion above is irrelevant. |
Hello there :) I was wondering whether you had any workaround for REST calls with a |
I believe that if you define the rest handlers before the graphql handlers it should be doable. the bigger annoyance is using |
@brantleyenglish et al In version 2.0.9 we shipped #1871 which should help differentiate graphql from rest requests and avoid the That version allows us to bail out of request parsing early if the endpoint is not specifically designated as a graphql endpoing when using To modify existing code: import {graphql, http} from 'msw'
+ const exampleGraphqlApi = graphql.link("https://example.com/graphql")
const worker = setupWorker(
- graphql.query('GetUser', ...),
+ exampleGraphqlApi.query('GetUser', ...),
http.post("https://example.com/users", ({ request }) => {
const {query} = await request.json()
})
) Before, we'd parse requests to Using that version or later, and |
Prerequisites
Environment check
msw
versionNode.js version
14.17.5
Reproduction repository
https://github.com/brantleyenglish/msw-test
Reproduction steps
Run
yarn
thenyarn test
Current behavior
Currently I am getting this error, and the mocked rest endpoint does not get populated with mock data.
This url should be parsed as a rest endpoint. This is not a graphql query.
Expected behavior
I expect the test to pass the
resultSet
to return some form of data.The text was updated successfully, but these errors were encountered: