GraphQL to TypeScript: turn your schema into types
This GraphQL to TypeScript converter reads a schema written in the GraphQL Schema Definition Language (SDL) and generates matching TypeScript declarations. Object types, interfaces and input types become interface declarations, enums become string unions or TypeScript enums, and unions become union types. The schema is parsed with the reference graphql-js library, so syntax errors and unknown types are reported with a line and column.
What the converter produces
It targets the schema itself, not individual operations. That makes it a lightweight alternative to a full GraphQL Code Generator setup when you just need types for a resolver, a mock server or a quick prototype. Type descriptions written in triple quotes are kept as JSDoc comments, and deprecated fields get an @deprecated note.
Example
Input schema (an excerpt of the built-in sample, which also defines the Node and Order types that are referenced here):
"""A registered customer"""
type User implements Node {
id: ID!
email: String!
name: String
role: Role!
orders(first: Int = 10): [Order!]!
createdAt: DateTime!
}
enum Role {
ADMIN
CUSTOMER
}
scalar DateTimeOutput for these types with the default options:
export type Scalars_DateTime = string;
export interface UserOrdersArgs {
first?: number | null;
}
/** A registered customer */
export interface User extends Node {
id: string;
email: string;
name?: string | null;
role: Role;
orders: Array<Order>;
createdAt: Scalars_DateTime;
}
export type Role = 'ADMIN' | 'CUSTOMER';Notice that ID maps to string, implements Node becomes extends Node, and the orders(first: Int) field produces a separate UserOrdersArgs interface.
Type mapping
| GraphQL | TypeScript |
|---|---|
ID, String | string |
Int, Float | number |
Boolean | boolean |
[T!]! | Array<T> |
T (nullable) | T | null or Maybe<T>, optionally with ? |
Custom scalar | A Scalars_Name alias: string for names containing Date or Time, unknown for JSON, any otherwise |
Options explained
- Enums as: string union types (
'ADMIN' | 'CUSTOMER') have no runtime cost and work well withisolatedModules. TypeScript enums (enum Role { ADMIN = 'ADMIN' }) give you a runtime object you can iterate. - Nullable fields:
T | nullis explicit.Maybe<T>adds aMaybehelper type at the top, a convention familiar from GraphQL Code Generator. - Nullable fields are optional (?): marks nullable fields with
?, which matches responses where the field may be left out of the selection. Turn it off if every field is always present. - Generate argument types: creates a
TypeFieldArgsinterface for every field with arguments, handy for typing resolvers. - Add __typename: adds an optional
__typenameliteral to each object type, which helps TypeScript narrow unions and interfaces.
Limitations
The tool converts schemas, not queries, so it does not create result types for a specific query { ... } selection. Argument default values such as first: Int = 10 appear only as an optional property, not as a value; a non-null argument or input field that has a default (limit: Int! = 20) is optional as well. Custom scalars get a placeholder alias you can edit, for example changing Scalars_Money from any to string. Directives other than @deprecated are ignored.
The Query and Mutation root types are generated like any other object type, which is useful for typing a resolver map.
Privacy
Your schema is parsed and converted in the browser; nothing is uploaded. To clean up the SDL before converting, run it through the GraphQL formatter. If you only have a sample JSON response, JSON to TypeScript infers types from data, and JSON Schema to TypeScript handles REST schemas.