Registry / testing / graphql-codegen-mock-results

graphql-codegen-mock-results

JSON →
library1.0.8jsnpmunverified

GraphQL Code Generator plugin for generating mock results for GraphQL operations based on your documents and schema. Current stable version is 1.0.8. It integrates with the GraphQL Codegen ecosystem, supports both JavaScript and TypeScript output, and requires peer dependency graphql (multiple versions supported). Key differentiators: automatically generates realistic mock data for queries and mutations, supports custom scalars, configurable naming conventions, and adds __typename when needed. It works seamlessly with @graphql-codegen/typescript and @graphql-codegen/typescript-operations for typed mocks.

npm install graphql-codegen-mock-results
INSTALL
IMPORT
SIG · GRAPHQL-CODEGEN-MO
G
graphql-codegen-mock-results
testingjavascriptv1.0.8
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.

graphql-codegen-plugin
Use the plugin via graphql-codegen config, not direct import. Refer to the codegen config as shown in README.
import { mockResults } from 'graphql-codegen-mock-results'
The plugin is integrated via codegen.yml, not directly imported in your code.
mock variable
The generated file exports mock variables like `const userQueryMock = ...`. No import needed from the package itself.
import { userQueryMock } from 'graphql-codegen-mock-results'
Mock variables are generated into a separate file by the plugin, not exported from the package.
config
Configure in codegen.yml as a plugin, e.g., `plugins: - graphql-codegen-mock-results` with options.
import config from 'graphql-codegen-mock-results'
Configuration is done in the codegen YAML file, not via JavaScript import.

Shows a complete setup: installation, codegen config with TypeScript and mock plugin, a sample query, and the generated mock output with __typename.

// Install plugin and graphql npm install -D graphql-codegen-mock-results graphql // codegen.yml schema: path/to/schema.graphql documents: './src/**/*.graphql' generates: path/to/mocks.ts: plugins: - typescript - typescript-operations - graphql-codegen-mock-results addTypename: true // GraphQL query query user($id: ID!) { user(id: $id) { id name age } } // Run codegen npx graphql-codegen // Generated mock (mocks.ts) const userQueryMock: ExecutionResult<UserQuery> = { data: { __typename: 'Query', user: { __typename: 'User', id: '9e6ea9fe-e42a-4996-a587-ce71904308da', name: 'Hello World', age: 84 } } };
Debug
Known issues
gotchaPlugin generates mocks for operations based on documents; it does not generate mocks for types/scalars. Ensure you have both schema and documents configured.
fix
Include a `documents` path pointing to your .graphql files or GraphQL operations.
affects: >=1.0.0
gotchaWhen using TypeScript output, you must also include `@graphql-codegen/typescript` and `@graphql-codegen/typescript-operations` plugins for type generation.
fix
Add 'typescript' and 'typescript-operations' to the plugins list before 'graphql-codegen-mock-results'.
affects: >=1.0.0
gotchaCustom scalars must be specified in `customScalarValues` option; otherwise, custom scalar values are not defined and will be represented by their name string.
fix
Add `customScalarValues: { Date: '2024-01-01', JSON: '{}' }` to plugin configuration.
affects: >=1.0.0
breakingMajor version changes in graphql peer dependency may break compatibility. The plugin supports graphql ^0.8.0 through ^15.0.0.
fix
Ensure your project's graphql version is within the supported range. For graphql v16+, this plugin may not be compatible.
affects: >=1.0.0
Errors
Common errors & fixes
Error: Unknown plugin graphql-codegen-mock-results
The plugin is not installed or not listed in package.json.
fix
Run npm install -D graphql-codegen-mock-results
Cannot find module 'graphql-codegen-mock-results'
The plugin is not installed or the working directory is incorrect.
fix
Ensure the plugin is installed in your project root. Run npm install -D graphql-codegen-mock-results from the project root.
TypeError: The 'documents' config is required for this plugin
The plugin cannot generate mocks without defined document paths.
fix
Add a `documents` entry in your codegen.yml pointing to your GraphQL operations.
Upgrade
Version history
1.0.8latest on npm
Audit
Dependencies
graphqlrequiredpeer dependency for GraphQL type definitions and ExecutionResult
Agent activity
6 hits · last 30 days
node
6
Resources
graphql-codegen-mock-results — npm install graphql-codegen-mock-results · libregistry