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
This project is a lerna mono repo, consisting of multiple packages.
This project can either:
- Take a type schema to generate TS interfaces
- Take a type schema & query and generate TS interfaces
Pass a GraphQL type
schema to generate a TypeScript interface:
Input:
type Query {
thing: String!
anotherThing: Boolean!
}
Output:
interface IQuery {
thing: string;
anotherThing: boolean;
}
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;
}
Note that flow
and typescript
generator outputs are supported via separate packages:
language-flow
- defaults for runninggql2ts
with Flow as your targetlanguage-typescript
- defaults for runninggql2ts
with Typescript as your target
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.
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