Registry / testing / graphql-codegen-factories

graphql-codegen-factories

JSON →
library1.2.1jsnpmunverified

A plugin for GraphQL Code Generator that automatically creates factory functions for your GraphQL types. Version 1.2.1 (stable) generates factories for all types defined in your schema, enabling easy test data creation with default values. Differentiates from similar tools by tight integration with graphql-codegen and zero-config setup. Released under MIT license.

npm install graphql-codegen-factories
INSTALL
IMPORT
SIG · GRAPHQL-CODEGEN-FA
G
graphql-codegen-factories
testingjavascriptv1.2.1
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.

factory function
import { UserFactory } from './generated/factories'
import { UserFactory } from '@graphql-codegen/factories'
Factories are generated into your project, not imported from the package.
config
import { FactoryConfig } from 'graphql-codegen-factories'
const FactoryConfig = require('graphql-codegen-factories')
ESM only; no default export.
plugin
import factoriesPlugin from 'graphql-codegen-factories'
import { factories } from 'graphql-codegen-factories'
Default export is the plugin function.

Configure GraphQL Code Generator to use the factories plugin and generate factory functions from your schema. Then use them in tests to create data with custom overrides.

// codegen.yml schema: './schema.graphql' generates: ./generated/factories.ts: plugins: - graphql-codegen-factories exec: npx graphql-codegen // Use factory import { UserFactory } from './generated/factories'; const user = UserFactory.build({ name: 'Alice' });
Debug
Known issues
gotchaGenerated factories do not include enum values from schema; enum fields are left undefined unless explicitly provided in build()
fix
Manually specify enum fields when calling build() or wrap factory to provide defaults
affects: 1.0.0 - 1.2.1
breakingIn v1.2.0, the generated output format changed from JavaScript to TypeScript, breaking CommonJS require() imports
fix
Ensure your project supports ESM or TypeScript, or pin to 1.1.x
affects: >=1.2.0
breakingThe plugin no longer supports inline type definitions; types must be defined in a separate schema file
fix
Move inline types to a .graphql schema file
affects: >=1.0.0
deprecatedThe 'mocks' option has been removed; use 'factories' instead for all configurations
fix
Remove 'mocks' from codegen.yml and use 'factories' if you had custom mock settings
affects: >=1.0.0
Errors
Common errors & fixes
Error: Cannot find module 'graphql-codegen-factories'
Package not installed or path incorrect
fix
Run: npm install graphql-codegen-factories --save-dev
TypeError: factories is not a function
Incorrect import: using named import instead of default
fix
Use: import factoriesPlugin from 'graphql-codegen-factories'
Failed to load schema from ./schema.graphql
Schema file not found or path wrong
fix
Check file path and extension in codegen.yml
Upgrade
Version history
1.2.1latest on npm
Audit
Dependencies
graphqlrequiredpeer dependency for codegen runtime
Agent activity
12 hits · last 30 days
node
12
Resources
graphql-codegen-factories — npm install graphql-codegen-factories · libregistry