Hey Folks, we are facing an issue with v3 (continu...
# apollo-kotlin
a
Hey Folks, we are facing an issue with v3 (continuing migration from v2). It's kind of a weird ask bear with me, in v2 when an
Integer
overflows it never threw an error, but v3 throws an error e.g
java.lang.IllegalStateException: 2927716020 cannot be converted to Int
seemingly related to the exactInt check here. We are under the process of migrating that field to Long by introducing a new scalar type on our graph, but while we migrate to it, is there a workaround that could be used to safely cast to Integer with overflow on v3?
m
You can probably override the adapter for the “Int” scalar type to remove the check
Everything is named “custom scalar” but IIRC you can use it with built-in scalars, might depend your 3.x version
a
Thank you for the suggestion, we are on version
3.8.5
, I'll try the custom implementation
Sorry I have a few more questions first time working with this JsonReader API. 1. Setting the adapter on the apolloclient (
addCustomScalarAdapter(GraphQLInt.type, CustomIntegerAdapter)
) itself does not work do I have to set something on the apollo task on the build.gradle? 2. I am using a
nextString
to read the value from the reader would that make sense?
Copy code
class CustomIntegerAdapter : Adapter<Int> {
    override fun fromJson(reader: JsonReader, customScalarAdapters: CustomScalarAdapters) =
        reader.nextString()?.toInt() ?: 0

    override fun toJson(
        writer: JsonWriter,
        customScalarAdapters: CustomScalarAdapters,
        value: Int
    ) {
        writer.value(value)
    }
}
m
do I have to set something on the apollo task on the build.gradle?
Probably yes. If not the compiler will hardcode the IntAdapter, let me check quicly
I am using a
nextString
to read the value from the reader would that make sense?
Yes. This is how arbitrary precision numbers are read
They are all Strings ultimately
thank you color 1
Something like so should work:
Copy code
mapScalar("Int", "<http://kotlin.Int|kotlin.Int>", "com.example.MyIntAdapter")
Copy code
object MyIntAdapter: Adapter<Int> {
  override fun fromJson(reader: JsonReader, customScalarAdapters: CustomScalarAdapters): Int {
    return reader.nextString()!!.toLong().toInt()
  }

  override fun toJson(writer: JsonWriter, customScalarAdapters: CustomScalarAdapters, value: Int) {
    writer.value(value)
  }
}
a
Cool, yes was just replying I read the documentation. Thank you
m
Don’t trust me on the
toLong().toInt()
, arithmetic is hard but I think this is what you want
👀 1
a
I'll try it out. Thanks again.
m
Sure thing!