如何为 MySQL 模型制作 GraphQL 模式?
Posted
技术标签:
【中文标题】如何为 MySQL 模型制作 GraphQL 模式?【英文标题】:How to make GraphQL schema for MySQL models? 【发布时间】:2018-01-25 01:01:45 【问题描述】:我在mysql中有下表。
用户
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包好?
【问题讨论】:
好吧,这似乎是一个维护得当的解决方案:github.com/mickhansen/graphql-sequelize 已经看到了。我是否需要从我正在使用的 mysql 转移到续集。你能告诉我使用 sequelize 代替 mysql npm 包是否明智? 【参考方案1】:是的,你可以使用 sequelize orm 将 graphql 连接到 Mysql 数据库
参考:- 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];
connectors.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 ;
解析器.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;
【讨论】:
【参考方案2】:您可以尝试一个名为 SwitchQL (github.com/SwitchQL/SwitchQL) 的新开源工具。我已经在这个项目上工作了一段时间。
您将连接字符串传递给它,它会返回在现有数据库之上运行 graphql 服务器所需的一切。它还返回符合 Apollo 的客户端突变和查询。
我们目前只支持 Postgres,但如果您想帮助我们支持 MySQL,请告诉我!
【讨论】:
以上是关于如何为 MySQL 模型制作 GraphQL 模式?的主要内容,如果未能解决你的问题,请参考以下文章
如何为graphql类型的字段对象添加排序,引用不同的graphql类型?