Registry / testing / graphql-mock-network

graphql-mock-network

JSON →
library1.0.2jsnpmunverified

Network-level mocking for GraphQL APIs using service workers. Version 1.0.2 (stable, infrequent releases) wraps MSW to intercept GraphQL requests at the network level, avoiding monkey-patching fetch/XHR. Supports auto-mocking from a GraphQL schema and per-test manual mocks. Differentiated by realistic network tab visibility and seamless browser integration via service worker.

npm install graphql-mock-network
INSTALL
IMPORT
SIG · GRAPHQL-MOCK-NETWO
G
graphql-mock-network
testingjavascriptv1.0.2
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

MockNetwork
import { MockNetwork } from 'graphql-mock-network'
import MockNetwork from 'graphql-mock-network'
MockNetwork is a named export, not default.
MockNetwork
const { MockNetwork } = require('graphql-mock-network')
const MockNetwork = require('graphql-mock-network')
CommonJS requires destructuring because it's a named export.
MockNetwork (type)
import type { MockNetwork } from 'graphql-mock-network'
import { MockNetwork } from 'graphql-mock-network'
For type-only imports when using TypeScript, use `import type` to avoid bundling issues.

Initializes MockNetwork with schema and custom mocks, starts network interception, and makes a mocked GraphQL request using fetch.

import { MockNetwork } from 'graphql-mock-network'; import schema from './schema.graphql'; const mockNetwork = new MockNetwork({ schema, mocks: { Query: { todo: () => ({ id: 'xyz', title: 'Manually mocked todo', }), }, }, }); mockNetwork.start(); async function fetchTodo() { const res = await fetch('/graphql', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ query: `query { todo(id: "1") { id title } }`, }), }); const data = await res.json(); console.log(data); } fetchTodo();
Debug
Known issues
breakingRequires MSW service worker file to be served from root in browser environments.
fix
Copy mockServiceWorker.js to your public directory (e.g., public/mockServiceWorker.js) and serve at /mockServiceWorker.js.
affects: >=1.0
gotchaDoes not support graphql-ws or subscriptions; only HTTP POST requests to /graphql.
fix
Use separate mocking for subscriptions (e.g., graphql-tools with Apollo).
affects: >=1.0
gotchaSchema must be an introspection result object or SDL string; introspection JSON is preferred.
fix
Provide schema as generated by introspection query (e.g., from graphql-codegen).
affects: >=1.0
gotchaMocks object structure must match resolver shape: { TypeName: { fieldName: () => value } }.
fix
Ensure mocks are nested by type then field, not flat.
affects: >=1.0
deprecatedMSW v1 is used internally; consider migrating to MSW v2 patterns for future-proofing.
fix
No direct fix – library is unmaintained. Fork and update MSW dependency manually.
affects: >=1.0
Errors
Common errors & fixes
Uncaught ReferenceError: global is not defined
Using graphql-mock-network in browser without proper bundler config (Node.js global missing).
fix
Add 'globalThis' polyfill or set a global mock for 'global' in your bundler (e.g., webpack: { node: { global: true } }).
MSW: Failed to register Service Worker: ServiceWorker script evaluation failed
mockServiceWorker.js not served at root path.
fix
Serve mockServiceWorker.js from your server's root (e.g., public/mockServiceWorker.js for Create React App).
TypeError: schema is not a valid GraphQL schema
Passed introspection JSON as a string instead of parsed object.
fix
Parse introspection JSON: `const schema = buildClientSchema(introspectionResult.data);`
Cannot find module 'msw'
graphql-mock-network has a peer dependency on msw that is not installed.
fix
Run `npm install msw` to add msw as a dependency.
Upgrade
Version history
1.0.2latest on npm
Audit
Dependencies
mswrequiredProvides the service worker infrastructure for network interception
Agent activity
6 hits · last 30 days
node
6
Resources
graphql-mock-network — npm install graphql-mock-network · libregistry