Skip to content

Adding special type for Paginator and FixedLengthPaginator classes #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 2, 2019

Conversation

moufmouf
Copy link
Member

@moufmouf moufmouf commented Oct 1, 2019

Support for pagination

In your query, if you explicitly return an object that extends the Illuminate\Pagination\LengthAwarePaginator class,
the query result will be wrapped in a "paginator" type.

class MyController
{
    /**
     * @Query
     * @return Product[]
     */
    public function products(): Illuminate\Pagination\LengthAwarePaginator
    {
        return Product::paginate(15);
    }
}

Notice that:

  • the method return type MUST BE Illuminate\Pagination\LengthAwarePaginator or a class extending Illuminate\Pagination\LengthAwarePaginator
  • you MUST add a @return statement to help GraphQLite find the type of the list

Once this is done, you can get plenty of useful information about this page:

products {
    items {      # The items for the selected page
        id
        name
    }
    totalCount   # The total count of items.
    lastPage     # Get the page number of the last available page.
    firstItem    # Get the "index" of the first item being paginated.
    lastItem     # Get the "index" of the last item being paginated.
    hasMorePages # Determine if there are more items in the data source.
    perPage      # Get the number of items shown per page.
    hasPages     # Determine if there are enough items to split into multiple pages.
    currentPage  # Determine the current page being paginated.
    isEmpty      # Determine if the list of items is empty or not.
    isNotEmpty   # Determine if the list of items is not empty.
}
Be sure to type hint on the class (Illuminate\Pagination\LengthAwarePaginator) and not on the interface (Illuminate\Contracts\Pagination\LengthAwarePaginator). The interface itself is not iterable (it does not extend Traversable) and therefore, GraphQLite will refuse to iterate over it.

Simple paginator

Note: if you are using simplePaginate instead of paginate, you can type hint on the Illuminate\Pagination\Paginator class.

class MyController
{
    /**
     * @Query
     * @return Product[]
     */
    public function products(): Illuminate\Pagination\Paginator
    {
        return Product::simplePaginate(15);
    }
}

The behaviour will be exactly the same except you will be missing the totalCount and lastPage fields.

@moufmouf moufmouf merged commit e4fd6ad into thecodingmachine:master Oct 2, 2019
@moufmouf moufmouf deleted the collections branch October 2, 2019 14:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant