Skip to content
This repository has been archived by the owner on Feb 10, 2019. It is now read-only.

How to output paging data? #391

Open
lilianjin opened this issue Oct 4, 2018 · 2 comments
Open

How to output paging data? #391

lilianjin opened this issue Oct 4, 2018 · 2 comments

Comments

@lilianjin
Copy link

lilianjin commented Oct 4, 2018

PHP

class ProductsQuery extends Query
{
	protected $attributes = [
		'name'	=> 'Products Query'
	];

	public function type()
	{
		return GraphQL::pagination(
				GraphQL::type('Product')
			);
	}

	public function resolve($root, $args)
	{
		return app(ProductRepositoryInterface::class)
			->paginate([
				'per_page'	=> 6
			]);
	}
}

Query

{
    products {
      items {
        name
        str_id
        description
     }
   }
}
@ikudosi
Copy link

ikudosi commented Oct 12, 2018

Hope this helps but here's an example of what I have.

class ModelWithPageInfo extends Folklore\GraphQL\Support\Type
{
    protected $attributes = [
        'name' => '',
        'description' => ''
    ];

    /**
     * @var string
     */
    protected $graphqlType;

    public function fields()
    {
        return [
            'pageInfo' => [
                'type' => GraphQL::type('PageInfo'),
                'resolve' => function ($root, $args) {
                    return [
                        'per_page' => $root->perPage(),
                        'current_page' => $root->currentPage(),
                        'total' => $root->total(),
                        'result_count' => $root->count()
                    ];
                }
            ],
            'hasMorePages' => [
                'type' => Type::boolean(),
                'resolve' => function ($root) {
                    return $root->hasMorePages();
                }
            ],
            'items' => [
                'type' => Type::listOf(GraphQL::type($this->graphqlType)),
                'resolve' => function ($root, $args) {
                    return $root->items();
                }
            ]
        ];
    }
}
class FoobarWithPageInfo extends ModelWithPageInfo
{
    protected $attributes = [
        'name' => 'FoobarWithPageInfo'
    ];

    protected $graphqlType = 'Foobar';
}
class PaginatedModelQuery extends Folklore\GraphQL\Support\Query
{
    public function type()
    {
        return GraphQL::type('ModelWithPageInfo ');
    }

    /**
     * @return array
     */
    public function args()
    {
        return ['name' => 'page', 'description' => 'The page', 'type' => Type::int()]);
    }

    /**
     * @return mixed
     */
    protected function resolve($root, $args, $context, ResolveInfo $info)
    {
        return Foobar::paginate($args['limit'] ?? 20, ['*'], 'page', $args['page'] ?? 1);
    }
}

@lilianjin
Copy link
Author

@ikudosi
Thank you, I have solved this problem.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants