A plugin for graphql-code-generator that generates TypeScript fixtures for testing.
import fixture from './generated/graphql-fixtures.ts'
const user = fixture('User')
user.name // ""
user.followers.totalCount // 0
// with Immer.js
const repo = fixture('Repository', repo => {
repo.name = 'my-cool-stuff'
repo.stargazerCount = 1234
})
repo.name // "my-cool-stuff"
repo.stargazerCount // 1234
-
🍭 Strongly typed.
-
🧬 Built-in support for Immer integration.
- Using Yarn:
$ yarn add graphql-codegen-typescript-fixtures --dev
- Using npm:
$ npm install graphql-codegen-typescript-fixtures --dev
Add lines below in your graphql-codegen configuration file. Check out Configuration section for more details.
generates:
src/generated/graphql.ts:
plugins:
- "typescript"
- "typescript-operations"
+ src/generated/graphql-fixtures.ts:
+ plugins:
+ - graphql-codegen-typescript-fixtures
config:
scalars:
Date: string
DateTime: string
+ fixtures:
+ typeDefinitionModule: "path/to/graphql/types.ts"
(Required) A path for the GraphQL type definition module. This value is used to import the GraphQL type definitions.
For example:
config:
fixtures:
typeDefinitionModule: "@src/generated/graphql"
And the generated code will be:
// src/generated/graphql-fixtures.ts
import * as types from '@src/generated/graphql'
(Optional) Whether to generate Immer integration.
For example:
config:
fixtures:
immer: true
Then the second parameter of fixture()
will become available.
fixture('User', user => {
user.name = 'Suyeol Jeon'
})
(Optional) The default values of scalar types. Note that the values are directly written to the TypeScript code so you need to wrap strings with quotes properly.
For example:
config:
fixtures:
scalarDefaults:
Date: "'2021-01-01'"
DateTime: "'2021-01-01T00:00:00+00:00'"
Timestamp: 1609426800
This project is under MIT license. See the LICENSE file for more info.