overblog / GraphQLBundle

This bundle provides tools to build a complete GraphQL API server in your Symfony App.
MIT License
788 stars 222 forks source link

How can I get list of queried fields in my attribute mapped resolver? #977

Open Makapashev opened 2 years ago

Makapashev commented 2 years ago
Q A
Bug report? no
Feature request? no
BC Break report? no
RFC? no
Version/Branch 0.14.1

Hello! In my project i'm using attribute as mapping type. How can I get list of queried fields in my resolver?

<?php

declare(strict_types=1);

namespace App\Category\Presentation;

use App\Category\Domain\CategoryRepositoryInterface;
use Overblog\GraphQLBundle\Annotation as GQL;
use Overblog\GraphQLBundle\Definition\Resolver\QueryInterface;

#[GQL\Provider]
final class CatalogResolver implements QueryInterface
{
    private CategoryRepositoryInterface $repository;

    public function __construct(CategoryRepositoryInterface $repository)
    {
        $this->repository = $repository;
    }

    #[GQL\Query(name: "categories", type: "[Category]")]
    public function getCategories(): array
    {
        return $this->repository->getCategories();
    }
}
calvera commented 2 years ago

via expression info


info | A GraphQL\Type\Definition\ResolveInfo object of current resolver

see https://github.com/overblog/GraphQLBundle/blob/0.14/docs/definitions/expression-language.md#registered-variables

my code is

#[GQL\Provider]
class MovementReportResolver implements QueryInterface
{
    public function __construct(private MovementReportRepositoryInterface $movementReportRepository)
    {
    }

    /**
     * @return MovementReportRecord[]
     */
    #[GQL\Query(name: 'movementReport', type: '[MovementReportRecord!]!', resolve: "query('App\\\\GraphQL\\\\Query\\\\Warehouse\\\\MovementReportResolver', args, info)")]
    #[GQL\Arg(name: 'first', type: 'Int')]
    #[GQL\Arg(name: 'sort', type: '[MovementReportSort!]')]
    #[GQL\Arg(name: 'filter', type: '[MovementReportFilter!]')]
    #[GQL\Access(value: "hasPermission('movement', 'READ')")]
    public function __invoke(Argument $args, ResolveInfo $info): array
    {
        $filter = $args['filter'];
        $sort = $args['sort'];
        $first = $args['first'];

        return $this->movementReportRepository->report(
            array_keys($info->getFieldSelection(16)),
            $filter,
            $sort,
            $first
        );
    }
}
Makapashev commented 2 years ago

via expression info


info | A GraphQL\Type\Definition\ResolveInfo object of current resolver

see https://github.com/overblog/GraphQLBundle/blob/0.14/docs/definitions/expression-language.md#registered-variables

my code is

#[GQL\Provider]
class MovementReportResolver implements QueryInterface
{
    public function __construct(private MovementReportRepositoryInterface $movementReportRepository)
    {
    }

    /**
     * @return MovementReportRecord[]
     */
    #[GQL\Query(name: 'movementReport', type: '[MovementReportRecord!]!', resolve: "query('App\\\\GraphQL\\\\Query\\\\Warehouse\\\\MovementReportResolver', args, info)")]
    #[GQL\Arg(name: 'first', type: 'Int')]
    #[GQL\Arg(name: 'sort', type: '[MovementReportSort!]')]
    #[GQL\Arg(name: 'filter', type: '[MovementReportFilter!]')]
    #[GQL\Access(value: "hasPermission('movement', 'READ')")]
    public function __invoke(Argument $args, ResolveInfo $info): array
    {
        $filter = $args['filter'];
        $sort = $args['sort'];
        $first = $args['first'];

        return $this->movementReportRepository->report(
            array_keys($info->getFieldSelection(16)),
            $filter,
            $sort,
            $first
        );
    }
}

Thanks, but I got an error message Failed to parse GraphQL metadata from file "/var/www/src/QueryType.php". Do you know how to solve this?

Makapashev commented 2 years ago

@calvera My QueryType file contains:

<?php

declare(strict_types=1);

namespace App;

use Overblog\GraphQLBundle\Annotation as GQL;

#[GQL\Type]
final class QueryType
{
}