0.0.1-beta.9 • Published 5 years ago

@typeql/core v0.0.1-beta.9

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

TypeQL

Auto generated GraphQL.js schema from your TypeORM Models with dataloader.

import 'reflect-metadata'

import TypeQL, { GraphEntity } from '@typeql/core'
import dotenv from 'dotenv'
import { ApolloServer } from 'apollo-server'
import { Column, ManyToOne, OneToMany, PrimaryGeneratedColumn, createConnection } from 'typeorm'

dotenv.config()

@GraphEntity()
export class User {
  @PrimaryGeneratedColumn()
  public id!: number

  @Column()
  public name: string

  @Column()
  public age: number

  @OneToMany(() => Post, post => post.author)
  public posts: Post[]

  constructor(name: string, age: number, posts: Post[]) {
    this.name = name
    this.age = age
    this.posts = posts
  }
}

@GraphEntity()
class Post {
  @PrimaryGeneratedColumn()
  public id!: number

  @Column()
  public title: string

  @ManyToOne(() => User, user => user.posts)
  public author: User

  constructor(title: string, author: User) {
    this.title = title
    this.author = author
  }
}

const { DB_HOST, DB_TYPE, DB_NAME, DB_USERNAME, DB_PASSWORD, DB_PORT } = process.env

createConnection({
  entities: [User, Post],
  host: DB_HOST,
  type: DB_TYPE as 'mysql',
  database: DB_NAME,
  username: DB_USERNAME,
  password: DB_PASSWORD,
  port: DB_PORT ? parseInt(DB_PORT as any, 10) : undefined,
  synchronize: true,
})
  .then(() => {
    const schema = TypeQL.buildSchema()
    const server = new ApolloServer({
      schema,
    })

    server.listen(3000)
  })
  .catch(error => {
    // eslint-disable-next-line no-console
    console.error(error)
    process.exit(1)
  })

To run tests

Create .env file at the project root and fill it with database information.

TEST_DB_HOST=localhost
TEST_DB_TYPE=mysql
TEST_DB_NAME=test
TEST_DB_USERNAME=root
TEST_DB_PASSWORD=mypassword
TEST_DB_PORT=3316

If you want, you can run a Docker container of MySQL for test based on .env file.

docker-compose up -d

Now you can run tests.

yarn test

Notes

Implementation is now at experimental stage. It's currently tested on the simplest cases.