英文:
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
短答案:不行。您需要依次运行这两个查询。然而,更好的方法是定义一个不同的查询,返回您所需要的name
和parsedName
。甚至更好的方法是扩展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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论