[英]React-Apollo : Make query calls for a list of variables
Given a list of usernames and the following query to GITHUB API
:- 给定用户名列表和对
GITHUB API
的以下查询:-
query.gql:
query USER_QUERY($username:String!){
user(login: $username){
name
repositories(isFork:false){
totalCount
}
}
}
I want to create a composite query
for every username
. 我想为每个
username
创建一个composite query
。
Example: 例:
Let's say this is the list of usernames 假设这是用户名列表
array usernames=["Sheldon","Leonard","Raj","Holowitz"]
Expected Query: 预期查询:
{
sheldon:user(login: "sheldon") {
name
repositories(isFork: false) {
totalCount
}
}
leonard:user(login: "leonard") {
name
repositories(isFork: false) {
totalCount
}
}
raj:user(login: "raj") {
name
repositories(isFork: false) {
totalCount
}
}
holowitz:user(login: "holowitz") {
name
repositories(isFork: false) {
totalCount
}
}
Is there any react
ive-way to achieve the result? 有什么
react
的方法来达到目的吗?
There is no react
ive way for this - react is not for glue string templates. 对此没有
react
方法-反应不适用于胶粘线模板。
Your expected query is not constructed in the right graphQL way. 您的预期查询不是以正确的graphQL方式构建的。 It might work but you shouldn't think that way - when you need array - just ask for an array.
它可能有用,但是您不应该那样想-当您需要数组时-只需请求一个数组即可。 If you really need this kind of structure do it (conversion) after feching 'normal' array - it's a matter of one-liner
.filter()
fn. 如果您真的需要这种结构,可以在缓存“普通”数组后执行(转换)-这是
.filter()
fn的问题。 Don't expect this (strange) format (behavoiur) from universal response. 不要期望普遍响应会采用这种(奇怪的)格式(行为)。
Read related answers for this and this . 阅读相关答案这个和这个 。
Even calling separate queries may be batched by apollo. 甚至调用单独的查询也可能由apollo批处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.