Skip to main content

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
schema.graphql
type User {
id: ID!
username: String!
}
types.ts
export function createUserMock(props: Partial<User>): User {
return {
id: "",
username: "",
...props,
};
}

You can customize the factories' name by configuring factoryName:

codegen.yml
overwrite: true
schema: ./schema.graphql
generates:
./types.ts:
plugins:
- typescript
- graphql-codegen-factories/schema
config:
factoryName: new{Type}
Example
schema.graphql
type User {
id: ID!
username: String!
}
types.ts
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
schema.graphql
type User {
isAdmin: Boolean!
}
types.ts
export function createUserMock(props: Partial<User>): User {
return {
isAdmin: false,
...props,
};
}

You can customize the default values by configuring scalarDefaults:

codegen.yml
overwrite: true
schema: ./schema.graphql
generates:
./types.ts:
plugins:
- typescript
- graphql-codegen-factories/schema
config:
scalarDefaults:
Boolean: true
Example
schema.graphql
type User {
isAdmin: Boolean!
}
types.ts
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
codegen.yml
overwrite: true
schema: ./schema.graphql
generates:
./types.ts:
plugins:
- typescript
- graphql-codegen-factories/schema
config:
scalarDefaults:
Date: new Date()
schema.graphql
scalar Date

type User {
createdAt: Date!
}
types.ts
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:

codegen.yml
overwrite: true
schema: ./schema.graphql
generates:
./types.ts:
plugins:
- typescript
./factories.ts:
plugins:
- graphql-codegen-factories/schema
config:
typesPath: ./types
Example
schema.graphql
type User {
id: ID!
username: String!
}
factories.ts
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
schema.graphql
type User {
id: ID!
username: String!
}
factories.ts
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
schema.graphql
type Post {
title: String
}

input PostInput {
title: String
}
types.ts
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
schema.graphql
type Post {
author: PostAuthor
}

type PostAuthor {
username: String
}
types.ts
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
schema.graphql
input PostInput {
title: String
}
types.ts
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
schema.graphql
"""
Description of a Post object.
"""
type Post {
title: String
}
types.ts
export function createPostInputMock(props: Partial<PostInput> = {}): PostInput {
return {
title: undefined,
...props,
};
}