Loading GraphQL to TypeScript...
Please wait a moment

How to Generate TypeScript from GraphQL - Step by Step Guide

Step 1

Input Your GraphQL Schema

Start by pasting your GraphQL schema (the type definitions, not a query). Whether you're working with Apollo Client, need to validate schemas, or want to format GraphQL first, you have several options:

•
Paste directly: Copy the SDL from your server project, or the schema.graphql your urql or Apollo setup already uses
•
Upload a file: Click "Upload" to pick a .graphql, .gql or .graphqls file
•
Try the sample: Click "Sample" to load a small User / Post / Profile schema

Example: GraphQL Schema Input

Sample GraphQL type definition:

type User {
  id: ID!
  name: String!
  email: String!
  posts: [Post!]!
}

type Post {
  id: ID!
  title: String!
}
Step 2

Automatic TypeScript Generation

About half a second after you stop typing, the TypeScript appears on the right:

•
Nullability: fields marked with ! are required; the rest are optional and typed T | null, the way a GraphQL server can return them
•
Every kind of type: interfaces for object and input types, string unions for enums, TypeScript unions for GraphQL unions, and Args interfaces for fields that take arguments
•
Your comments: descriptions become JSDoc, and @deprecated reasons are kept, so your editor shows them on hover

Example: Generated TypeScript Output

What the page writes for the schema in Step 1:

// Generated from a GraphQL schema.
// Non-null (!) fields are required; nullable fields are optional and may be null.

export interface User {
  __typename?: 'User';
  id: string;
  name: string;
  email: string;
  posts: Post[];
}

export interface Post {
  __typename?: 'Post';
  id: string;
  title: string;
}
Step 3

Use in Your TypeScript Project

Take the code into your project:

•
Copy to clipboard: paste it into a types file in your editor
•
Download as types.ts: drop the file into your project's types folder
•
Type your data: use the interfaces with Apollo Client, urql or any other client, and tighten custom scalars from unknown to their real type. For typed query results generated in your build, look at GraphQL Code Generator

Frequently Asked Questions

How do I turn a GraphQL schema into TypeScript types?

Paste the SDL your server is built from, or upload the .graphql file. Object, interface and input types become TypeScript interfaces, enums become string unions such as 'ADMIN' | 'EDITOR', GraphQL unions become TypeScript unions, and every field that takes arguments also gets an Args interface, for example QueryUserArgs. Descriptions turn into JSDoc comments, and @deprecated is kept.

How are GraphQL types mapped to TypeScript?

ID and String become string, Int and Float become number, Boolean becomes boolean. A field marked with ! is required. A field without ! is optional and typed as T | null, because a server can send null there. Lists keep their own nullability: [Post!]! becomes Post[] and [String] becomes (string | null)[] | null. Input fields with a default value are optional as well.

What happens to custom scalars like DateTime or JSON?

A schema only names a custom scalar; it never says what the value looks like on the wire. So each one comes out as a type set to unknown, for example export type DateTime = unknown, with a comment above it. Change unknown to string, or whatever your server sends, and every field that uses the scalar follows along. We avoid any on purpose, so the compiler still makes you check the value.

Can I paste a query or a fragment?

Not on its own. The shape of a query result depends on the schema and on which fields the query selects, and this page only writes types for the schema itself. If a file holds both the schema and some queries, the queries are skipped with a note and the schema types are converted. For typed query results, the typescript-operations plugin of GraphQL Code Generator is the usual choice.

How is this different from GraphQL Code Generator?

GraphQL Code Generator runs as part of your build and can also type operations, resolvers and React hooks; its output leans on helper types such as Maybe and Scalars. This page gives you plain interfaces you can paste in seconds, with nothing to install or configure. That suits a quick look at an API or a small project. If your schema changes every week, Code Generator keeps the types in sync for you.

Is my schema uploaded anywhere?

No. The schema is parsed by graphql-js in your browser and the TypeScript is written there too. Nothing is sent to a server or kept, and a reload starts with an empty editor.