Registry / testing / graphql-data-generator

graphql-data-generator

JSON →
library0.4.7jsnpmunverified

GraphQL testing utility for generating mock data objects and operation mocks from your schema, with TypeScript types. Current stable version is 0.4.7, with weekly releases on npm. It combines generated types, patching mechanisms, and customizable transforms for common object variations. Key differentiators: built-in mock coverage assertions, array helpers like `next` and `last`, and a `MockedProvider` wrapper for Apollo Client with better stack traces, making it more comprehensive than simple faker-based mocks. Requires peer dependencies graphql, @graphql-tools/graphql-tag-pluck, react, @types/react, @apollo/client 3, @testing-library/dom, and @testing-library/react.

npm install graphql-data-generator
INSTALL
IMPORT
SIG · GRAPHQL-DATA-GENER
G
graphql-data-generator
testingjavascriptv0.4.7
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.

init
import { init, Patch } from 'graphql-data-generator'
import graphqlDataGenerator from 'graphql-data-generator'
init is a named export, not default. Patch is a type for patches.
MockedProvider
import { MockedProvider } from 'graphql-data-generator'
import { MockedProvider } from '@apollo/client/testing'
graphql-data-generator provides its own MockedProvider wrapping Apollo's with better assertions.
assertMockCoverage
import { assertMockCoverage } from 'graphql-data-generator'
const { assertMockCoverage } = require('graphql-data-generator')
ESM-only package; require() will cause an error.

Shows minimal setup to initialize a builder and build a User object with overrides.

import { readFileSync } from 'node:fs'; import { init, Patch } from 'graphql-data-generator'; const schema = readFileSync('schema.graphql', 'utf-8'); const build = init<Record<string, any>, Record<string, any>, Record<string, any>, Record<string, any>, Record<string, any>>( schema, {}, {}, {}, {}, {}, {} )(() => ({})); const user = build.User({ id: '1', name: 'Alice' }); console.log(user);
Debug
Known issues
breakingAPI may change drastically before v1 as project is still experimental
fix
Pin to current version and test upgrades thoroughly.
affects: <1.0.0
gotchaPeer dependencies are required but not listed as direct dependencies; npm may not install them automatically
fix
Install peer deps manually: npm install graphql @graphql-tools/graphql-tag-pluck react @types/react @apollo/client @testing-library/dom @testing-library/react
affects: >=0.4.0
gotchainit function uses a curried call pattern — double parentheses — which can be confusing
fix
Call init as init(...)(() => ({...})) not init(...).
affects: >=0.4.0
deprecatedThe old MockedProvider export path may be removed in future versions
fix
Always import MockedProvider from 'graphql-data-generator' directly.
affects: >=0.4.0
Errors
Common errors & fixes
Cannot find module 'graphql-data-generator' or its corresponding type declarations.
Package not installed or missing TypeScript declaration files.
fix
Run npm install graphql-data-generator --save-dev and ensure tsconfig.json includes node_modules types.
TypeError: (0 , graphql_data_generator.init) is not a function
Importing default export instead of named export init.
fix
Use import { init } from 'graphql-data-generator' instead of import init from 'graphql-data-generator'.
Property 'User' does not exist on type ...
init not called with correct generic types or missing type generation step.
fix
Ensure you've generated types via CLI and passed correct query/mutation/subscription/type/input objects to init.
Upgrade
Version history
0.4.7latest on npm
Audit
Dependencies
graphqlrequiredPeer dependency for schema parsing and type definitions
@graphql-tools/graphql-tag-pluckrequiredPeer dependency for extracting GraphQL operations from source files
reactoptionalPeer dependency for MockedProvider component
@types/reactoptionalPeer dependency for React type support
@apollo/clientoptionalPeer dependency for MockedProvider and GraphQL client mocking
@testing-library/domoptionalPeer dependency for testing utilities
@testing-library/reactoptionalPeer dependency for React testing utilities
Agent activity
8 hits · last 30 days
node
8
Resources
graphql-data-generator — npm install graphql-data-generator · libregistry