lighthouse icon indicating copy to clipboard operation
lighthouse copied to clipboard

Add interface to implementations of paginator

Open GregPeden opened this issue 3 years ago • 1 comments

What problem does this feature proposal attempt to solve?

The @paginate directive has a standard pattern which would sometimes benefit from an "interface" but this is not provided natively by Lighthouse. An interface would permit use of GraphQL features like fragments.

Which possible solutions should be considered?

the @paginate directive ought to provide an interface based on the type of paginator implemented.

interface SimplePaginator {
  paginatorInfo: SimplePaginatorInfo!
}

interface Paginator {
  paginatorInfo: PaginatorInfo!
}

The generated paginator type for a hypothetical "users" query would look something like this:

type UserSimplePaginator implements SimplePaginator {
  paginatorInfo: SimplePaginatorInfo!
  data: [User!]!
}

An example fragment might look like this, note the use of a client-side generated field just to highlight some of the utility in doing this:

const PaginatorFragment = gql`
  fragment standardPaginator on SimplePaginator {
    paginatorInfo {
      hasMorePages
      lastPage @client
    }
  }
`;

const usersList = gql`
  query usersList(
    $page: Int
    $first: Int!
  ) {
    users(
      page: $page
      first: $first
    )
    data {
      id
      name
      email
    }
    ...standardPaginator
  }
  ${PaginatorFragment}
`;

So imagine a standard infinite scroll UI used for multiple data models for which we want the paginator response data to be defined at a single point. An interface is necessary to implement this way.

I tried looking in to the code implementation for paginators but it's not clear to me how or where to modify the implementation to add this interface proposal. I'd like to offer to prepare a PR but I think I'd be lost.

GregPeden avatar May 25 '22 19:05 GregPeden

The generated types in https://github.com/nuwave/lighthouse/blob/master/src/Pagination/PaginationManipulator.php would have to change and https://github.com/nuwave/lighthouse/blob/master/src/Pagination/PaginationServiceProvider.php would have to add the interface to the schema.

spawnia avatar May 25 '22 21:05 spawnia