我正在尝试使用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的一个例子(我使用mongoose):
articleSchema.methods.getAuthor = function(id){
let article = this.model('Article').findOne({_id: id})
return article.author
}