https://kotlinlang.org logo
#apollo-kotlin
Title
# apollo-kotlin
w

wasyl

03/27/2024, 10:11 AM
👋 I have a question about data builders — from what I see, the builders are pretty much generated from the schema, and the queries only contribute a helper function with top-level properties. This makes it very flexible, in the sense that there's no help from the compiler regarding e.g. which fields are fetched. For example given
Copy code
query Foo { 
  bar { baz }
}
the builders allow me to write
Copy code
Foo.Builder {
 bar = buildBar { 
    someUnrelatedPropertyInBar = ""
    // I'm allowed to leave `baz` unset
  }
}
is that right or I'm missing something? Is there a way for a less flexible API, one that mirrors the queries and would give a typesafe way to build a json for a given data (one that forces me to pass the fetched fields, and doesn't allow passing fields that aren't fetched)?
1
b

bod

03/27/2024, 10:18 AM
That's correct, they are based on the schema. Actually the previous iteration (that we called 'test builders') were query based, but they were prone to a potential 'exponential explosion' of generated code because they were response based.
w

wasyl

03/27/2024, 10:20 AM
can you give an example of such explosion? Wouldn't such safe API need ~one method per level of nesting in the queries, where each level is a method and each field is an argument?
I understand it would require several methods for each query, but it'd still be linear (with maybe large constant 😄) and not necessarily exponential 🤔
b

bod

03/27/2024, 10:24 AM
It's about polymorphism - each
... on Xyz
fragment can have a different shape, which needs to generate its own model - and if you have nested ones that leads to a lot of combinations. You can read about this in this issue about the responseBased codegen 🙂
w

wasyl

03/27/2024, 10:27 AM
I'll take a look, thanks 🙂 And finally a lazyweb question — I vaguely recall something about Apollo artifacts that might help with parsing schema/queries in Kotlin, is there something like that that might help me building/generating my own builders?
b

bod

03/27/2024, 10:28 AM
you're probably thinking of Apollo AST
w

wasyl

03/27/2024, 10:30 AM
❤️ perfect, thanks a lot!
b

bod

03/27/2024, 10:32 AM
sure thing! (re data builders: one advantage with them being based on the schema is that you can re-use the data for different tests)
w

wasyl

03/27/2024, 10:39 AM
what do you mean different tests? 🤔
b

bod

03/27/2024, 10:43 AM
I mean you can reuse the result of
buildXyz {}
and not be bound to a specific query
(assuming you're using this for tests, but actually don't know 😊)
w

wasyl

03/27/2024, 10:51 AM
yeah, in practice we'd have a fragment in such case, which I'd imagine would be reusable 🙂
👍 1