Skip to main content


Nock is used for testing and mocking http requests in Node. Integrating graphql-mocks with Nock is easy with @graphql-mocks/network-nock.


Install the nock and @graphql-mocks/network-nock npm packages.

# npm
npm install --save-dev nock @graphql-mocks/network-nock

# yarn
yarn add --dev nock @graphql-mocks/network-nock

# pnpm
pnpm add --save-dev nock @graphql-mocks/network-nock


In any Nock POST .reply(), use the nockHandler with a GraphQLHandler instance.

import { GraphQLHandler } from 'graphql-mocks';
import graphqlSchema from './graphqlSchema';
import { nockHandler } from '@graphql-mocks/network-nock';
import nock from 'nock';

const graphqlHandler = new GraphQLHandler({
dependencies: { graphqlSchema }


The nockHandler function also accepts an options argument, an object with checkGraphQLResult and checkRequest callbacks:

nockHandler(graphqlHandler, {
checkRequest(request, requestBody) => void,
checkGraphQLResult: (result) => void

These callbacks are useful for doing checks or additional assertions during testing

  • checkRequest callback is passed the incoming request and the requestBody
  • checkGraphQLResult callback is passed the result from the GraphQLHandler

With the above nock handler setup, a node-fetch call will be intercepted by and responded to from nock and graphql-mocks.

import fetch from 'node-fetch';

fetch('http://localhost:8080/graphql, {
method: 'POST',
body: JSON.stringify({
// required
query: `
query {
# graphql query

// optional
variables: {},

// optional
operationName: 'OperationName'
}).then(async (response) => {
// get the final json payload
const result = await response.json();
return result;

See the GraphQL docs for details on these body parameters, and making http requests.

Resolver Context

The Nock request object is made available within the resolver context under the nock property:

function resolver(parent, args, context, info) {
const { nock } = context;

// reference to the Nock request object

API Documentation

See the API Documentation for types and more details.