GQL2TS
April 4, 2019 ยท View on GitHub
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 runninggql2tswith Flow as your targetlanguage-typescript- defaults for runninggql2tswith 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