2017-08-16 58 views
2

我在mysql中有以下表格。如何爲MySQL模型創建GraphQL模式?

用戶

CREATE TABLE users(
    id INTEGER AUTO_INCREMENT PRIMARY KEY, 
    username VARCHAR(255) UNIQUE NOT NULL, 
    email VARCHAR(255) UNIQUE NOT NULL, 
    password VARCHAR(100) NOT NULL, 
    created_at TIMESTAMP DEFAULT NOW() 
); 

帖子

CREATE TABLE posts(
    id INTEGER AUTO_INCREMENT PRIMARY KEY, 
    created_by INTEGER NOT NULL, 
    created_at TIMESTAMP DEFAULT NOW(), 
    post_title VARCHAR(100) NOT NULL UNIQUE, 
    post_body VARCHAR(255) NOT NULL, 
    slug VARCHAR(100) NOT NULL UNIQUE, 
    FOREIGN KEY (created_by) REFERENCES users(id) 
); 

我使用的NodeJS和MySQL NPM包。我如何爲模型創建graphQL模式?

我搜索了很多,但沒有找到任何解決方案。大多數人正在使用sequelize這個目的。它比mysql包好嗎?

+0

嗯,這似乎是一個正確維護的解決方案:https://github.com/mickhansen/graphql-sequelize –

+0

已經看到。我是否需要從我正在使用的mysql轉換到sequelize。你能告訴我使用sequelize而不是mysql npm包是否明智嗎? – Developer101

回答

1

是的,你可以使用sequelize ORM連接graphql MySQL數據庫

REFFERENCE: - http://docs.sequelizejs.com/manual/installation/getting-started

示例模式和解析器給出

Schema.js

const typeDefinitions = ` 



type Author { 

    authorId: Int 
    firstName: String 
    lastName: String 
    posts: [Post] 

} 

type Post { 

    postId: Int 
    title: String 
    text: String 
    views: Int 
    author: Author 

} 

input postInput{ 
    title: String 
    text: String 
    views: Int 
} 


type Query { 

    author(firstName: String, lastName: String): [Author] 
    posts(postId: Int, title: String, text: String, views: Int): [Post] 

} 



type Mutation { 

createAuthor(firstName: String, lastName: String, posts:[postInput]): Author 

updateAuthor(authorId: Int, firstName: String, lastName: String, posts:[postInput]): String 

} 


schema { 
    query: Query 
    mutation:Mutation 
} 
`; 

export default [typeDefinitions]; 

連接器。 js

import rp from 'request-promise'; 
var Sequelize = require('sequelize'); 
var db = new Sequelize('test', 'postgres', 'postgres', { 
    host: '192.168.1.168', 
    dialect: 'postgres', 

    pool: { 
    max: 5, 
    min: 0, 
    idle: 10000 
    } 

}); 


const AuthorModel = db.define('author', { 
    authorId: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true, field: "author_id" }, 
    firstName: { type: Sequelize.STRING, field: "first_name" }, 
    lastName: { type: Sequelize.STRING, field: "last_name" }, 
},{ 
     freezeTableName: false, 
     timestamps: false, 
     underscored: false, 
     tableName: "author" 
    }); 


const PostModel = db.define('post', { 
    postId: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true, field: "post_id" }, 
    text: { type: Sequelize.STRING }, 
    title: { type: Sequelize.STRING }, 
    views: { type: Sequelize.INTEGER }, 
},{ 
     freezeTableName: false, 
     timestamps: false, 
     underscored: false, 
     tableName: "post" 
    }); 


AuthorModel.hasMany(PostModel, { 
    foreignKey: 'author_id' 
}); 
PostModel.belongsTo(AuthorModel, { 
    foreignKey: 'author_id' 
}); 

const Author = db.models.author; 
const Post = db.models.post; 

export { Author, Post }; 

resolver.js

import { Author } from './connectors'; 
import { Post } from './connectors'; 


const resolvers = { 

    Query: { 
    author(_, args) { 
     return Author.findAll({ where: args }); 
    }, 
    posts(_, args) { 
     return Post.findAll({ where: args }); 
    } 
    }, 

    Mutation: { 

    createAuthor(_, args) { 
     console.log(args) 
     return Author.create(args, { 
     include: [{ 
      model: Post, 
     }] 
     }); 
    }, 

    updateAuthor(_, args) { 

     var updateProfile = { title: "name here" }; 
     console.log(args.authorId) 
     var filter = { 
     where: { 
      authorId: args.authorId 
     }, 
     include: [ 
      { model: Post } 
     ] 
     }; 
     Author.findOne(filter).then(function (product) { 
     Author.update(args, { where: { authorId: args.authorId } }).then(function (result) { 
      product.posts[0].updateAttributes(args.posts[0]).then(function (result) { 
      //return result; 
      }) 
     }); 
     }) 
     return "updated"; 
    }, 

    }, 


    Author: { 
    posts(author) { 
     return author.getPosts(); 
    }, 
    }, 
    Post: { 
    author(post) { 
     return post.getAuthor(); 
    }, 
    }, 
}; 

export default resolvers;