绕过 GraphQL 中的 “nested” 对象

huangapple go评论53阅读模式
英文:

bypass "nested" objects in GraphQL

问题

I have next gql fragment:

fragment PropertyInformationStateFragment on Property{
property_state{
data{
attributes{
state
description
}
}
}
}

and here is example of data returned:

"property_state": {
"data": {
"attributes": {
"state": "Tres bon état",
"description": "Tres bon état du bien immobilier"
}
}
}

Do somebody knows how to "bypass" this data and attributes objects?

Data returned I expect:

"property_state": {
"state": "blabla",
"description": "blabla"
}

What should I do?

英文:

I have next gql fragment:

fragment PropertyInformationStateFragment on Property{
    property_state{
      data{
        attributes{
          state
          description
        }
      }
    }
}

and here is example of data returned:

"property_state": {
  "data": {
    "attributes": {
      "state": "Tres bon état",
      "description": "Tres bon état du bien immobilier"
    }
  }
}

Do somebody knows how to "bypass" this data and attributes objects?

Data returned I expect:

"property_state": {
  "state": "blabla",
  "description": "blabla"
}

What should I do?

答案1

得分: 1

在这五年里,没有任何改变。仍然没有方法可以做到,我不认为将来会有改变。对此感到抱歉。

这是因为 GraphQL 只允许客户端定义响应中包含哪些字段或更改字段名称,但不允许更改响应结构。你的查询必须遵循服务器定义的类型结构。服务器只能以这个定义的结构返回数据,但你现在要求的是以完全不同的结构返回数据。

你可以在客户端手动将其转换为你想要的结构,这应该非常容易。

英文:

Nothing changed in these 5 years. There are still no ways to do it and I don't think it will have in the future. Sorry about that.

It is because GraphQL only allows client to define which fields to be included in the response or changing the field name but not allow change the response structure. Your query must following the type structure defined by the server. The server can only return you the data in this defined structure but what you are asking now is to return the data in a total different structure.

What you can do is to convert it to the structure that you want manually in the client side which should be a piece of cake.

huangapple
  • 本文由 发表于 2023年2月24日 00:49:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75547868.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定