1.0.11 • Published 5 years ago

ts2graphql v1.0.11

Weekly downloads
207
License
ISC
Repository
github
Last release
5 years ago

ts2graphql

This tool converts your typescript interfaces to graphql schema.

// schema.ts

import { ID, Int, Float } from 'ts2graphql';
interface Query {
    getByAuthor?(args: { id: ID }): Author;
}

interface Author {
    id: ID;
    name: string;
    books(filter: { max?: Int }): Book[];
}

type Book = PDFBook | AudioBook;

interface BookBase {
    /** The authors of this content */
    authors: Author[];
    name: string;
    publishedAt: Date;
    illustrator?: {
        __typename: 'Illustrator';
        name: string;
        email: string;
    };
}

interface PDFBook extends BookBase {
    file: string;
}

interface AudioBook extends BookBase {
    audioFile: string;
}

// index.ts

import { printSchema } from 'graphql';
import { createSchema } from 'ts2graphql';

const schema = createSchema(__dirname + '/schema.ts');
const rootValue = {}; // you should implement resolve methods
express.use(
    '/api/graphql',
    graphqlHTTP({
        schema: schema,
        rootValue: rootValue,
    })
);

console.log(printSchema(schema));

will generate schema

type AudioBook {
  audioFile: String!

  """The authors of this content"""
  authors: [Author!]!
  name: String!
  publishedAt: Date!
  illustrator: Illustrator
}

type Author {
  id: ID!
  name: String!
  books(max: Int): [Book!]!
}

union Book = PDFBook | AudioBook

scalar Date

type Illustrator {
  __typename: String!
  name: String!
  email: String!
}

type PDFBook {
  file: String!

  """The authors of this content"""
  authors: [Author!]!
  name: String!
  publishedAt: Date!
  illustrator: Illustrator
}

type Query {
  getByAuthor(id: ID!): Author
}
1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago