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
// Type aliases become GraphQL scalars.; // If you want an explicit GraphQL ID type, you can do that too:/** @graphql ID */; // Interfaces become GraphQL types. /** @graphql input */ /** @graphql input */ // Methods are transformed into parameteried edges: // Input types can be composed with inheritence. This renders to the same graphql as QueryRoot above. // Don't forget to declare your schema and the root types!/** @graphql schema */ // If you have input objects (http://docs.apollostack.com/graphql/schemas.html#input-objects)/** @graphql input */ // You may also wish to expose some GraphQL specific fields or parameterized// calls on particular types, while still preserving the shape of your// interfaces for more general use:/** @graphql override Category */
> 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
}