簡體   English   中英

API 平台 - GraphQL - 如何查詢計算字段?

[英]API Platform - GraphQL - how to query for a Calculated Field?

我們需要使用計算字段(相當於 JMSSerializer 的 @VirtualProperty 用於 API 平台),當我們以老式方式與后端交互時,一切都很好。 但是,我們無法弄清楚如何在 GraphQL 端點上查詢此計算字段。 例如:

/**
     * Undocumented function.
     *
     * @Serializer\VirtualProperty()
     * @Groups({"read"})
     */
    public function getSlug()
    {
        return $this->name.$this->description;
    }

常規查詢:

curl -X GET "http://127.0.0.1:8000/platform/api/properties?page=1" -H "accept: application/ld+json"...

常規結果:

{
  "@context": "/platform/api/contexts/Property",
  "@id": "/platform/api/properties",
  "@type": "hydra:Collection",
  "hydra:member": [
    {
      "@id": "/platform/api/properties/1",
      "@type": "Property",
      "area": "0.00",
      "bathroomCount": 2,
      "bedroomCount": 2,
      "floorCount": 2,
      "description": "bar",
      "name": "foo",
      "slug": "foobar" // The calculated property
    },...

GraphQL 查詢:

{
  properties {
    edges {
      node {
        slug
      }
    }
  }
}

GraphQL 結果:

{
  "errors": [
    {
      "message": "Cannot query field \"slug\" on type \"Property\".",
      "extensions": {
        "category": "graphql"
      },
      "locations": [
        {
          "line": 5,
          "column": 9
        }
      ]
    }
  ]
}

謝謝。

看來這是緩存的問題,特別是 redis 注釋緩存。 如果遇到此類問題,請確保清除所有緩存。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM