可以构建一个 GraphQL 查询,其中查询的第二部分依赖于第一部分的结果吗?

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

Is it possible to build GraphQL query where the second part of the query depends on the result of the first one?

问题

我有一个设置,在前端通过路由器(Apollo Federation Gateway)运行查询到两个分别提供GQL端点的服务 - serviceA有findItems,serviceB有parseName

现在我想一次运行以下查询:

query findMyItemDescriptionNameAndParse {
  findItems(id: 1) {
    description {
      name
    }
  }
  parseName(input: $name) {
     parsedName {
        name
     }
  }
}

Apollo能否在内部传递变量,还是我必须将查询拆分成两个查询来处理这种情况?

谢谢。

英文:

I have a setup where I run a query on the frontend through the router (Apollo Federation Gateway) to two separate services exposing GQL endpoints - serviceA has findItems and serviceB has parseName.

Now I want to run the following query in one go:

query findMyItemDescriptionNameAndParse {
  findItems(id: 1) {
    description {
      name
    }
  }
  parseName(input: $name) {
     parsedName {
        name
     }
  }
}

Can Apollo pass the variables internally or I just have to split the query into two for such case?

Thank you.

答案1

得分: 1

短答案:不行。您需要依次运行这两个查询。然而,更好的方法是定义一个不同的查询,返回您所需要的nameparsedName。甚至更好的方法是扩展description以包括一个parsedName字段,然后只需为其编写一个解析程序,这样您就不需要运行顺序查询。

英文:

Short answer: no. You'd have to run the two queries in sequence. What's preferable however is to define a different query that returns both the name and parsedName that you're looking for. Even better would be to extend the description to include a parsedName field and just write a resolver for that then you'd never need to run sequential queries.

huangapple
  • 本文由 发表于 2023年2月18日 02:20:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75487972.html
匿名

发表评论

匿名网友

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

确定