【问题标题】:I am querying for the author with particular '_id' but it displaying the all authors [Graphql]我正在查询具有特定“_id”的作者,但它显示了所有作者 [Graphql]
【发布时间】:2017-01-07 21:41:11
【问题描述】:

我已将作者定义如下:

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 显示作者。它显示所有作者_idname。我该如何解决这个问题??

【问题讨论】:

    标签: graphql graphql-js


    【解决方案1】:

    问题在于以下数据库代码,由于空查询{},它会获取所有作者:

    return authorsCollection.find({}, fields).toArray()
    

    _id 添加到您的查询中:

    // import {ObjectID} from 'mongodb';
    const authorId = ObjectID.createFromHexString(args._id);
    return authorsCollection.find({_id: authorId}, fields).toArray()
    

    你必须

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-13
      • 2021-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多