Hi, I was wondering, could `kottlinx.serialization...
# serialization
b
Hi, I was wondering, could
kottlinx.serialization
be utilized to parse a
GraphQLType
(from graphql-java) and map it to a POJO without reflection costs? Using a custom format of some sort?
b
Otherwise you can write your custom serializer and register it into your Json module
v
kotlinx.serialization
is always without reflection, that's one of its core concepts. Only exception might be some specific methods that search for the serializer by reflection. But afair you get warned if you use those.
m
Usually you'd want to deserialize queries and not types? (a query contains fields of different types)
But to answer the original question, it depends where the POJO comes from. If you're writing the POJO yourself, you can for sure use kotlinx-serialization. Else you can use graphql-kotlin or apollo-android. They will generate models and parsers based on your queries automatically
m
I see a few issues which make it more difficult to use
kotlinx.serialization
for GraphQL parsing. 1) The structure of GraphQL responses is not static but depends on the query. If your queries are static and all result in a very predictable structure then this shouldn’t be an issue though. 2) Kotlin has no notion of union types so those will need special handling. 3) You need a serializer. If you’re in full control over the types then you can use annotations and model around the limitations of
kotlinx.serialization
. If not they you’d have to write your own serializers which is quite painful. Apart from that… A GraphQL response is simply JSON. Everything that’s JSON you can parse with
kotlinx.serialization
somehow. There are just some limitations you may have to work around.