简体   繁体   English

API 平台 - GraphQL - 如何查询计算字段?

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

We need to use Calculated Fields (the equivalent of JMSSerializer's @VirtualProperty for API Platform) and all is fine and dandy when we interact with the backend the old school way.我们需要使用计算字段(相当于 JMSSerializer 的 @VirtualProperty 用于 API 平台),当我们以老式方式与后端交互时,一切都很好。 However, we can't figure out how to query this calculated field on the GraphQL endpoint.但是,我们无法弄清楚如何在 GraphQL 端点上查询此计算字段。 For example:例如:

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

Regular query:常规查询:

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

Regular result:常规结果:

{
  "@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 query: GraphQL 查询:

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

GraphQL result: GraphQL 结果:

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

Thanks.谢谢。

It appears this was an issue with caching, specifically redis annotations cache.看来这是缓存的问题,特别是 redis 注释缓存。 Make sure you clear all caches, if you have such an issue.如果遇到此类问题,请确保清除所有缓存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM