简体   繁体   中英

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. However, we can't figure out how to query this calculated field on the GraphQL endpoint. 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:

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

GraphQL result:

{
  "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. Make sure you clear all caches, if you have such an issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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