我已将作者定义如下:
const Author = new GraphQLObjectType({
name: 'Author',
description: 'Represent the type of an author of a blog post or a comment',
fields: () => ({
_id: {type: GraphQLString},
name: {type: GraphQLString},
posts: {type: Post}
})
});
我已将我的查询定义如下
const Query = new GraphQLObjectType({
name: "Root_Query",
fields: {
authors: {
type: new GraphQLList(Author),
args: {_id: { type: GraphQLString },name:{type: GraphQLString}},
resolve: function(rootValue, args, info) {
let fields = {};
let fieldASTs = info.fieldASTs;
fieldASTs[0].selectionSet.selections.map(function(selection) {
fields[selection.name.value] = 1;
});
return authorsCollection.find({}, fields).toArray();
}
}
现在,当我使用下面给出的查询查询具有特定ID的作者时
{
authors(_id: "57c5794a92aef65040c4e0e6"){
_id
name
}
}
而不是使用_id
57c5794a92aef65040c4e0e6
显示作者。它显示所有作者_id
和name
。我该如何解决这个问题?
答案 0 :(得分:0)
问题在于以下数据库代码,由于空查询{}
而获取所有作者:
return authorsCollection.find({}, fields).toArray()
将_id
添加到您的查询中:
// import {ObjectID} from 'mongodb';
const authorId = ObjectID.createFromHexString(args._id);
return authorsCollection.find({_id: authorId}, fields).toArray()
你必须