apollo-kotlin icon indicating copy to clipboard operation
apollo-kotlin copied to clipboard

Combine multiple queries

Open DeWolfRobin opened this issue 3 years ago • 3 comments

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.

DeWolfRobin avatar Jul 26 '22 08:07 DeWolfRobin

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).

BoD avatar Jul 26 '22 08:07 BoD

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

DeWolfRobin avatar Jul 26 '22 09:07 DeWolfRobin

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
}

BoD avatar Jul 26 '22 10:07 BoD

@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

martinbonnin avatar Sep 09 '22 20:09 martinbonnin