Search code examples
laravelgraphqllaravel-lighthousegraphql-php

Graphql: User Error: expected iterable, but did not find one for field XXX


This is movie schema:

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

This is how I related book and author

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

This is schema for author

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

This is my query for Book:

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

This is how I query:

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

Finally, this is what I get as error message:

"User Error: expected iterable, but did not find one for field Book.author."

If I use hasMany instead of belongsTo, it works fine.

Please let me know what is wrong here?


Solution

  • Your type should be

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