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.

Request to graphql server not intercepted

See original GitHub issue

Describe the bug

Request to graphql server not intercepted

Environment

  • msw: 0.16.0
  • nodejs: 12.16.3
  • npm: 6.14.5

To Reproduce

// src/App.js
import ApolloClient, { gql } from 'apollo-boost';
import { ApolloProvider } from '@apollo/react-hooks'; 

const client = new ApolloClient({
  uri: 'https://<SOME_DOMAIN>/graphql'
})

const TEST_MSW = gql`
  query TestMSW {
    user(id: 123) {
      firstname
      lastname
    }
  }
`;

client.query({
  query: TEST_MSW
}).then(res => console.log(res)).catch(err => console.log(err));
// src/mocks.js

import { setupWorker, graphql } from 'msw';

const worker = setupWorker(
  graphql.query("TestMSW", (req, res, ctx) => {
    return res(
      ctx.data({
        user: {
          firstname: 'John',
          lastname: 'Doe'
        }
      })
    )
  })
)

// Calling the "start" function registers the Service Worker
worker.start();

// src/index.js

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';

// include mocks by msw
require('./mocks.js');

ReactDOM.render(<App />, document.getElementById('root'));

// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: https://bit.ly/CRA-PWA
serviceWorker.unregister();

Expected behavior

The docs mentioned that all outgoing requests will be intercepted

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
bennett-gsicommented, May 20, 2020

I’ve tried the deferred started approach but that first query is still not intercepted. I used the same bootstrap code as in the documentation and use the App.js as above

1reaction
kettanaitocommented, May 19, 2020

In case you are performing requests on load (or on component’s mount), you can defer the start of your application until the mocking is ready (see Deferred start).

I’m considering to make this approach the recommended one when getting started with the library. Thanks for bumping that in my priorities list!

Read more comments on GitHub >

github_iconTop Results From Across the Web

MSW do not intercept GraphQL requests at all #63 - GitHub
The call is not intercepted. I've already tried: Import jest-fetch-mock in the configuration file;; Start the server in setupFiles.js (as ...
Read more >
Full Stack Error Handling with GraphQL and Apollo
If networkError is present in your response, it means your entire query was rejected, and therefore no data was returned. For example, the ......
Read more >
msw: graphql operation doesn't get intercepted - Stack Overflow
It seems you just add Runtime request handler using server.use() method. But there is no graphql client call it.
Read more >
Spring for GraphQL Documentation
Interception. Server transports allow intercepting requests before and after the GraphQL Java engine is called to process a request. 3.4.1.
Read more >
Intercepting GraphQL Requests With Cypress
If your app uses GraphQL, every network request will match these first two arguments, since every GraphQL request will be a POST request...
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