My requirement is to run a query with multiple variables.
type Record {
asset(count: String!, lang: String!): Cat!
}
query ($count: String!, $lang: Str!) {
asset(coun: $count, lang: $lang) {
vid {
orig
}
}
}
{
count:"abc", lang:"def"
}
This works fine, but I want to run for multiple combinations of count and lang. For example:
{
count:["abc", "def"],
lang:["a", "b"]
}
I want to send count as "abc" and lang as "a" in one request, and then count as "def" and lang as "b" in another request. How to do this in graphql?
I tried to change query definition to query ($count: [String!], $lang: [Str!]), but it's not working. I read there is a way as alias, but I have large query and more than 10 combination.
Ken White
126k15 gold badges237 silver badges476 bronze badges
-
are you saying that if both count & lang array have 5 elements each you want to make 5 requests?sid– sid2024年06月02日 04:30:50 +00:00Commented Jun 2, 2024 at 4:30
-
@sid yes and in future this might increase to 30 or 50 combinations as well. Also the number of fields that i want in return are large in number, around 20 to 25 fields.vishal– vishal2024年06月02日 04:43:34 +00:00Commented Jun 2, 2024 at 4:43
-
why not just make a single request and have your resolver return all data in one request? usually developers want to optimise api calls and therefore batch requests into onesid– sid2024年06月02日 04:59:48 +00:00Commented Jun 2, 2024 at 4:59
-
The server is owned by 3rd party. We have no information about resolver. I will ask them if they have resolver written or not.vishal– vishal2024年06月02日 05:09:15 +00:00Commented Jun 2, 2024 at 5:09
-
@sid: If my function asset() in. type "Record" itself just accepts count and lang as just strings and not array of string, do you think its still possible to send multiple combination of count and lang if there is a resolver? if yes, can you show an example of resolver for this request?vishal– vishal2024年06月03日 10:57:23 +00:00Commented Jun 3, 2024 at 10:57
1 Answer 1
You will define aliases for each combination of count and lang:
query {
asset1: asset(count: "abc", lang: "a") {
vid {
orig
}
}
asset2: asset(count: "def", lang: "b") {
vid {
orig
}
}
# Add more
}
answered Jun 2, 2024 at 4:34
M Junaid
9882 gold badges7 silver badges27 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
vishal
but lets say i have 30 queries or it may increase more and the return fields are also large in number, then what is the best option or is there any alternative? If i use alias then i have put same query n number of times, where n is number of different possible combinations. And i dont think this is a scalable approach.
vishal
not 30 queries but 30+ combinations of count and lang.