api 平台 graphql 查询解析器未触发

问题描述 投票:0回答:0

我有实体

Product

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\GraphQl\Query;
use ApiPlatform\Metadata\GraphQl\QueryCollection;
use App\UI\ApiPlatform\Resolver\ProducsCollectionResolver;
use App\UI\ApiPlatform\Resolver\ProductResolver;

#[ApiResource(
    graphQlOperations: [
        new Query(
            resolver: ProductResolver::class,
        ),
        new QueryCollection(
            resolver: ProducsCollectionResolver::class,
        ),
    ]
)]
class Product
{
    public function __construct(
        private string $id,
        public string $name
    ){}
}

和查询解析器:

use ApiPlatform\GraphQl\Resolver\QueryItemResolverInterface;
use App\UI\ApiPlatform\Entity\Product;

final class ProductResolver implements QueryItemResolverInterface
{
    public function __invoke($item, array $context): Product
    {
        dump('here');
        return new Product('id', 'name');
    }
}

/graphql
我可以看到带有ID和名称的产品资源,但正在运行查询

{
  product(id: "id") {
    name
  }
}

出现错误


  "errors": [
    {
      "message": "Internal server error",
      "locations": [
        {
          "line": 32,
          "column": 3
        }
      ],
      "path": [
        "product"
      ],
      "extensions": {
        "debugMessage": "No route matches \"586b8f112f7d8059668e5f73\".",
        "file": "/srv/app/vendor/api-platform/core/src/Symfony/Routing/IriConverter.php",
        "line": 69,
        "trace": [
          {
            "file": "/srv/app/vendor/api-platform/core/src/GraphQl/Resolver/Stage/ReadStage.php",
            "line": 99,
            "call": "ApiPlatform\\Symfony\\Routing\\IriConverter::getResourceFromIri()"
          },
  ...

看起来我的自定义解析器没有被触发,因为我没有看到我转储的调试字符串。我在 Symfony 中启用了自动装配,但我也尝试在带有标签

services.yaml
api_platform.graphql.query_resolver
中添加解析器,但仍然相同。

更新: 我也试过这样的查询:

{
  product(id: "/products/586b8f112f7d8059668e5f73") {
    id
  }
}

它也不起作用,但错误现在不同了

{
  "errors": [
    {
      "message": "Provider not found on operation \"_api_/products/{id}{._format}_get\"",
      "locations": [
        {
          "line": 32,
          "column": 3
        }
      ],
      "path": [
        "product"
      ],
symfony api-platform.com
© www.soinside.com 2019 - 2024. All rights reserved.