我正在尝试Relay和GraphQL。当我在做架构时,我正在这样做:
let articleQLO = new GraphQLObjectType({
name: 'Article',
description: 'An article',
fields: () => ({
_id: globalIdField('Article'),
title: {
type: GraphQLString,
description: 'The title of the article',
resolve: (article) => article.getTitle(),
},
author: {
type: userConnection,
description: 'The author of the article',
resolve: (article) => article.getAuthor(),
},
}),
interfaces: [nodeInterface],
})
所以,当我要求这样的文章时:
{
article(id: 1) {
id,
title,
author
}
}
它会对数据库进行 3 次查询吗?我的意思是,每个字段都有一个解析方法(getTitle
,getAuthor
等),它向数据库发出请求。我做错了吗?
这是getAuthor
(我使用猫鼬)的一个例子:
articleSchema.methods.getAuthor = function(id){
let article = this.model('Article').findOne({_id: id})
return article.author
}