@@ -552,126 +552,6 @@ If you want to change the name of a default field to fit with users expectations
552552than 'total'), just copy the entry for the field you want to replace (they're in Rebing/GraphQL/Support/PaginationType.php)
553553and add it to your custom class.
554554
555- #### Custom Pagination Type
556- If pagination cant fulfill you needs, you might want to custom your own pagination type.<br />
557- Replace default ** pagination_type** inside graphql config and use your own pagination type class.
558- ``` php
559- 'pagination_type' => \Rebing\GraphQL\Support\PaginationType::class,
560- ```
561- Example usage of custom pagination_type: <br >
562- <br />
563- If you want pagination info under ** cursor** rather than same level with ** data** ,<br >
564- ** Result of default pagination type:**
565- ```
566- {
567- "user": {
568- "data": [],
569- "total": 0 // pagination info
570- }
571- }
572- ```
573- ** Result of custom pagination type:**
574- ```
575- {
576- "user": {
577- "items": [],
578- "cursor": {
579- "total": 0, // pagination info
580- }
581- }
582- }
583- ```
584- you will need to create your own ** Custom Pagination Type**
585- <br />
586- ** Example of PaginationType.php**
587- ``` php
588- <?php
589-
590- namespace App\GraphQL\Pagination;
591-
592- use GraphQL\Type\Definition\ObjectType;
593- use GraphQL\Type\Definition\Type as GraphQLType;
594- use Illuminate\Pagination\LengthAwarePaginator;
595- use Rebing\GraphQL\Support\Facades\GraphQL;
596-
597- class PaginationType extends ObjectType
598- {
599- //custom object-types, reference https://webonyx.github.io/graphql-php/type-system/object-types/#recurring-and-circular-types
600- private static $PaginationCursorType;
601- public function __construct($typeName, $customName = null)
602- {
603- $name = $customName ?: $typeName . 'Pagination';
604- $config = [
605- 'name' => $name,
606- 'fields' => array_merge(
607- [
608- 'cursor' => [ // we want pagination info under cursor rather than same level with data
609- 'type' => GraphQLType::nonNull(self::$PaginationCursorType ?: (self::$PaginationCursorType = new PaginationCursorType())),
610- 'resolve' => function (LengthAwarePaginator $paginator) {
611- return $paginator;
612- },
613- ]
614- ],
615- [
616- 'data' => [
617- 'type' => GraphQLType::listOf(GraphQL::type($typeName)),
618- 'resolve' => function (LengthAwarePaginator $data) {
619- return $data->getCollection();
620- },
621- ],
622- ]
623- )
624- ];
625- parent::__construct($config);
626- }
627- }
628- ```
629- ** Example of PaginationCursorType.php**
630- ``` php
631- <?php
632-
633- namespace App\GraphQL\Pagination;
634-
635- use GraphQL\Type\Definition\ObjectType;
636- use Illuminate\Pagination\LengthAwarePaginator;
637- use GraphQL\Type\Definition\Type as GraphQLType;
638-
639- class PaginationCursorType extends ObjectType
640- {
641- public function __construct()
642- {
643- // See https://laravel.com/api/5.6/Illuminate/Pagination/LengthAwarePaginator.html for more fields.
644- parent::__construct([
645- 'fields' => [
646- 'total' => [
647- 'type' => GraphQLType::nonNull(GraphQLType::int()),
648- 'resolve' => function (LengthAwarePaginator $paginator) {
649- return $paginator->total();
650- },
651- ],
652- 'perPage' => [
653- 'type' => GraphQLType::nonNull(GraphQLType::int()),
654- 'resolve' => function (LengthAwarePaginator $paginator) {
655- return $paginator->perPage();
656- },
657- ],
658- 'currentPage' => [
659- 'type' => GraphQLType::nonNull(GraphQLType::int()),
660- 'resolve' => function (LengthAwarePaginator $paginator) {
661- return $paginator->currentPage();
662- },
663- ],
664- 'hasPages' => [
665- 'type' => GraphQLType::nonNull(GraphQLType::boolean()),
666- 'resolve' => function (LengthAwarePaginator $paginator) {
667- return $paginator->hasMorePages();
668- },
669- ],
670- ],
671- ]);
672- }
673- }
674-
675555```
676556### Batching
677557
0 commit comments