Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 83 additions & 1 deletion docs/advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -454,12 +454,94 @@ Query `posts(limit:10,page:1){data{id},total,per_page}` might return
...
],
"total": 21,
"per_page": 10"
"per_page": 10
]
}
}
```

Note that you need to add in the extra 'data' object when you request paginated resources as the returned data gives you
the paginated resources in a data object at the same level as the returned pagination metadata.

#### Customising the pagination results
You can add in additional metadata results alongside the Laravel 'standard' ones. To keep the Posts theme going, we could
create some additional metadata to show the total number of posts, comments and likes for the posts returned in the paginated
results.

First, create a class that returns the custom fields you want to see:

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

class MyCustomPaginationFields
{
public static function getPaginationFields()
{
return [
// Pass through a User object that we can use to calculate the totals
'totals_for_user' => [
'type' => \GraphQL::type('total'),
'description' => 'Total posts, comments and likes for the result set',
'resolve' => function () {
return app()->make('App\User');
},
'selectable' => false,
],
// Add in the 'last page' value from the Laravel Paginator
'last_page' => [
'type' => GraphQLType::nonNull(GraphQLType::int()),
'description' => 'Last page of the result set',
'resolve' => function (LengthAwarePaginator $data) {
return $data->lastPage();
},
'selectable' => false,
],
];
}
}
```

Then add a config entry to map this class:

```
'custom_paginators' => [
'post_pagination' => \Namespace\Of\The\MyCustomPaginationFields::class,
],
```
You can now query against the new fields in the same way as for the core pagination metadata. We could now extend the example
query from earlier to get the new fields.

Query: `posts(limit:10,page:1){data{id},totals_for_user,total,per_page,last_page}`:

```
{
"data": {
"posts: [
"data": [
{"id": 3},
{"id": 5},
...
],
"totals_for_user": [
{"posts": 12},
{"comments": 42},
{"likes": 101}
],
"total": 21,
"per_page": 10,
"last_page": 3
]
}
}
```


If you want to change the name of a default field to fit with users expectations (maybe you want 'total_records' rather
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.


### Batching

You can send multiple queries (or mutations) at once by grouping them together. Therefore, instead of creating two HTTP requests:
Expand Down
22 changes: 15 additions & 7 deletions src/Rebing/GraphQL/Support/PaginationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,22 @@ class PaginationType extends ObjectType {
public function __construct($typeName, $customName = null)
{
$name = $customName ?: $typeName . '_pagination';

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

$config = [
'name' => $name,
'fields' => array_merge($this->getPaginationFields(), [
'data' => [
'type' => GraphQLType::listOf(GraphQL::type($typeName)),
'resolve' => function(LengthAwarePaginator $data) { return $data->getCollection(); },
],
])
'fields' => array_merge(
$this->getPaginationFields(),
$customFields,
[
'data' => [
'type' => GraphQLType::listOf(GraphQL::type($typeName)),
'resolve' => function(LengthAwarePaginator $data) { return $data->getCollection(); },
],
]
)
];

parent::__construct($config);
Expand Down Expand Up @@ -61,4 +69,4 @@ protected function getPaginationFields()
];
}

}
}
8 changes: 8 additions & 0 deletions src/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@
'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,
],

/*
* Config for GraphiQL (see (https://github.com/graphql/graphiql).
*/
Expand Down