An open API service indexing awesome lists of open source software.

https://github.com/devxoul/graphql-codegen-typescript-fixtures

A plugin for graphql-code-generator that generates TypeScript fixtures for testing.
https://github.com/devxoul/graphql-codegen-typescript-fixtures

Last synced: 8 months ago
JSON representation

A plugin for graphql-code-generator that generates TypeScript fixtures for testing.

Awesome Lists containing this project

README

          

# graphql-codegen-typescript-fixtures

[![npm version](https://badge.fury.io/js/graphql-codegen-typescript-fixtures.svg)](https://badge.fury.io/js/graphql-codegen-typescript-fixtures)
[![CI](https://github.com/devxoul/graphql-codegen-typescript-fixtures/workflows/CI/badge.svg)](https://github.com/devxoul/graphql-codegen-typescript-fixtures/actions/workflows/ci.yml)

A plugin for [graphql-code-generator](https://www.graphql-code-generator.com/) that generates TypeScript fixtures for testing.

## At a Glance

```tsx
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
```

## Features

* 🍭 Strongly typed.

type-hints

* 🧬 Built-in support for [Immer](https://github.com/immerjs/immer) integration.

immer

## Installation

* Using Yarn:
```console
$ yarn add graphql-codegen-typescript-fixtures --dev
```
* Using npm:
```console
$ npm install graphql-codegen-typescript-fixtures --dev
```

Add lines below in your graphql-codegen configuration file. Check out [Configuration](Configuration) section for more details.

```diff
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"
```

## Configuration

### typeDefinitionModule

*(Required)* A path for the GraphQL type definition module. This value is used to import the GraphQL type definitions.

For example:

```yaml
config:
fixtures:
typeDefinitionModule: "@src/generated/graphql"
```

And the generated code will be:

```ts
// src/generated/graphql-fixtures.ts
import * as types from '@src/generated/graphql'
```

### immer

*(Optional)* Whether to generate [Immer](https://github.com/immerjs/immer) integration.

For example:

```yaml
config:
fixtures:
immer: true
```

Then the second parameter of `fixture()` will become available.

```ts
fixture('User', user => {
user.name = 'Suyeol Jeon'
})
```

### scalarDefaults

*(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:

```yaml
config:
fixtures:
scalarDefaults:
Date: "'2021-01-01'"
DateTime: "'2021-01-01T00:00:00+00:00'"
Timestamp: 1609426800
```

## License

This project is under MIT license. See the [LICENSE](LICENSE) file for more info.