Registry / testing / graphql-mocks

graphql-mocks

JSON →
library0.12.0jsnpmunverified

`graphql-mocks` is the foundational package providing core primitives for robustly mocking GraphQL APIs, primarily designed for testing and development environments. Its current stable version is 0.12.0. The library offers key differentiators such as the `GraphQLHandler` for orchestrating mock resolution, `Resolver Map Middlewares` for modifying resolver behavior, `Resolver Wrappers` for aspect-oriented resolver logic, and `Highlight` for debugging complex mock interactions. This package is part of a larger ecosystem of `graphql-mocks` libraries, including network adapters for various HTTP mocking tools. The project exhibits an active release cadence, with frequent updates across its monorepo packages, indicating ongoing development and support. It ships with comprehensive TypeScript types, promoting type-safe development.

npm install graphql-mocks
INSTALL
IMPORT
SIG · GRAPHQL-MOCKS
G
graphql-mocks
testingjavascriptv0.12.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

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

GraphQLHandler
import { GraphQLHandler } from 'graphql-mocks';
const { GraphQLHandler } = require('graphql-mocks');
`graphql-mocks` is primarily designed for ESM usage, but CJS is supported with appropriate tooling. Always prefer named imports.
createResolverMap
import { createResolverMap } from 'graphql-mocks';
import createResolverMap from 'graphql-mocks/createResolverMap';
A utility function to structure resolver maps, imported as a named export from the main package.
highlight
import { highlight } from 'graphql-mocks';
import { Highlight } from 'graphql-mocks';
The debugging utility `highlight` is a named export, not a class or default export.

This quickstart demonstrates how to set up a `GraphQLHandler` with a schema and a resolver map, then execute a mock query and mutation against it, simulating a GraphQL API.

import { buildSchema } from 'graphql'; import { GraphQLHandler, createResolverMap } from 'graphql-mocks'; const typeDefs = ` type User { id: ID! name: String! email: String } type Query { hello: String! user(id: ID!): User users: [User!]! } type Mutation { createUser(name: String!, email: String): User! } `; // Create a GraphQL schema from the type definitions const schema = buildSchema(typeDefs); // Define a resolver map with mock data and logic const resolverMap = createResolverMap({ Query: { hello: () => 'Hello GraphQL Mocks!', user: (parent, { id }) => ({ id, name: `User ${id}`, email: `user${id}@example.com` }), users: () => [ { id: '1', name: 'Alice', email: 'alice@example.com' }, { id: '2', name: 'Bob', email: 'bob@example.com' } ] }, Mutation: { createUser: (parent, { name, email }) => ({ id: String(Math.random()), name, email }) } }); // Initialize the GraphQLHandler with the schema and resolver map const handler = new GraphQLHandler({ schema, resolverMap }); async function runMockOperations() { // Execute a mock query const query = ` query GetUsers { users { id name email } } `; const queryResult = await handler.query(query); console.log('Mocked Query Result:', queryResult.data); // Execute a mock mutation const mutation = ` mutation CreateNewUser { createUser(name: "Charlie", email: "charlie@example.com") { id name } } `; const mutationResult = await handler.mutate(mutation); console.log('Mocked Mutation Result:', mutationResult.data); } runMockOperations();
Debug
Known issues
breakingThe package requires Node.js version 24.0.0 or higher. Users on older Node.js versions will encounter compatibility issues.
fix
Upgrade your Node.js environment to version 24.0.0 or newer.
affects: >=0.12.0
gotchaThis library has a peer dependency on `graphql` version `^16.0.0 <17`. Mismatched `graphql` versions in your project can lead to unexpected errors or runtime issues.
fix
Ensure your project's `graphql` dependency matches the peer dependency range specified by `graphql-mocks`. Use `npm install graphql@^16` or `yarn add graphql@^16` if necessary.
affects: >=0.12.0
gotchaAs a 0.x.x versioned library, `graphql-mocks` may introduce breaking changes without a major version bump. Always review release notes carefully when updating.
fix
Consult the project's GitHub releases or changelog for specific breaking changes and migration guides during updates.
affects: >=0.0.0
Errors
Common errors & fixes
Error: Cannot find module 'graphql-mocks'
The `graphql-mocks` package is not installed or the import path is incorrect.
fix
Run `npm install graphql-mocks` or `yarn add graphql-mocks`. Verify your import statement uses `from 'graphql-mocks'`.
Error: Expected `schema` to be a GraphQLSchema instance, but got [object Object]
The `schema` provided to `GraphQLHandler` is not a valid `GraphQLSchema` object from the `graphql` library.
fix
Ensure you are using `buildSchema` from `graphql` to create your schema, or that the schema object is correctly instantiated.
GraphQLError: Cannot query field "unknownField" on type "Query".
Attempting to query a field that is not defined in the GraphQL schema provided to the `GraphQLHandler`.
fix
Review your GraphQL schema (`typeDefs`) and the query you are sending to ensure all fields are correctly defined and match.
Upgrade
Version history
0.12.0latest on npm
Audit
Dependencies
graphqlrequiredCore GraphQL library for schema definition and execution, required as a peer dependency.
Agent activity
9 hits · last 30 days
node
8
Resources
graphql-mocks — npm install graphql-mocks · libregistry