1.0.2 • Published 3 years ago

@pocketlesson/graphql-codegen-typescript-fixtures v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

graphql-codegen-typescript-fixtures

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

At a Glance

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.

  • 🧬 Built-in support for Immer integration.

Installation

  • Using Yarn:
    $ yarn add @pocketlesson/graphql-codegen-typescript-fixtures --dev
  • Using npm:
    $ npm install @pocketlesson/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:
+       - "@pocketlesson/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:

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

And the generated code will be:

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

immer

(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'
})

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:

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 file for more info.