【发布时间】:2018-12-12 09:27:11
【问题描述】:
我用graphql做了一个简单的board api系统。
我使用 sequelize(version 4) 连接数据库。
[schema.graphql]
type Article {
article_no: Int!
subject: String!
content: String!
createdAt: String!
updatedAt: String!
comment: String
}
type Comment {
article_no: Int!
content: String!,
createdAt: String!
}
type Query {
articles: [Article]!
article(article_no: Int!): Article
comments: [Comment]!
comment(article_no: Int!): Comment
}
type Mutation {
createArticle(subject: String!, content: String!, password: String!): Boolean!
createComment(article_no: Int!, content: String!, password: String!): Boolean!
}
[解析器.js]
import { Article, Comment } from './db';
const resolvers = {
Query: {
articles: async () => {
return Article.all();
},
article: async(_, args) => {
return Article.find({
where: args.article_no,
});
},
comments: async () => {
return Comment.all();
},
comment: async(_, args) => {
return Comment.find({
where: args.article_no
});
}
},
Mutation: {
createArticle: async (_, args) => {
try {
const article = await Article.create({
subject: args.subject,
content: args.content,
password: args.password
});
return true;
} catch(e) {
return false;
}
},
createComment: async(_, args) => {
try {
const comment = await Comment.create({
article_no: args.article_no,
content: args.content,
password: args.password
})
return comment;
} catch(e) {
return false;
}
}
}
}
export default resolvers;
[db.js]
const Sequelize = require('sequelize');
import config from '../config/config';
const db = new Sequelize(config.DB, config.USER, config.PASS, {
host: 'localhost',
dialect: 'mysql'
})
export const Article = db.define('article', {
article_no: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
subject: {
type: Sequelize.STRING(30),
allowNull: false
},
content: {
type: Sequelize.STRING(100),
allowNull: false
},
password: {
type: Sequelize.STRING(20),
allowNull: false
},
comment: Sequelize.STRING
}, {
freezeTableName: true,
timestamps: true,
underscored: true
})
export const Comment = db.define('comment', {
content: {
type: Sequelize.STRING(150),
allowNull: false
},
password: {
type: Sequelize.STRING(20),
allowNull: false
},
}, {
freezeTableName: true,
timestamps: true,
underscored: true
})
Article.hasMany(Comment, {
foreignKey: 'article_no',
scope: {
comment: 'comment'
}
})
Comment.belongsTo(Article, {
foreignKey: 'article_no',
targetKey: 'article_no',
allowNull: false,
as: 'comment'
});
db.sync()
.then(console.log('[*] DB Sync Done'))
Article 和 Comment 是 1:N 关系。
所以我将hasMany 设置为Article 并将belongsTo 设置为Comment。
也将评论作为评论并将其包含在文章的范围内。
但是当我请求查询{ article(id:1) { subject, comment } }时,
评论返回null。
我参考了文档http://docs.sequelizejs.com/manual/tutorial/associations.html#foreign-keys 并关注。
但它不起作用。
我的预期结果在这里:
{
"data": {
"article": {
"subject": "test",
"comment": {
"article_no":1,
"content: "first comment",
"created_at": "2018010101001",
# every comment related article is here
}
}
}
}
当前结果在这里:
{
"data": {
"article": {
"subject": "test",
"comment": null
}
}
}
我想显示与特定文章相关的所有 cmets。
有什么解决办法吗?
谢谢。
【问题讨论】:
标签: sequelize.js graphql