通过API平台在GraphQL模式中进行自定义类型

发布于 2025-01-23 18:10:42 字数 454 浏览 5 评论 0原文

我正在使用Symfony中的Postgres Extension Postgis和LeckitudeOne软件包。 因此,在我的实体中,我有一个称为坐标的属性,这是“点”类型。 但是,当我试图通过GraphQl查询一个实体集合时,该属性不在我的模式中...问题肯定来自自定义类型...

我想得到的是

query
{
  destinations
  {
    collection
    {
     id
     coordinates
    }
  }
}

我拥有的:

query
{
  destinations
  {
    collection
    {
     id
    }
  }
}

如果我强制坐标属性,它说:

无法在类型“目标”上查询字段“坐标”。

I'm using postgres extension POSTGIS and longitudeOne package in Symfony.
So in my entity I have an attribute called coordinates which is a "point" type.
But when I'm trying to get through a graphQL query a collection of entities, the attribute is not in my schema... The issue surely comes from the custom type but I don't find a way to make it recognize in my schema...

What I would like to get is

query
{
  destinations
  {
    collection
    {
     id
     coordinates
    }
  }
}

What I have:

query
{
  destinations
  {
    collection
    {
     id
    }
  }
}

And if I force the coordinates attributes, it says :

Cannot query field "coordinates" on type "Destination".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

氛圍 2025-01-30 18:10:42

通过2.8版本中的API平台GraphQL,仅允许基本类型,例如字符串,INT和数组。当您通过Symfony CLI创建对实体的新属性时,将自动创建Getters和Setter。

例如:

如果我创建一个称为newentity的新实体。我将有一个setNewentity
方法和getNewentity方法,基于类型
实体具有。

GET方法很重要,API平台将使用它来返回GraphQL响应中的数据。在这里,您可以按照所需的方式格式化数据。

在我的几何示例中,当我创建“ mycoordinates”属性时,我将自动生成一个getMyCoordinates属性。它还将自动返回“点”类型的答案。 GraphQl无法读取。因此,您可以修改返回基本类型的GET方法,在这里,我修改了返回数组类型而不是点类型的GET方法:

#[ORM\Column(type: "point")]
private $myCoordinates;

public function getMyCoordinates(): ?array
{
    if ($this->myCoordinates) {
        return [
            "longitude" => $this->myCoordinates->getLongitude(),
            "latitude" => $this->myCoordinates->getLatitude()
        ];
    } else {
        return null;
    }
}

GraphQL through api platform in the 2.8 version, only allows basic types, as string, int and arrays. When you create a new attribute to the entity through the symfony cli, the getters and the setters are automatically created.

For example:

If I create a new entity called NewEntity. I will have a setNewEntity
method and a getNewEntity method, that are based on the type the
entity have.

The get method is important has it will be used by api platform to return the data in your graphql response. And this is where you can format the data the way you want.

In my geometry example, when I create the 'myCoordinates' attribute, I will have a getMyCoordinates attribute automatically generated. It will also automatically return an answer of type 'point'. Which can't be read by graphQL. So you can modify the get method to return a basic type, here I modify the get method to return an array type instead of a point type:

#[ORM\Column(type: "point")]
private $myCoordinates;

public function getMyCoordinates(): ?array
{
    if ($this->myCoordinates) {
        return [
            "longitude" => $this->myCoordinates->getLongitude(),
            "latitude" => $this->myCoordinates->getLatitude()
        ];
    } else {
        return null;
    }
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文