Giter Club home page Giter Club logo

graphgentypeorm's Introduction

graphGenTypeorm

Generate TypeORM Entities and Query/Mutation/Field Resolvers for use with graphql.

Important

Library requires:

Example graphql file usage

# import Entity, PrimaryGeneratedColumn, ManyToMany, ManyToOne, OneToMany, EntityEnum, Column from 'graphgentypeorm/src/typeorm.graphql'
# optional graphql-import of all typeorm directives
# alternatively you can just combine the files and not use graphql-import

type Author @Entity {
    id: Int! @PrimaryGeneratedColumn
    name: String! @Column({type: "varchar"})
    books: [Book] @ManyToMany(inverseSide: "authors") @JoinColumn
}

type Book @Entity {
    id: Int! @PrimaryGeneratedColumn
    title: String! @Column({type: "varchar"})
    description: String @Column({type: "varchar"})
    authors: [Author] @ManyToMany(inverseSide: "books")
    publisher: Publisher @ManyToOne(inverseSide: "books")
}

type Publisher @Entity {
    id: Int! @PrimaryGeneratedColumn
    name: String! @Column({type: "varchar"})
    books: @OneToMany(inverseSide: "publisher")
}

Create graphql Query and Mutation graphql files

import { schemaToJS } from 'graphschematojson'
import { queryTemplate, mutationTemplate } from 'graphgentypeorm'
import { importSchema } from 'graphql-import'
import { makeExecutableSchema } from 'graphql-tools'
import { mkdirSync, writeFileSync } from 'fs'

const typeDefs = importSchema('./path/to/schema.graphql')
const schema = makeExecutableSchema({typeDefs, resolvers: {}})
const jsSchema = schemaToJS(schema)

if (!existsSync('./generated')) {
    mkdirSync('./generated')
}
writeFileSync(`./generated/query.graphql`, queryTemplate(jsSchema))
writeFileSync(`./generated/mutation.graphql`, mutationTemplate(jsSchema))

Create TypeORM instance with generated Entities. Example using graphql-yoga

import { createConnection, EntitySchema } from 'typeorm
import { getQueryResolvers, getMutationResolvers, getRepositories,  getEntitySchemas } from 'graphgentypeorm'


/** Assuming You have jsSchema already created from above */
const connection = createConnection({
    type: {YOUR_TYPE},
    host: {YOUR_HOST}",
    port: {YOUR_PORT},
    username: {YOUR_USERNAME},
    password: {YOUR_PASSWORD},
    database: {YOUR_DATABASE},
    entities: getEntitySchemas(jsSchema).map(schema => new EntitySchema(schema)),
    synchronize: true
}).then(connection => {
    const server = new GraphQLServer({
        typeDefs: [
            importSchema('./path/to/schema.graphql'),
            readFileSync('./generated/mutation.graphql').toString(),
            readFileSync('./generated/query.graphql').toString()
        ],
        resolvers: {...getQueryResolvers(jsSchema), ...getMutationResolvers(jsSchema)},
        /** Must add repositories and connection to context */
        context: {
            repositories: getRepositories({schema: jsSchema, connection}),
            connection
        },
        /** Must add if using relationships in schema.graphql. */
        schemaDirectives: {
            OneToOne,
            OneToMany,
            ManyToOne,
            ManyToMany,
        }

    })
    server.start(() => console.log('Server is running on localhost:4000'))
})

TODO (tons)

  • Improve input get all query to support filter/sort/limit/...
  • Add directives for type checking (email, url, regex, ...)
  • unit-testing (jest)
  • make export resolvers template for migration away when user wants.
  • Add protected directives for fields that should be removed from input generation and have separate input generation (?)
  • clean up messy code (WIP)

graphgentypeorm's People

Contributors

jjwtay avatar kristianmandrup avatar

Watchers

 avatar  avatar  avatar

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.