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.

`graphql.operation` cannot intercept the unnamed graphql

See original GitHub issue

Environment

Name Version
msw 0.35.0
browser Chrome v93.0.4577.82
OS MacOS v11.5.2

Request handlers

import { setupWorker, rest } from 'msw'

const worker = setupWorker(
  graphql.operation((res, req, ctx) => {
    // ...
  })
)

worker.start()

Actual request

import { request, gql } from "graphql-request";

const query = gql`
  {
    hero {
      name
      friends {
        name
      }
    }
  }
`;

request("/", query)

Current behavior

The handler didn’t work, and there is a warning in the console:

[MSW] Failed to intercept a GraphQL request at "POST /": unnamed GraphQL operations are not supported.

Consider naming this operation or using "graphql.operation" request handler to intercept GraphQL requests regardless of their operation name/type. Read more: https://mswjs.io/docs/api/graphql/operation    

Note that using rest.post is workable:

rest.post(/\//, (res, req, ctx) => {
   // ...
})

Expected behavior

The handlers should works.

Screenshots

Here is a reproducible codesandbox.

Finally, thanks for the fantastic library 🙌. MSW helps my daily work a lot! Please let me know if there is anything I can help with this issue.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
kettanaitocommented, Sep 17, 2021

Hey, @andykao1213. Thank you for reporting this!

We are aware of this issue and collaborating with @neilime who has already implemented a fix! Please await the next release.

0reactions
R4YM3commented, May 23, 2022

For others who find this, i had this issue aswell. It did a mutation like this:

updateSomething(input: $input){
  foo
  bar
}

but it should have been:

mutation UpdateSomething($input: UpdateSomethingInput){
  updateSomething(input: $input){
    foo
    bar
  }
}

Regards

Read more comments on GitHub >

github_iconTop Results From Across the Web

Full Stack Error Handling with GraphQL and Apollo
This effectively intercepts the old // failed request, and retries it with a new token return forward(operation); // handle other errors ...
Read more >
msw: graphql operation doesn't get intercepted - Stack Overflow
Using MWS, I'm trying to mock the graphql operation testRequest as follows: Funny thing is, the console.log in the ...
Read more >
Introspection - GraphQL
A query language for your API — GraphQL provides a complete description of the data in your API, gives clients the power to...
Read more >
GraphQL Step Last Updated December 15, 2022 - TechDocs
schema-based error checking. If you select the content type as application/graphql, you cannot use the GraphQL variables and the operation Name. GraphQL ......
Read more >
Multiple GraphQL Operations in a Request - Dgraph
GraphQL requests can contain one or more operations. Operations include query, mutation, or subscription.
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