Laravel Lighthouse: Using @paginate on related models breaks ability to query model directly

Suppose we have Series and Episodes, and each Series has many Episodes:

type Query {
  series: [Series!]! @paginate(defaultCount: 10)
  series(id: ID @eq): Series @find

  episodes: [Episode!]! @paginate(defaultCount: 10)
  episode(id: ID @eq): Episode @find
}

type Series {
  id: ID!
  title: String!
  episodes: [Episode]! @hasMany
  plot: String!
}

type Episode {
  id: ID!
  title: String!
  season: Int!
  series: Series! @belongsTo
  plot: String!
}

Everything works fine. We can query series and episodes and they are paginated.

The Lighthouse docs says we can also paginate relations by setting type on the @hasMany directive (https://lighthouse-php.com/master/api-reference/directives.html#hasmany) like so:

type Series {
  id: ID!
  title: String!
  episodes: [Episode]! @hasMany(type: "paginator")
  plot: String!
}

This works fine when querying series like this:

query series {
  series {
    paginatorInfo {
      total
      currentPage
      hasMorePages
    }
    data {
      title
      episodes {
        paginatorInfo {
          total
          currentPage
          hasMorePages
        }
        data {
          title
        }
      }
    }
  }
}

Series and their episodes each get paginated.

But now I can no longer query episodes directly:

query episodes {
  episodes {
    paginatorInfo {
      total
      currentPage
      hasMorePages
    }
    data {
      title
    }
  }
}

This returns the error No class 'EpisodePaginator' was found for directive 'paginate'" What does that mean exactly, and how do I get the ability to paginate both related models and all models directly?

1 answer

  • answered 2020-01-14 15:59 Jason Adams

    Everything looks correct except for the fact that you have two queries with the same name:

    type Query {
      series: [Series!]! @paginate(defaultCount: 10)
      series(id: ID @eq): Series @find
    }
    

    You will need to change one of those. I suspect what's happening is that you're overwriting the paginated query with one that only grabs a single series.

    Try changing the name of the plural query to allSeries or something like that and see if that fixes your issue.