graphql-to-io-ts
generate typescript and io-ts types using graphql-code-generator
Last updated a year ago by micimize .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install graphql-to-io-ts 
SYNC missed versions from official npm registry.

graphql-to-io-ts

Custom templates for graphql-code-generator for generating both native typescript and io-ts types.

usage

yarn add -D graphql-code-generator graphql graphql-to-io-ts@0.2.1
```, then put something like the following in your `package.json`, then run `yarn generate-types`:
```json
{
  "scripts": {
    "generate-types": "gql-gen --template graphql-to-io-ts --schema ./schema/__generated__/schema.json --out schema/__generated__/io-types.ts './src/*/*/*.gql' --customScalars src/io-types/scalars"
  }
}

Custom Scalars

The templates support a custom generator config for custom scalars that you need to define in your gql-gen.json:

{
  "generatorConfig": {
    "customScalars": "src/io-types/scalars"
  }
}

It needs to export all the custom scalars used in your schema:

import * as t from 'io-ts'
import moment from 'moment'

// represents a Date from an ISO string
export const Datetime = new t.Type<moment.Moment, string>(
  'Datetime',
  (mixed): mixed is moment.Moment => moment.isMoment(mixed),
  (mixed, context) =>
    t.string.validate(mixed, context).chain(str => {
      const instance = moment(str)
      return instance.isValid() ? t.success(instance) : t.failure(str, context)
    }),
  instance => instance.toISOString()
)

export type Datetime = moment.Moment

Examples

Tranit decoding, i.e. for apollo-client:

import * as t from 'io-ts'
import { Operations } from 'schema/__generated__/io-types'

type Op = { operationName: Operations.operationType }

export function decoder<O extends Op>(operation: O) {
  return ({ data, ...response }) => ({
      ...response,
      data: Operations[operation.operationName]
        .Operation
        .decode(data)
        .getOrElseL(errors => {
          let failures = failure(errors).join('\n');
          console.error(failures);
          throw new Error(failures)
        })
    })
}

Current Tags

  • 0.2.3                                ...           latest (a year ago)

12 Versions

  • 0.2.3                                ...           a year ago
  • 0.2.2                                ...           a year ago
  • 0.2.1                                ...           a year ago
  • 0.2.0                                ...           a year ago
  • 0.1.6                                ...           2 years ago
  • 0.1.6-alpha                                ...           2 years ago
  • 0.1.5                                ...           2 years ago
  • 0.1.4                                ...           2 years ago
  • 0.1.3                                ...           2 years ago
  • 0.1.2                                ...           2 years ago
  • 0.1.1                                ...           2 years ago
  • 0.1.0                                ...           2 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 2
Dependencies (1)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |