Files
kuvalda-api/GraphQLSchemaPagination.php
T
2026-12-04 10:24:39 +04:00

108 lines
3.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace dominion\api;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use dominion\api\GraphQLPagination;
/**
* Базовый ObjectType пагинационного блока GraphQL-схемы: добавляет поля
* totalCount/pageCount/currentPage/perPage и аргументы page/perPage/offset,
* а getCustomResolve() собирает из аргументов запроса объект GraphQLPagination —
* дальше модель-наследник считает по нему выборку (MySQL/ES) и заполняет
* метаданные через setMeta()/setMetaByQuery().
*/
class GraphQLSchemaPagination extends ObjectType
{
/** @var bool пометка «это пагинация» для собирающих схему приложений */
public $isPagination = false;
/**
* Служебные поля страницы — добавляются к `data`/`items` наследника.
*
* @return array
*/
public function getCustomFields()
{
return [
'totalCount' => [
'type' => Type::int(),
'description' => 'Количество элементов',
],
'pageCount' => [
'type' => Type::int(),
'description' => 'Количество страниц',
],
'currentPage' => [
'type' => Type::int(),
'description' => 'Текущая страница',
],
'perPage' => [
'type' => Type::int(),
'description' => 'Элементов на странице',
],
];
}
/**
* Резолвер блока: превращает аргументы запроса в GraphQLPagination
* (page не меньше 1, perPage по умолчанию 50, offset, addArgs).
*
* @param mixed $root корневой резолвер
* @param array $args аргументы поля
* @return GraphQLPagination
*/
public function getCustomResolve($root, $args)
{
$model = new GraphQLPagination;
$model->parentRoot = $root;
$model->page = isset($args['page']) && $args['page'] > 1 ? $args['page'] : 1;
$model->perPage = isset($args['perPage']) && $args['perPage'] > 0 ? $args['perPage'] : 50;
$model->offset = isset($args['offset']) && $args['offset'] > 0 ? $args['offset'] : 0;
$model->addArgs = isset($args['addArgs']) ? (array)$args['addArgs'] : [];
return $model;//['totalCount' =>0, 'pageCount' => 0, 'currentPage' => 0, 'perPage'=> 0, 'data' => []];
}
/**
* Аргументы запроса страницы — подмешиваются к аргументам наследника.
*
* @return array
*/
public function getCustomArgs()
{
return [
'page' => [
'type' => Type::int(),
'description' => 'Номер страницы (по умолчанию 1)',
],
'perPage' => [
'type' => Type::int(),
'description' => 'Количество элементов на странице (по умолчанию 50)',
],
'offset' => [
'type' => Type::int(),
'description' => 'Смещение от начала выборки',
],
];
}
/**
* Распаковывает аргументы, пришедшие JSON-строками (сложные фильтры
* клиент сериализует в строку), в массивы значений.
*
* @param array $args
* @return array
*/
public function resultArguments($args)
{
$output = [];
foreach ($args as $key => $value)
{
$output[$key] = json_decode($value, true);
}
return $output;
}
}