0

这是电影模式:

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

这是我对书的查询:

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,它可以正常工作。

请让我知道这里有什么问题?

4

1 回答 1

1

你的类型应该是

type Book {
    id: ID!
    title: String
    author: Author @belongsTo(relation: "author")
}
于 2021-07-02T11:55:18.917 回答