ts2graphql
TypeScript icon, indicating that this package has built-in type declarations

1.0.11 • Public • Published

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
}
 

Package Sidebar

Install

npm i ts2graphql

Weekly Downloads

17

Version

1.0.11

License

ISC

Unpacked Size

12.3 kB

Total Files

10

Last publish

Collaborators

  • cevek