Non-GraphQL requests with 'query' in URL params fail with AbortError
See original GitHub issueDescribe the bug
We have a setup where we talk to both a GraphQL API and some REST APIs as well, thus our server instance has both types of request handlers. If you make a regular REST request with ?query=some-not-graphql-value
in the URL params and your server also has GraphQL request handlers, the first GraphQL handler will attempt to parse the request, which will fail and cause the request to error out.
For us, this was manifesting as AbortError: Aborted
on those requests because it looks like the interceptor library aborts any requests where middleware throws errors. The underlying error causing it to abort the requests is a GraphQL syntax error when trying to parse: GraphQLError: Syntax Error: Unexpected Name ...
.
We were getting this from GET
requests, but after tracing through the code, I think the same is probably true if you tried it with POST
and had query
in the request body that was not a GraphQL query. We saw this behavior using the Node server in Jest unit tests.
Environment
msw: 0.20.5
nodejs: 14.4.0
To Reproduce
Steps to reproduce the behavior:
- Create a server and set it to
.use
any GraphQL request handler (e.g. in Jest). - Create a test that sends a regular fetch
GET
request to a URL with?query=not-graphql
in the URL. - See request fail when GraphQL handler parsing fails.
Expected behavior
The request shouldn’t fail in this case, although I’m unsure of what the correct fix is. It could be as simple as just doing a try...catch
around parsing and then just returning null
so that processing moves on to the next handler if parsing fails. If the “predicate” step (i.e. URL matching I think?) happened before parsing, it would probably be possible for us to workaround it by specifying a URL for our GraphQL endpoint (i.e. use graphql.link
) so that those GraphQL handlers wouldn’t try to parse the request at all.
Happy to help with a PR or however I can.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:9 (4 by maintainers)
Top GitHub Comments
Hi, @pa3. Yes, that’s a valid suggestion. We are currently reworking the request handler API (#561) and it will run the list of handlers exactly as you’ve described:
handlers.filter(handler => handler.test(req))
. Heregraphql.link
will do the URL matching first.handler.run()
after.With those changes,
graphql.link
should be able to resolve this issue. It’s worth mentioning that.test()
will parse first and test the predicate later anyway. That is the right execution order, as you may need additional information from parsing to conclude the predicate phase. It’d be up to the GraphQL request handler to ensure that any given endpoint URL is matched first, before parsing, as you don’t need to parse queries destined to unspecified endpoints.Sounds awesome! Thanks a ton for explanation, @kettanaito!