Schema
config.factoryName
By default, this plugin generates factories named create{Type}Mock
.
So for a type User
, the corresponding factory will be named createUserMock
.
Example
type User {
id: ID!
username: String!
}
export function createUserMock(props: Partial<User>): User {
return {
id: "",
username: "",
...props,
};
}
You can customize the factories' name by configuring factoryName
:
overwrite: true
schema: ./schema.graphql
generates:
./types.ts:
plugins:
- typescript
- graphql-codegen-factories/schema
config:
factoryName: new{Type}
Example
type User {
id: ID!
username: String!
}
export function newUser(props: Partial<User>): User {
return {
id: "",
username: "",
...props,
};
}
config.scalarDefaults
By default, this plugin infers the default values based on the properties' type.
For example, a property whose type is Boolean
will have a value of false
.
Example
type User {
isAdmin: Boolean!
}
export function createUserMock(props: Partial<User>): User {
return {
isAdmin: false,
...props,
};
}
You can customize the default values by configuring scalarDefaults
:
overwrite: true
schema: ./schema.graphql
generates:
./types.ts:
plugins:
- typescript
- graphql-codegen-factories/schema
config:
scalarDefaults:
Boolean: true
Example
type User {
isAdmin: Boolean!
}
export function createUserMock(props: Partial<User>): User {
return {
isAdmin: true,
...props,
};
}
caution
This plugin only infers default values for built-in scalars.
You will probably need to use this option to define the values of custom scalars, e.g Date
.
Example
overwrite: true
schema: ./schema.graphql
generates:
./types.ts:
plugins:
- typescript
- graphql-codegen-factories/schema
config:
scalarDefaults:
Date: new Date()
scalar Date
type User {
createdAt: Date!
}
export function createUserMock(props: Partial<User>): User {
return {
createdAt: new Date(),
...props,
};
}
config.typesPath
By default, this plugin assumes that the types and factories are generated in the same file. The factories reference types without importing them.
If you want to generate types and factories in different files, you need to provide the typesPath
:
overwrite: true
schema: ./schema.graphql
generates:
./types.ts:
plugins:
- typescript
./factories.ts:
plugins:
- graphql-codegen-factories/schema
config:
typesPath: ./types
Example
type User {
id: ID!
username: String!
}
import * as Types from "./types";
export function createUserMock(props: Partial<Types.User>): Types.User {
return {
id: "",
username: "",
...props,
};
}
info
You don't need to configure this option when using @graphql-codegen/near-operation-file-preset.
config.importTypesNamespace
By default, the import types namespace when using config.typesPath
is Types
.
You can customize this namespace by configuring importTypesNamespace
:
overwrite: true
schema: ./schema.graphql
generates:
./types.ts:
plugins:
- typescript
./factories.ts:
plugins:
- graphql-codegen-factories/schema
config:
typesPath: ./types
importTypesNamespace: SharedTypes
Example
type User {
id: ID!
username: String!
}
import * as SharedTypes from "./types";
export function createUserMock(
props: Partial<SharedTypes.User>
): SharedTypes.User {
return {
id: "",
username: "",
...props,
};
}
config.maybeValueDefault
By default, nullable fields are initialized as "null".
You can customize this default value through maybeValueDefault
:
overwrite: true
schema: ./schema.graphql
generates:
./types.ts:
plugins:
- typescript
- graphql-codegen-factories/schema
config:
maybeValueDefault: undefined
Example
type Post {
title: String
}
input PostInput {
title: String
}
export function createPost(props: Partial<Post> = {}): Post {
return {
title: undefined,
...props,
};
}
export function createPostInputMock(props: Partial<PostInput> = {}): PostInput {
return {
title: undefined,
...props,
};
}
This option can also be used to initialize nullable fields, instead of defaulting to a static value:
overwrite: true
schema: ./schema.graphql
generates:
./types.ts:
plugins:
- typescript
- graphql-codegen-factories/schema
config:
maybeValueDefault: "{defaultValue}"
Example
type Post {
author: PostAuthor
}
type PostAuthor {
username: String
}
export function createPost(props: Partial<Post> = {}): Post {
return {
author: createPostAuthor(),
...props,
};
}
export function createPostAuthor(props: Partial<PostAuthor> = {}): Post {
return {
username: "",
...props,
};
}
config.inputMaybeValueDefault
By default, inputs' nullable fields are initialized as config.maybeValueDefault
("null" by default).
You can customize this default value through inputMaybeValueDefault
:
overwrite: true
schema: ./schema.graphql
generates:
./types.ts:
plugins:
- typescript
- graphql-codegen-factories/schema
config:
inputMaybeValueDefault: undefined
Example
input PostInput {
title: String
}
export function createPostInputMock(props: Partial<PostInput> = {}): PostInput {
return {
title: undefined,
...props,
};
}
See config.maybeValueDefault
to initialize nullable input fields by setting config.inputMaybeValueDefault
to "{defaultValue}"
.
config.disableDescriptions
By default, objects' and inputs' description is added above the factory function.
You can turn it off by setting disableDescriptions
to true
:
overwrite: true
schema: ./schema.graphql
generates:
./types.ts:
plugins:
- typescript
- graphql-codegen-factories/schema
config:
disableDescriptions: true
Example
"""
Description of a Post object.
"""
type Post {
title: String
}
export function createPostInputMock(props: Partial<PostInput> = {}): PostInput {
return {
title: undefined,
...props,
};
}