ts2gql
Walks a TypeScript type hierarchy and translates it into GraphQL's IDL.
Usage: ts2gql root/module.ts
ts2gql
will load root/module.ts
(and transitive dependencies), and look for an exported interface annotated with /** @graphql schema */
, which will become the GraphQL schema
declaration. All types referenced by that interface will be converted into GraphQL's IDL.
Example (and "Docs")
input.ts
export type Url = string;
export type Id = string;
export interface User {
id: Id;
name: string;
photo: Url;
}
export interface PostContent {
title: string;
body: string;
}
export interface Post extends PostContent {
id: Id;
postedAt: Date;
author: User;
}
export interface Category {
id: Id;
name: string;
posts: Post[];
}
export interface IdQuery {
id: Id;
}
export interface PostQuery extends IdQuery {
authorId: Id;
categoryId: Id;
}
export interface QueryRoot {
users(args: {id: Id}): User[]
posts(args: {id: Id, authorId: Id, categoryId: Id}): Post[]
categories(args: {id: Id}): Category[]
}
export interface QueryRootUsingTypes {
users(args:IdQuery): User[]
posts(args:PostQuery): Post[]
categories(args:IdQuery): Category[]
}
export interface MutationRoot {
login(args: {username: string, password: string}): QueryRoot;
}
export interface Schema {
query: QueryRoot;
mutation: MutationRoot;
}
export interface EmailRecipients {
type:string
name:string
email:Email
}
export interface CategoryOverrides {
posts(args: {authorId:Id}): Post[]
}
> ts2gql input.ts
scalar Date
scalar Url
type User {
id: ID
name: String
photo: Url
}
interface PostContent {
body: String
title: String
}
type Post {
author: User
body: String
id: ID
postedAt: Date
title: String
}
type Category {
id: ID
name: String
posts(authorId: ID): [Post]
}
type QueryRoot {
categories(id: ID): [Category]
posts(id: ID, authorId: ID, categoryId: ID): [Post]
users(id: ID): [User]
}
type QueryRootUsingTypes {
categories(id: ID): [Category]
posts(id: ID, authorId: ID, categoryId: ID): [Post]
users(id: ID): [User]
}
type MutationRoot {
login(username: String, password: String): QueryRoot
}
schema {
mutation: MutationRoot
query: QueryRoot
}