How to Generate TypeScript from GraphQL - Step by Step Guide
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:
Example: GraphQL Schema Input
Sample GraphQL type definition:
type User {
id: ID!
name: String!
email: String!
posts: [Post!]!
}
type Post {
id: ID!
title: String!
}Automatic TypeScript Generation
About half a second after you stop typing, the TypeScript appears on the right:
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;
}Use in Your TypeScript Project
Take the code into your project:
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.
Related Tools
GraphQL Query Formatter
Format and beautify GraphQL queries and schemas with syntax highlighting
GraphQL Schema Validator
Validate GraphQL schemas with syntax and structure checking
GraphQL Fixer
Fix broken GraphQL queries and schemas automatically, repair syntax errors and malformed types
GraphQL Minifier
Compress and optimize GraphQL queries for production
GraphQL to JSON Schema
Convert GraphQL schemas to JSON Schema format
GraphQL to JSON
Convert GraphQL schemas and queries to JSON format