admin管理员组

文章数量:1131160

My configuration Query.yaml

Query:
  type: object
  config:
    fields:
      allUsers:
        type: "[User]!" # Array type Users
        resolve: "@=query('App\\Domain\\User\\Api\\GraphQL\\Query\\UserQuery::resolveAllUsers')"
        description: "Отримати список усіх користувачів."
      user:
        type: "User"
        args:
          id:
            type: "ID!"
            description: "ID user."
        resolve: "@=query('App\\Domain\\User\\Api\\GraphQL\\Query\\UserQuery::resolveUserById', [args['id']])"
        description: "Found user by ID."

Class UserQuery.php

<?php

declare(strict_types=1);

namespace App\Domain\User\Api\GraphQL\Query;

use App\Domain\User\Repository\UserRepository;
use Overblog\GraphQLBundle\Definition\Resolver\QueryInterface;

class UserQuery implements QueryInterface
{
    private array $users;

    public function __construct(private readonly UserRepository $userRepository)
    {
        $this->users = [
            ['id' => 1, 'name' => 'John Doe', 'email' => '[email protected]'],
            ['id' => 2, 'name' => 'Jane Smith', 'email' => '[email protected]'],
        ];
    }


    public function resolveAllUsers(): array
    {
        return $this->users;
    }


    public function resolveUserById($id): ?array
    {
        foreach ($this->users as $user) {
            if ($user['id'] === (int)$id) {
                return $user;
            }
        }

        return null;
    }
}

For the directory App\Domain\User\Api\GraphQL, auto-discovery is configured as follows:

App\Domain\User\Api\GraphQL\:
    resource: '../src/Domain/User/Api/GraphQL'

Running the command php bin/console cache:clear successfully registers my services in GraphQL Query Services.

However, when GraphQLBundle processes a response, it expects an alias like 'AppDomainUserApiGraphQLQueryUserQuery'.

After inspecting the generated files, I found the following call:

return $services->query("AppDomainUserApiGraphQLQueryUserQuery::resolveUserById", [0 => $args["id"]]);

Here, the backslashes (\) were removed during generation.

To resolve this issue, I had to modify the configuration in Query.yaml from:

resolve: "@=query('App\\Domain\\User\\Api\\GraphQL\\Query\\UserQuery::resolveAllUsers')"

to:

resolve: "@=query('App\\\\Domain\\\\User\\\\Api\\\\GraphQL\\\\Query\\\\UserQuery::resolveAllUsers')"

After making this change, the functionality started working. Is there perhaps a better solution to this problem?

本文标签: