【问题标题】:Graphql: User Error: expected iterable, but did not find one for field XXXGraphql:用户错误:预期可迭代,但没有为字段 XXX 找到一个
【发布时间】:2021-09-14 02:39:53
【问题描述】:

这是电影架构:

type Book {
    id: ID!
    title: String
    author: [Author] @belongsTo(relation: "author")
}

这就是我如何关联书籍和作者

public function author()
{
    return $this->belongsTo('App\Models\Author', 'id', 'book_id');
}

这是作者的架构

type Author {
    id: ID!
    title: String!
    book_id: Int!
}

这是我对 Book 的查询:

extend type Query {
    bookCriteria(
        orderBy: _ @orderBy
        where: _ @whereConditions
    ): [Book!]! @paginate
}

我是这样查询的:

{
  bookCriteria
  (
    first: 1, page: 1
  )
  {
    data
    {
      id
      uuid
      author
      {
        id
        title
      }
    }
  }
}

最后,这是我得到的错误消息:

“用户错误:预期可迭代,但没有为字段 Book.author 找到一个。”

如果我使用 hasMany 而不是 belongsTo,它可以正常工作。

请告诉我这里出了什么问题?

【问题讨论】:

    标签: laravel graphql laravel-lighthouse graphql-php


    【解决方案1】:

    你的类型应该是

    type Book {
        id: ID!
        title: String
        author: Author @belongsTo(relation: "author")
    }
    

    【讨论】:

      猜你喜欢
      • 2020-07-04
      • 2019-08-01
      • 2021-01-20
      • 2018-09-06
      • 2017-10-31
      • 1970-01-01
      • 2020-03-13
      相关资源
      最近更新 更多