Guides · Mocks
How to mock one GraphQL query in Chrome
By Marlon Maniti · Last updated
Short answer: match on the GraphQL operation name, not the URL. Every
GraphQL query usually goes to the same POST /graphql endpoint, so a
URL-based mock would replace all of them. A DevHeader GraphQL mock rule reads
operationName from the request body and mocks only that operation.
Why URL matching isn't enough
A typical GraphQL client sends:
POST /graphql
Content-Type: application/json
{ "operationName": "GetUser", "query": "query GetUser { user { name } }" }
GetUser, GetPosts, and every mutation share that URL. To fake one, you
need to look inside the body.
Without an extension
- Mock in the client. Apollo Client and urql can be set up with mock links or exchanges in development. It works well, but it means code changes in your app.
- MSW. Mock Service Worker has a
graphql.query("GetUser", ...)handler that matches by operation name. It's the best fit for mocks your whole team and your tests share, and needs a service worker in your app. - Chrome DevTools Local Overrides match a URL, so overriding
/graphqlreplaces every operation's response.
With DevHeader
Install DevHeader and turn on Allow User Scripts on its details page in
chrome://extensions(Chrome 138+). Open the popup once and click Allow access.Open DevTools → DevHeader panel → Mock Management → New Mock.
Set Type to GraphQL, URL Pattern to your endpoint (for example
https://api.example.com/graphql), and Operation Name toGetUser.Under Response, enter the body:
{ "data": { "user": { "name": "Mocked user" } } }Click Save, enable the rule, and reload the page.
Faster: capture the real GetUser request in Live Status, click Add to
Mocks, switch its Type to GraphQL, add the operation name, and edit the
real response.
Check that it worked
Only GetUser changes. Other operations on the same endpoint still reach the
server. In DevHeader's own tests, a page sends GetUser and GetPosts to one
/graphql endpoint; GetUser gets the mocked body and GetPosts gets the
server's.
Mocking a GraphQL error
GraphQL servers usually report errors with status 200 and an errors
array. Mock that shape to test your error handling:
{ "data": null, "errors": [{ "message": "Not authorized", "path": ["user"] }] }
Set the status code to 500 instead to test a transport-level failure.
Limitations
- The operation name must be in the JSON body as
operationName. Requests that send only aquerystring, or use GET with the query in the URL, don't match an operation-name rule. A REST rule on the URL still works for those. - Persisted-query setups that send only a hash need a REST rule instead.
- Mocks can't add latency, and need Chrome 138+ with Allow User Scripts on.
FAQ
Can I mock mutations? Yes. A mutation's operation name matches the same
way, for example UpdateUser.
What if two rules match? The first matching rule in the list wins.
Sources
Try DevHeader
DevHeader is free, keeps every rule on your machine, and only touches sites you grant it access to. Install it from the Chrome Web Store or Edge Add-ons.
