Skip to content

Commit cb7a0a3

Browse files
author
Mikk Mihkel Nurges
committed
Merge branch 'release/v1.17.6'
2 parents c4f8bfc + a9212ad commit cb7a0a3

File tree

5 files changed

+6
-151
lines changed

5 files changed

+6
-151
lines changed

docs/advanced.md

Lines changed: 0 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -552,126 +552,6 @@ If you want to change the name of a default field to fit with users expectations
552552
than 'total'), just copy the entry for the field you want to replace (they're in Rebing/GraphQL/Support/PaginationType.php)
553553
and 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

src/Rebing/GraphQL/GraphQL.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,9 @@ public static function handleErrors(array $errors, callable $formatter)
296296
foreach ($errors as $error) {
297297
// Try to unwrap exception
298298
$error = $error->getPrevious() ?: $error;
299-
if ($error instanceof \GraphQL\Error\Error) {
300-
continue;
299+
if ($error instanceof \Exception) {
300+
$handler->report($error);
301301
}
302-
$handler->report($error);
303302
}
304303
return array_map($formatter, $errors);
305304
}

src/Rebing/GraphQL/Support/PaginationType.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,17 @@
99

1010
class PaginationType extends ObjectType {
1111

12-
public $typeName;
13-
public $dataKey = 'data';
14-
1512
public function __construct($typeName, $customName = null)
1613
{
17-
$this->typeName = $typeName;
18-
$name = $customName ?: $this->typeName . '_pagination';
19-
20-
$customPaginator = config('graphql.custom_paginators.' . $name, null);
21-
$customFields = $customPaginator ? $customPaginator::getPaginationFields() : [];
14+
$name = $customName ?: $typeName . 'Pagination';
2215

2316
$config = [
2417
'name' => $name,
2518
'fields' => array_merge(
2619
$this->getPaginationFields(),
27-
$customFields,
2820
[
29-
$this->dataKey => [
30-
'type' => GraphQLType::listOf(GraphQL::type($this->typeName)),
21+
'data' => [
22+
'type' => GraphQLType::listOf(GraphQL::type($typeName)),
3123
'resolve' => function(LengthAwarePaginator $data) { return $data->getCollection(); },
3224
],
3325
]

src/Rebing/GraphQL/Support/SelectFields.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Rebing\GraphQL\Support;
44

5-
use function array_key_exists;
65
use Closure;
76
use GraphQL\Error\InvariantViolation;
87
use GraphQL\Type\Definition\InterfaceType;
@@ -38,15 +37,8 @@ public function __construct(ResolveInfo $info, $parentType, array $args)
3837
if( ! is_null($info->fieldNodes[0]->selectionSet))
3938
{
4039
self::$args = $args;
41-
$requestedFields = $info->getFieldSelection(5);
42-
$paginationType = config('graphql.pagination_type', PaginationType::class);
4340

44-
if ($parentType instanceof $paginationType) {
45-
$requestedFields = $requestedFields[$parentType->dataKey];
46-
$parentType = $info->schema->getType($parentType->typeName);
47-
}
48-
49-
$fields = self::getSelectableFieldsAndRelations($requestedFields, $parentType);
41+
$fields = self::getSelectableFieldsAndRelations($info->getFieldSelection(5), $parentType);
5042

5143
$this->select = $fields[0];
5244
$this->relations = $fields[1];

src/config/config.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,6 @@
156156
'disable_introspection' => false
157157
],
158158

159-
// You can define custom paginators to override the out-of-the-box fields
160-
// Useful if you want to inject some parameters of your own that apply at the top
161-
// level of the collection rather than to each instance returned. Can also use this
162-
// to add in more of the Laravel pagination data (e.g. last_page).
163-
'custom_paginators' => [
164-
// 'my_custom_pagination' => \Path\To\Your\CustomPagination::class,
165-
],
166-
167159
/*
168160
* You can define your own pagination type.
169161
* Reference \Rebing\GraphQL\Support\PaginationType::class

0 commit comments

Comments
 (0)