Git Product home page Git Product logo

ts2gql's Introduction

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.
export type Url = string;

// If you want an explicit GraphQL ID type, you can do that too:
/** @graphql ID */
export type Id = string;

// Interfaces become GraphQL types.
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[];
}

/** @graphql input */
export interface IdQuery {
  id: Id;
}

/** @graphql input */
export interface PostQuery extends IdQuery {
  authorId: Id;
  categoryId: Id;
}

// Methods are transformed into parameteried edges:
export interface QueryRoot {
  users(args: {id: Id}): User[]
  posts(args: {id: Id, authorId: Id, categoryId: Id}): Post[]
  categories(args: {id: Id}): Category[]
}

// Input types can be composed with inheritence. This renders to the same graphql as QueryRoot above.
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;
}

// Don't forget to declare your schema and the root types!
/** @graphql schema */
export interface Schema {
  query: QueryRoot;
  mutation: MutationRoot;
}

// If you have input objects (http://docs.apollostack.com/graphql/schemas.html#input-objects)
/** @graphql input */
export interface EmailRecipients {
  type:string
  name:string
  email:Email
}

// 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 */
export interface CategoryOverrides {
  // for example, we may want to be able to filter or paginate posts:
  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
}

ts2gql's People

Contributors

alexturek avatar apoorvkwatra avatar brandonbloom avatar convoykent avatar donvoy avatar finnigantime avatar hluisson avatar igrayson avatar mwinstanley avatar nevir avatar rylanh avatar stevenfromconvoy avatar vinsidious avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ts2gql's Issues

Support complex values for enums

The following code will produce invalid GQL:

interface Foo {
  export enum FooEnums {
    ONE = <any>'ONE',
    TWO = <any>'TWO',
    THREE = <any>'THREE',
  }

  export enum BarEnums {
    ONE = FooEnums.ONE,
    TWO = FooEnums.TWO,
  }
}

The GQL produced will be something like:

enum FooEnums {
  ONE
  TWO
}

enum BarEnums {
  FooEnums.ONE
  FooEnums.TWO
}

This is invalid. The code generation should produce the expected:

enum FooEnums {
  ONE
  TWO
}

enum BarEnums {
  ONE
  TWO
}

Support generics and/or overrides.

Currently the following code will error with Error: Don't know how to handle AnyKeyword nodes:

// Foo.ts
export interface Foo {
  prop:any;
}

// schema.ts
import Foo from './foo';

export interface Bar extends Foo {
  prop:SomeConcreteType;
}

The expectation is that the GQL would contain the type:

type Bar {
  prop: SomeConcreteType
}

Alternatively the source code could be written:

// Foo.ts
export interface Foo<T> {
  prop:T;
}

// schema.ts
import Foo from './Foo';
 
export interface Bar extends Foo<SomeConcreteType>;

And that would result in the same output.

Support TypeScript >= 2.1

I am unable to get this to work under Windows 10. Running ts2gql input.ts using the sample input.ts file in the README produces the following output:

scalar Date

Any recommendations?

Support for GraphQL Required

I am opening a new issue since the other one (#4) is old and I haven't received any response in the discussion.
ts2gql is great but there are a lot of features of the GraphQL SDL that it does not support yet. Required is an essential feature and it would be great to exist support from the original repo.

I believe the most correct approach would be to create a NonNull node as a Wrapping Type and make everything required by default. This combines with the specification of TypeScript that declaring a function parameter requires the function to have this parameter and declaring an interface property requires it's not undefined (unless stated). Unions with null and undefined and ? marks would mark it as not required.

I'm up to implement this feature and submit a PR

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.