@@ -454,12 +454,94 @@ Query `posts(limit:10,page:1){data{id},total,per_page}` might return
454454 ...
455455 ],
456456 "total": 21,
457- "per_page": 10"
457+ "per_page": 10
458458 ]
459459 }
460460}
461461```
462462
463+ Note that you need to add in the extra 'data' object when you request paginated resources as the returned data gives you
464+ the paginated resources in a data object at the same level as the returned pagination metadata.
465+
466+ #### Customising the pagination results
467+ You can add in additional metadata results alongside the Laravel 'standard' ones. To keep the Posts theme going, we could
468+ create some additional metadata to show the total number of posts, comments and likes for the posts returned in the paginated
469+ results.
470+
471+ First, create a class that returns the custom fields you want to see:
472+
473+ ```
474+ use Illuminate\Pagination\LengthAwarePaginator;
475+ use GraphQL\Type\Definition\Type as GraphQLType;
476+
477+ class MyCustomPaginationFields
478+ {
479+ public static function getPaginationFields()
480+ {
481+ return [
482+ // Pass through a User object that we can use to calculate the totals
483+ 'totals_for_user' => [
484+ 'type' => \GraphQL::type('total'),
485+ 'description' => 'Total posts, comments and likes for the result set',
486+ 'resolve' => function () {
487+ return app()->make('App\User');
488+ },
489+ 'selectable' => false,
490+ ],
491+ // Add in the 'last page' value from the Laravel Paginator
492+ 'last_page' => [
493+ 'type' => GraphQLType::nonNull(GraphQLType::int()),
494+ 'description' => 'Last page of the result set',
495+ 'resolve' => function (LengthAwarePaginator $data) {
496+ return $data->lastPage();
497+ },
498+ 'selectable' => false,
499+ ],
500+ ];
501+ }
502+ }
503+ ```
504+
505+ Then add a config entry to map this class:
506+
507+ ```
508+ 'custom_paginators' => [
509+ 'post_pagination' => \Namespace\Of\The\MyCustomPaginationFields::class,
510+ ],
511+ ```
512+ You can now query against the new fields in the same way as for the core pagination metadata. We could now extend the example
513+ query from earlier to get the new fields.
514+
515+ Query: ` posts(limit:10,page:1){data{id},totals_for_user,total,per_page,last_page} ` :
516+
517+ ```
518+ {
519+ "data": {
520+ "posts: [
521+ "data": [
522+ {"id": 3},
523+ {"id": 5},
524+ ...
525+ ],
526+ "totals_for_user": [
527+ {"posts": 12},
528+ {"comments": 42},
529+ {"likes": 101}
530+ ],
531+ "total": 21,
532+ "per_page": 10,
533+ "last_page": 3
534+ ]
535+ }
536+ }
537+ ```
538+
539+
540+ If you want to change the name of a default field to fit with users expectations (maybe you want 'total_records' rather
541+ than 'total'), just copy the entry for the field you want to replace (they're in Rebing/GraphQL/Support/PaginationType.php)
542+ and add it to your custom class.
543+
544+
463545### Batching
464546
465547You can send multiple queries (or mutations) at once by grouping them together. Therefore, instead of creating two HTTP requests:
0 commit comments