apollo-kotlin
apollo-kotlin copied to clipboard
Combine multiple queries
Question
I saw that it is possible to combine multiple queries as one inside react: https://levelup.gitconnected.com/how-to-run-multiple-queries-at-once-using-graphqls-apollo-client-c24bea52e079
Is this also possible inside kotlin? Because I have a third-party backend that enforces rate-limitation and I don't want to blast it everytime I load an array.
Hi! 👋
From the article you linked, it looks like this technique would not actually help with rate limits:
Note: this will do a network call for each GraphQL operation.
Apollo Kotlin supports batching, which may help you if your particular backend supports it. Otherwise, your only solution would be to query multiple fields at once (like Solution 1 of the article you linked).
Hi @BoD,
Thank you for your answer. Unfortunately the backend doesn't support batching.
I see one solution, although it might be laborious: I could create a subclass or class companion for the query that takes an array and merges multiple queries into one. might be a nice feature for apollo to do so automatically though
I'm not completely sure of what you had in mind, but I think using fragments may help your use case. With them, you can issue single queries that return many fields, while still having them organized in manageable units in the generated code.
query ComplexQuery {
a { ... AFields }
b { ... BFields }
}
fragment AFields on A {
a1
a2
}
fragment BFields on B {
b1
b2
}
@DeWolfRobin I'm going to assume the fragments solution from @BoD did the trick and close this issue. Let me know if you want us to reopen