question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Non-GraphQL requests with 'query' in URL params fail with AbortError

See original GitHub issue

Describe 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:

  1. Create a server and set it to .use any GraphQL request handler (e.g. in Jest).
  2. Create a test that sends a regular fetch GET request to a URL with ?query=not-graphql in the URL.
  3. 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:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
kettanaitocommented, Jan 27, 2021

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)). Here graphql.link will do the URL matching first.
  • Then relevant handlers will attempt to produce a response with 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.

0reactions
pa3commented, Jan 27, 2021

Sounds awesome! Thanks a ton for explanation, @kettanaito!

Read more comments on GitHub >

github_iconTop Results From Across the Web

RTK Query Error: 'AbortError: Aborted' while running test
I'm trying to run a test on login form submit which goes through a Rest API call to authenticate a user. I have...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found