Skip to content

Commit

Permalink
Merge branch 'release/v1.17.6'
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikk Mihkel Nurges committed Jan 24, 2019
2 parents c4f8bfc + a9212ad commit cb7a0a3
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 151 deletions.
120 changes: 0 additions & 120 deletions docs/advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -552,126 +552,6 @@ If you want to change the name of a default field to fit with users expectations
than 'total'), just copy the entry for the field you want to replace (they're in Rebing/GraphQL/Support/PaginationType.php)
and add it to your custom class.

#### Custom Pagination Type
If pagination cant fulfill you needs, you might want to custom your own pagination type.<br/>
Replace default **pagination_type** inside graphql config and use your own pagination type class.
```php
'pagination_type' => \Rebing\GraphQL\Support\PaginationType::class,
```
Example usage of custom pagination_type: <br>
<br/>
If you want pagination info under **cursor** rather than same level with **data** ,<br>
**Result of default pagination type:**
```
{
"user": {
"data": [],
"total": 0 // pagination info
}
}
```
**Result of custom pagination type:**
```
{
"user": {
"items": [],
"cursor": {
"total": 0, // pagination info
}
}
}
```
you will need to create your own **Custom Pagination Type**
<br/>
**Example of PaginationType.php**
```php
<?php

namespace App\GraphQL\Pagination;

use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type as GraphQLType;
use Illuminate\Pagination\LengthAwarePaginator;
use Rebing\GraphQL\Support\Facades\GraphQL;

class PaginationType extends ObjectType
{
//custom object-types, reference https://webonyx.github.io/graphql-php/type-system/object-types/#recurring-and-circular-types
private static $PaginationCursorType;
public function __construct($typeName, $customName = null)
{
$name = $customName ?: $typeName . 'Pagination';
$config = [
'name' => $name,
'fields' => array_merge(
[
'cursor' => [ // we want pagination info under cursor rather than same level with data
'type' => GraphQLType::nonNull(self::$PaginationCursorType ?: (self::$PaginationCursorType = new PaginationCursorType())),
'resolve' => function (LengthAwarePaginator $paginator) {
return $paginator;
},
]
],
[
'data' => [
'type' => GraphQLType::listOf(GraphQL::type($typeName)),
'resolve' => function (LengthAwarePaginator $data) {
return $data->getCollection();
},
],
]
)
];
parent::__construct($config);
}
}
```
**Example of PaginationCursorType.php**
```php
<?php

namespace App\GraphQL\Pagination;

use GraphQL\Type\Definition\ObjectType;
use Illuminate\Pagination\LengthAwarePaginator;
use GraphQL\Type\Definition\Type as GraphQLType;

class PaginationCursorType extends ObjectType
{
public function __construct()
{
// See https://laravel.com/api/5.6/Illuminate/Pagination/LengthAwarePaginator.html for more fields.
parent::__construct([
'fields' => [
'total' => [
'type' => GraphQLType::nonNull(GraphQLType::int()),
'resolve' => function (LengthAwarePaginator $paginator) {
return $paginator->total();
},
],
'perPage' => [
'type' => GraphQLType::nonNull(GraphQLType::int()),
'resolve' => function (LengthAwarePaginator $paginator) {
return $paginator->perPage();
},
],
'currentPage' => [
'type' => GraphQLType::nonNull(GraphQLType::int()),
'resolve' => function (LengthAwarePaginator $paginator) {
return $paginator->currentPage();
},
],
'hasPages' => [
'type' => GraphQLType::nonNull(GraphQLType::boolean()),
'resolve' => function (LengthAwarePaginator $paginator) {
return $paginator->hasMorePages();
},
],
],
]);
}
}

```
### Batching
Expand Down
5 changes: 2 additions & 3 deletions src/Rebing/GraphQL/GraphQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,9 @@ public static function handleErrors(array $errors, callable $formatter)
foreach ($errors as $error) {
// Try to unwrap exception
$error = $error->getPrevious() ?: $error;
if ($error instanceof \GraphQL\Error\Error) {
continue;
if ($error instanceof \Exception) {
$handler->report($error);
}
$handler->report($error);
}
return array_map($formatter, $errors);
}
Expand Down
14 changes: 3 additions & 11 deletions src/Rebing/GraphQL/Support/PaginationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,17 @@

class PaginationType extends ObjectType {

public $typeName;
public $dataKey = 'data';

public function __construct($typeName, $customName = null)
{
$this->typeName = $typeName;
$name = $customName ?: $this->typeName . '_pagination';

$customPaginator = config('graphql.custom_paginators.' . $name, null);
$customFields = $customPaginator ? $customPaginator::getPaginationFields() : [];
$name = $customName ?: $typeName . 'Pagination';

$config = [
'name' => $name,
'fields' => array_merge(
$this->getPaginationFields(),
$customFields,
[
$this->dataKey => [
'type' => GraphQLType::listOf(GraphQL::type($this->typeName)),
'data' => [
'type' => GraphQLType::listOf(GraphQL::type($typeName)),
'resolve' => function(LengthAwarePaginator $data) { return $data->getCollection(); },
],
]
Expand Down
10 changes: 1 addition & 9 deletions src/Rebing/GraphQL/Support/SelectFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Rebing\GraphQL\Support;

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

if ($parentType instanceof $paginationType) {
$requestedFields = $requestedFields[$parentType->dataKey];
$parentType = $info->schema->getType($parentType->typeName);
}

$fields = self::getSelectableFieldsAndRelations($requestedFields, $parentType);
$fields = self::getSelectableFieldsAndRelations($info->getFieldSelection(5), $parentType);

$this->select = $fields[0];
$this->relations = $fields[1];
Expand Down
8 changes: 0 additions & 8 deletions src/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,6 @@
'disable_introspection' => false
],

// You can define custom paginators to override the out-of-the-box fields
// Useful if you want to inject some parameters of your own that apply at the top
// level of the collection rather than to each instance returned. Can also use this
// to add in more of the Laravel pagination data (e.g. last_page).
'custom_paginators' => [
// 'my_custom_pagination' => \Path\To\Your\CustomPagination::class,
],

/*
* You can define your own pagination type.
* Reference \Rebing\GraphQL\Support\PaginationType::class
Expand Down

0 comments on commit cb7a0a3

Please sign in to comment.