【问题标题】:When querying for a single item in graphql, meteor, and apollo i get null values returned在 graphql、meteor 和 apollo 中查询单个项目时,我返回空值
【发布时间】:2016-12-01 14:38:43
【问题描述】:

我可能忽略了文档中的某些内容,但我似乎遇到了无法从我的 graphql 查询中获取单个对象的问题。

这是架构:

type Query {
    product(name: String!): Product
}

type Product {
    _id: String
    name: String
}

这里是解析器:

Query: {
   product (_, args) {
       return Products.find({where: args})
   },
   products () {
       return Products.find().fetch()
   }
}

这里是查询:

query {
    product(name: "burgers") {
        name
    }
}

我得到了这个结果:

{
    "data": {
        "product": {
          "name": null
        }
    }
}

我是不是忘了添加一些东西,如果是的话,你能给我指出正确的方向吗?

【问题讨论】:

  • 您的数据是什么样的?当您说返回空值时,它是否返回具有空值的行?
  • 我得到了最后一个代码 sn-p 提供的结果。

标签: meteor graphql apollo


【解决方案1】:

如果Products 是一个流星集合,那么.find 返回一个游标,所以正确的返回应该是Products.findOne({name: args.name})

http://docs.meteor.com/api/collections.html#Mongo-Collection-findOne

【讨论】:

    猜你喜欢
    • 2017-12-09
    • 2023-03-13
    • 2018-10-19
    • 1970-01-01
    • 2020-11-02
    • 2019-01-04
    • 2020-01-05
    • 2019-08-26
    • 2019-07-18
    相关资源
    最近更新 更多