# graphql-codegen-factories

> graphql-codegen plugin to generate factories

Latest version **1.2.1** (published 2023-03-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install graphql-codegen-factories
pnpm add graphql-codegen-factories
yarn add graphql-codegen-factories
bun add graphql-codegen-factories
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.1 |
| Published | 2023-03-06 |
| First published | 2020-06-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 3 |
| Unpacked size | 54.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 29 |
| Maintainers | gabinzhouzi |
| Keywords | graphql, graphql-codegen, plugin, factories |

## Links

- npm: https://www.npmjs.com/package/graphql-codegen-factories
- Repository: https://github.com/zhouzi/graphql-codegen-factories
- Homepage: https://github.com/zhouzi/graphql-codegen-factories#readme
- Issues: https://github.com/zhouzi/graphql-codegen-factories/issues
- npm.io page: https://npm.io/package/graphql-codegen-factories

## Dependencies (3)

- [change-case-all](https://npm.io/package/change-case-all.md) ^1.0.14
- [@graphql-codegen/plugin-helpers](https://npm.io/package/@graphql-codegen/plugin-helpers.md) ^2.4.2
- [@graphql-codegen/visitor-plugin-common](https://npm.io/package/@graphql-codegen/visitor-plugin-common.md) ^2.7.4

## Alternatives

- [apollo-link-http-common](https://npm.io/package/apollo-link-http-common.md) — 879.0K weekly downloads
- [react-relay](https://npm.io/package/react-relay.md) — 336.8K weekly downloads
- [relay-test-utils](https://npm.io/package/relay-test-utils.md) — 181.6K weekly downloads
- [@vendure/core](https://npm.io/package/@vendure/core.md) — 14.8K weekly downloads
- [@pnpm/deps.graph-sequencer](https://npm.io/package/@pnpm/deps.graph-sequencer.md) — 13.4K weekly downloads

## Recent versions

- 1.2.1 (latest) — 2023-03-06
- 1.2.0 — 2023-01-20
- 1.1.0 — 2023-01-19
- 1.0.0 — 2022-05-07
- 1.0.0-beta.4 — 2022-04-24
- 1.0.0-beta.3 — 2022-04-11
- 1.0.0-beta.2 — 2022-03-27
- 1.0.0-beta.1 — 2022-03-26
- 1.0.0-beta.0 — 2022-02-22
- 0.0.10 — 2021-04-08
- 0.0.9 — 2021-04-06
- 0.0.8 — 2021-01-20
- 0.0.7 — 2020-06-30
- 0.0.6 — 2020-06-29
- 0.0.5 — 2020-06-29
- … 4 more at https://npm.io/package/graphql-codegen-factories/versions

## README

# graphql-codegen-factories

![](https://img.shields.io/github/license/zhouzi/graphql-codegen-factories?style=for-the-badge) ![](https://img.shields.io/github/actions/workflow/status/zhouzi/graphql-codegen-factories/.github/workflows/ci.yml?branch=main&style=for-the-badge) ![](https://img.shields.io/npm/v/graphql-codegen-factories?style=for-the-badge)

`graphql-codegen-factories` is a plugin for [GraphQL Code Generator](https://www.graphql-code-generator.com/) that generates factories from a GraphQL schema and operations.
The factories can then be used to mock data, e.g for testing or seeding a database.

For example, given this GraphQL schema:

```graphql
type User {
  id: ID!
  username: String!
}
```

The following factory will be generated:

```typescript
export type User = /* generated by @graphql-codegen/typescript */;

export function createUserMock(props: Partial<User> = {}): User {
  return {
    __typename: "User",
    id: "",
    username: "",
    ...props,
  };
}
```

It is also possible to generate factories from an operation, for example:

```graphql
query GetUser {
  user {
    id
    username
  }
}
```

Will result in the following factories:

```typescript
export type GetUserQuery = /* generated by @graphql-codegen/typescript-operations */;

export function createGetUserQueryMock(props: Partial<GetUserQuery> = {}): GetUserQuery {
  return {
    __typename: "Query",
    user: createGetUserQueryMock_user({}),
    ...props,
  };
}

export function createGetUserQueryMock_user(props: Partial<GetUserQuery["user"]> = {}): GetUserQuery["user"] {
  return {
    __typename: "User",
    id: "",
    username: "",
    ...props,
  };
}
```

You can also use a fake data generator to generate realistic factories such as:

```typescript
import { faker } from "@faker-js/faker";

export function createUserMock(props: Partial<User> = {}): User {
  return {
    __typename: "User",
    id: faker.random.alphaNumeric(16),
    username: faker.lorem.word(),
    ...props,
  };
}
```

- [Documentation](https://gabinaureche.com/graphql-codegen-factories/)
- [Examples](https://github.com/zhouzi/graphql-codegen-factories/tree/main/examples)
  - [Minimal](https://stackblitz.com/github/zhouzi/graphql-codegen-factories/tree/main/examples/minimal)
  - [Usage with near-operation-file-preset](https://stackblitz.com/github/zhouzi/graphql-codegen-factories/tree/main/examples/usage-with-near-operation-file-preset)
  - [Usage with faker](https://stackblitz.com/github/zhouzi/graphql-codegen-factories/tree/main/examples/usage-with-faker)

## Showcase

- [yummy-recipes/yummy](https://github.com/yummy-recipes/yummy)

Are you using this plugin? [Let us know!](https://github.com/zhouzi/graphql-codegen-factories/issues/new)

## Contributors

[![](https://github.com/zhouzi.png?size=50)](https://github.com/zhouzi)
[![](https://github.com/ertrzyiks.png?size=50)](https://github.com/ertrzyiks)
[![](https://github.com/jongbelegen.png?size=50)](https://github.com/jongbelegen)

---
_Source: https://npm.io/package/graphql-codegen-factories · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
