Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cevek/ts2graphql
Convert typescript interfaces to graphql schema
https://github.com/cevek/ts2graphql
Last synced: about 2 months ago
JSON representation
Convert typescript interfaces to graphql schema
- Host: GitHub
- URL: https://github.com/cevek/ts2graphql
- Owner: cevek
- Created: 2019-01-20T15:48:20.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2023-09-12T00:12:31.000Z (over 1 year ago)
- Last Synced: 2024-11-01T02:34:54.153Z (about 2 months ago)
- Language: TypeScript
- Size: 16.6 KB
- Stars: 29
- Watchers: 4
- Forks: 8
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ts2graphql
This tool converts your typescript interfaces to graphql schema.
// schema.ts
```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
```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
```graphql
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
}```