Skip to content

REST query is mistaken for GraphQL query and can't be intercepted (throws an error) #732

Closed

Description

@thomasmery

Environment

Name Version
msw 0.28.2
browser Chrome
OS Mac OS X Catalina

Request handlers

no request handler for the following rest request

https://api.dev/artists?query=Petit+Biscuit&page=0&limit=5&workspaceId=ce54f030-2191-40b1-8b2a-cd5167e9ec5f

Actual request

const results = await Axios.get('/artists', {
    params: {
      query,
      page,
      limit,
      workspaceId,
      onlyBelieveArtists,
    },
  });

Current behavior

Browser console throws an error :

[MSW] Failed to intercept a GraphQL request to "GET https://api.dev/artists": cannot parse query. See the error message from the parser below.

Expected behavior

no error shoud be thrown as it is not a GraphQL request

this happens in

parseGraphQLRequest method

seemingly because it relies on the presence of a query param to start and parse the query

should this method even be called? maybe it should as no rest handler is provided it also tries to find a graphQL handler

thanks for any input on this

Activity

  1. added
    bug
    scope:browser
    on May 12, 2021
  2. kettanaito commented on May 12, 2021

    @kettanaito
    Member

    Hey, @thomasmery.

    Do you have any request handlers defined at all? If so, could you please post them?

    Looks like the issue happens when MSW attempts to call onUnhandledRequest for this request for which you have no request handler defined:

    const parsedGraphQLQuery = parseGraphQLRequest(request)

    In order to provide meaningful handler suggestions, it first needs to parse the request as REST/GraphQL. I believe in the case of running under onUnhandledRequest, it shouldn't throw an exception on invalid query, because the request type is ambiguous at this point (whereas when run in a GraphQL handler we know you're expecting GraphQL, so throwing on an invalid query is a viable behavior).

    We could benefit from wrapping the parseGraphQLRequest call in a function that'd silence any exceptions raised, because they are not relevant in the current execution context:

    const parsedGraphQLQuery = tryCatch(() => parseGraphQLRequest(request))

    And the utility would be:

    export function tryCatch<Payload>(fn: () => Payload): Payload | null {
      try {
        const payload = fn()
        return payload
      } catch (error) {
        return null
      }
    }

    Please, would you be interested in opening a pull request with this fix? We could use your help.

  3. thomasmery commented on May 12, 2021

    @thomasmery
    ContributorAuthor

    Hi @kettanaito

    thanks for the quick reply

    I actually made a mistake when reporting but the error is not thrown but only printed to the console

    export function parseGraphQLRequest(

    makes this check

     if (!input || !input.query) {
        return undefined
      }

    that allows the parsing to occur when query param is present

    if it can't parse it just says so and I believe the rest of the handler selection can happen

    I'd be happy to help but I confess I don't know why

    const parsedGraphQLQuery = parseGraphQLRequest(request)

    is made before the rest of the handler selection, at this stage I don't know if it is possible to tell that a request is definitely not a graphQL request and should not be parsed

    the error printing is just confusing when you're not using GraphQL, even if it does not seem to cause a pb

    FYI my handlers resemble this one

    const handlers = [
      rest.get(`${apiBaseUrl}/products/capi/album/:albumId`, (req, res, ctx) => {
        const mockedResponse = fixtures.products;
        return res(ctx.status(200), ctx.json(mockedResponse));
      }),
    ];
  4. kettanaito commented on May 12, 2021

    @kettanaito
    Member

    Your request has no matching request handler, so MSW attempts to handle it via onUnhandledRequest. That's only to produce a warning/error if you set it up to do so. It also attempts to parse all unhandled requests to provide you with smart suggestions based on your existing handlers (maybe you made a typo). It's during that step that it fails because it assumes it can safely call parseGraphQLRequest on an arbitrary request, which isn't the case.

    That's why I'm suggesting to wrap the parseGraphQLRequest call only in the onUnhandledRequest function to ignore any thrown exceptions/errors. If the parsing uses console.error to print out the parsing error, I'd argue it should throw instead. That way its exceptions can be handled in the parental scope.

  5. thomasmery commented on May 12, 2021

    @thomasmery
    ContributorAuthor

    thanks,

    makes sense if I turn the logging into a throwing

    I'll try and open a PR by the end of the week :)

  6. kettanaito commented on May 12, 2021

    @kettanaito
    Member

    We can try to

    1. Throw an exception instead of logging an error.
    2. Wrap the parseGraphQLRequest call in the onUnhandledRequest function in something like maybe or tryCatch custom function that would silence any exceptions thrown.

    Thank you for your support!

  7. locked and limited conversation to collaborators on Nov 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugscope:browser

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions