【问题标题】:graphql is only resolving _id field, other fields are nullgraphql 仅解析 _id 字段,其他字段为空
【发布时间】:2016-06-30 20:29:20
【问题描述】:

GraphQL 将我的标题和内容属性解析为 null,尽管数据肯定是从服务器检索的 - 控制台日志确认了这一点。 graphql 只返回 _id 属性,这是我从查询 json { listings: [ { _id: '56e6c94f1cf94a7c0a4e22ba', title: null, content: null } ] } 得到的返回据我所知,一切都设置正确,我还尝试为标题和内容提供 GraphQLIDType 以排除它类型的差异。

我有一个 graphql 查询:

query(`
  query findListings {
    listings(offset: 1) {
      _id,
      title,
      content
    }
  }
`).then((json) => {
  console.log('json', json.data)
})

我的根查询类型:

const QueryType = new GraphQLObjectType({
  name: 'Query',
  fields: {
    listings: {
      name: 'listings',
      type: new GraphQLList(ListingType),
      args: {
        limit: {
          type: GraphQLInt
        },
        offset: {
          type: GraphQLInt
        }
      },
      resolve(source, args, info) {
        const { fieldASTs } = info
        const projection = getProjection(fieldASTs[0])

        return ListingModel.find({}, projection).then(listing => {
          console.log(listing)
          return listing
        })
      }
    }
  }
})

和我的“列表类型”:

const ListingType = new GraphQLObjectType({
  name: 'Listing',
  fields: {
    _id: {
      type: GraphQLID
    },
    title: {
      type: GraphQLString
    },
    content: {
      type: GraphQLString
    }
  }
})

【问题讨论】:

  • console.log(listing) 打印什么?
  • 这个:[ { content: 'I am the content', title: 'Hello and welcome', _id: 56e6c94f1cf94a7c0a4e22ba } ]
  • 如果您绝对确定 console.log(listing) 打印该数组,那么错误应该在其他地方。也许在您进行查询的客户中?如果您仍然无法解决此问题,请考虑发布更多相关代码。这段代码对我来说看起来非常好。
  • 你试过用graphiql查询吗?

标签: graphql graphql-js


【解决方案1】:

我知道这是旧的但有类似的问题,当查询列表返回 null 时。

当查询数组时,你会得到一个对象 {key: yourArray},所以它应该是:

return ListingModel.find({}, projection).then(listing => {
 console.log(listing)
 return {listing: listing}

【讨论】:

    猜你喜欢
    • 2020-07-08
    • 2020-10-07
    • 1970-01-01
    • 1970-01-01
    • 2021-07-26
    • 2020-08-26
    • 1970-01-01
    • 2020-05-23
    • 2017-01-11
    相关资源
    最近更新 更多