0

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
asked Jun 2, 2024 at 4:01
5
  • are you saying that if both count & lang array have 5 elements each you want to make 5 requests? Commented 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. Commented 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 one Commented 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. Commented 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? Commented Jun 3, 2024 at 10:57

1 Answer 1

0

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
Sign up to request clarification or add additional context in comments.

2 Comments

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.
not 30 queries but 30+ combinations of count and lang.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.