当不使用别名时,类型fieldsconflict的GraphQl验证误差即将到来



当我不使用请求中的别名时,我会收到类型fieldsconflict的错误"验证错误"。请确认是否可以预期或是否有解决方法

{
    person(search: [{firstname: "DAN", lastname: "WATLER", country: "FRANCE"}])
    { 
        firstname
        lastname
        country
        age
        address      
    }
    person(search: [{firstname: "FRANK", lastname: "TEE", country: "FRANCE"}])
    { 
        firstname
        lastname
        country
        age
        address      
    }
}

上面的代码给出了验证错误,但是如果我使用下面显示的别名,则错误不会出现,并且我会成功响应。

我不想使用别名,请建议如果有任何解决方法。谢谢!

{
    dan: person(search: [{firstname: "DAN", lastname: "WATLER", country: "FRANCE"}])
    { 
        firstname
        lastname
        country
        age
        address      
    }
    frank: person(search: [{firstname: "FRANK", lastname: "TEE", country: "FRANCE"}])
    { 
        firstname
        lastname
        country
        age
        address      
    }
}

通常GraphQL将数据返回为JSON对象,并且在具有相同密钥的JSON文档中不可能具有2个(有效)对象(在您的情况下,person)。因此,实现您所描述的目标是非常不可能的。

您第一个查询的结果将是:

{
  "data": {
    "person": {
      "firstname": "DAN",
      ...
    },
    "person": { // this is not valid
      "firstname": "FRANK"
      ...
    }
  }
}

这就是为什么您必须使用alias

另一个选项是查看GraphQl Server是否具有返回person列表的查询,结果将在数组中,例如:

{
  "data": [
    {
      "firstname": "DAN",
      ...
    },
    {
      "firstname": "FRANK",
      ...
    }
  [
}

最新更新