graphql 只解析 _id 字段,其他字段为空

标签 graphql graphql-js

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
    }
  }
})

最佳答案

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

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

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

关于graphql 只解析 _id 字段,其他字段为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36010618/

相关文章:

javascript - "Type RootQueryType must define one or more fields."}

graphql,联合标量类型?

javascript - 使用 Cypress .request 调用 GraphQL 端点

php - Laravel Lighthouse GraphQL - 在服务器端排序

reactjs - Gatsby 和 Graphql - 如何按文件夹过滤 allMarkdownRemark

node.js - GraphQL 嵌套查询查找

javascript - 尝试使用 graphql.js 访问 Github 的 v4 API 时出现错误请求

javascript - 将自定义 GraphQL 解析器和类型添加到 Prisma/Nexus 架构中

javascript - 未捕获的类型错误 : Cannot read property 'data' of undefined with Gatsby and graphQl

reactjs - 来自 GraphQL 突变的动态响应