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.

Some GraphQL queries don't get intercepted

See original GitHub issue

Environment

Name Version
msw 0.28.2
browser Chrome
OS MacOS 11.3

Request handlers

// Example of declaration. Provide your code here.
import { setupWorker, rest } from 'msw'

const handlers: RequestHandler[] = [
    rest.post("*", (req) => console.log((req as any).href.url)),
    graphql.query("getSandwiches", (req, res, ctx) => {
        res(ctx.data({
            locations: MOCK_LOCATIONS
        }));
    })
];

const worker = setupWorker(...handlers)

worker.start();

Actual request

Using Apollo-client to fetch GraphQL queries.

function useGetSandwichesQuery(baseOptions?: ApolloReactHooks.QueryHookOptions<GetSandwichesQuery, GetSandwichesQueryVariables>) {
  return ApolloReactHooks.useQuery<GetSandwichesQuery, GetSandwichesQueryVariables>(GetSandwichesDocument, baseOptions);
}

const { data, loading, error } = useGetSandwichesQuery({ variables: { locationId } });

Current behavior

The getSandwiches query is not being intercepted at all. I’ve followed the advice from debugging-uncaught-requests.

onUncaughtHandledRequest doesn’t seem to throw an error on this request, even when I remove the handler from the handlers array. It looks like it doesn’t get intercepted at all. Even the "*" rest handler doesn’t seem to log that particular query. I have 3 GraphQL queries, 1 gets intercepted correctly and throws an error that it isn’t handled. 2 others don’t seem to get intercepted at all. The unmocked version does work perfectly fine however.

printHandlers prints the handler correctly.

<ROOT_URL>/mockServiceWorker.js is available, so I guess the service worker is in the correct scope.

Expected behavior

Expected all 3 GraphQL calls to be intercepted, getSandwiches should be mocked, while the other 2 should throw an error because of the onUncaughtHandledRequest option.

Is there anything else I can do to debug this?

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
ImLuzecommented, May 19, 2021

@kettanaito thank you for the quick reply. Removing the rest handler unfortunately has the same results. I’ve added it as a way to debug this.

I can’t share the repo I’m working on right now, but I’ll see if I can create a minimal reproduction in a couple days. Life is quite busy at the moment so bear with me on this one 😅

0reactions
kettanaitocommented, Jun 16, 2021

I’m closing this as we haven’t received any reproduction repository or evidence that there’s indeed an issue with the library. We can still discuss this and will try to help you debug the problem. Otherwise, we’re treating inactivity as a sign that the issue has been resolved.

Read more comments on GitHub >

github_iconTop Results From Across the Web

msw: graphql operation doesn't get intercepted - Stack Overflow
Funny thing is, the console.log in the graphql.query('testRequest', (_, res, ctx)=> {...}) block never gets invoked while the operation name ...
Read more >
Refetching queries in Apollo Client - Apollo GraphQL Docs
The InMemoryCache helps you determine which active queries might have been invalidated by recent cache updates. Local cache updates and refetching work ...
Read more >
Intercept GraphQL Requests in Cypress - YouTube
In this video I've shown how to Intercept GraphQL Requests in Cypress?Timecodes0:00 - Introduction00:34 - issues with OLD SCHOOL TIMEOUT ...
Read more >
Interceptors - Hot Chocolate - ChilliCream GraphQL Platform
Each GraphQL request sent over WebSockets can be intercepted using an ISocketSessionInterceptor before it is being executed. Since WebSockets ...
Read more >
A Practical Guide to Intercepting Network Requests in Cypress
Our .intercept() command is now matching any request that contains the url ... The reason why our test passing is that our .get()...
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