Skip to content
/ gql2ts Public

convert a graphql schema to a typescript definition

License

Notifications You must be signed in to change notification settings

amount/gql2ts

Repository files navigation

GQL2TS

Greenkeeper badge npm version TravisCI Build Status AppVeyor Build status Coveralls Coverage Reviewed by Hound

Generate TypeScript and Flow type interfaces from GraphQL types and query definitions.

# for CLI
npm install -g gql2ts

# for programmatic use
npm install @gql2ts/from-query
npm install @gql2ts/from-schema

Structure

This project is a lerna mono repo, consisting of multiple packages.

Goals and usage

This project can either:

  • Take a type schema to generate TS interfaces
  • Take a type schema & query and generate TS interfaces

Generate from type schema

Pass a GraphQL type schema to generate a TypeScript interface:

Input:

type Query {
  thing: String!
  anotherThing: Boolean!
}

Output:

interface IQuery {
  thing: string;
  anotherThing: boolean;
}

Generate from type schema and query

Pass a type and query to generate a TypeScript interface:

Input:

type Query {
  thing: String!
  anotherThing: Boolean!
}
query GetThing {
  thing
}

Output

interface GetThing {
  thing: string;
}

Language output

Note that flow and typescript generator outputs are supported via separate packages:

  • language-flow - defaults for running gql2ts with Flow as your target
  • language-typescript - defaults for running gql2ts with Typescript as your target

Fine control

If you look into the index.ts of either from-query or from-schema packages:

import {
  DEFAULT_TYPE_MAP,
  DEFAULT_OPTIONS,
} from '@gql2ts/language-typescript';

This can be replaced with mathing flow configuration objects:

import {
  DEFAULT_TYPE_MAP,
  DEFAULT_OPTIONS,
} from '@gql2ts/language-flow';

Note: Flow output support has yet to be packaged and made available for the CLI.

Packages

See the packages directory for package Readmes.

packages/cli                  # gql2ts CLI
packages/from-query           # @gql2ts/from-query
packages/from-schema          # @gql2ts/from-schema
packages/language-flow        # @gql2ts/language-flow
packages/language-typescript  # @gql2ts/language-typescript
packages/loader               # @gql2ts/loader
packages/types                # @gql2ts/types
packages/util                 # @gql2ts/util