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

Colton Idle

03/06/2023, 1:35 PM
My schema has a field that is defined as a Date, but when looking at the query I'm generating that fetches that field, it gets resolved to
Any
. Is there something I'm missing here?
s

Stylianos Gakis

03/06/2023, 1:38 PM
What’s the type it has? If it’s not a built-in type that apollo-kotlin knows about, I guess it resolves as
Any
? Do you have an
com.apollographql.apollo3.api.Adapter
for it? Perhaps like we do here, with this adapter.
With that said, you may be able to use one of the officially provided optional adapters, as explained here.
c

Colton Idle

03/06/2023, 1:50 PM
Am I missing something with adding the adapter?
Copy code
.addCustomScalarAdapter(Date.type, DateAdapter())
doesn't compile.
m

mbonnin

03/06/2023, 1:51 PM
You'll need to register your custom scalar at build time make the codegen aware of the type
Something like:
Copy code
apollo {
  service("service") {
    mapScalar("Date", "java.util.Date", "com.apollographql.apollo3.adapter.DateAdapter")
  }
}
Date
is not in the GraphQL spec so there's nothing special about it, it could be "Any" type by default
The mapping tells the codegen what type to use
If you can, I'd avoid
java.util.Date
though and prefer
kotlinx.datetime.Instant
c

Colton Idle

03/06/2023, 1:58 PM
Interesting. So I guess I have to ask my serverteam what type
2023-02-22T20:32:04.000Z
is?
m

mbonnin

03/06/2023, 1:58 PM
"Time is hard" 😅 so I'm not 100% sure but that looks like an UTC instant
s

Stylianos Gakis

03/06/2023, 1:58 PM
If it’s following the
ISO8601
spec, then you’re ok with either.
m

mbonnin

03/06/2023, 1:59 PM
Is the
Z
part of iso8601? I never know
s

Stylianos Gakis

03/06/2023, 1:59 PM
And yeah, this “looks” correct, but I wouldn’t bet on it 😂 I have no idea either
m

mbonnin

03/06/2023, 2:00 PM
That works 🎉
Copy code
val instant = Instant.parse("2023-02-22T20:32:04.000Z")

println(instant.epochSeconds)
@Colton Idle go with
kotlinx.datetime.Instant
and save you future Java Date troubles
c

Colton Idle

03/06/2023, 2:02 PM
Cool. So I'll use KotlinxInstantAdapter
m

mbonnin

03/06/2023, 2:02 PM
Copy code
dependencies {
  implementation("com.apollographql.apollo3:apollo-adapters")
}

apollo {
  service("service") {
    mapScalar("Date", "kotlinx.datetime.Instant", "com.apollographql.apollo3.adapter.KotlinxInstantAdapter")
  }
}
c

Colton Idle

03/06/2023, 2:03 PM
Oh. I still need the mapScalar in the build.gradle file?
m

mbonnin

03/06/2023, 2:03 PM
Yep, codegen is decided at build time
c

Colton Idle

03/06/2023, 2:03 PM
And then this?
Copy code
.addCustomScalarAdapter(Date.type, KotlinxInstantAdapter)
m

mbonnin

03/06/2023, 2:03 PM
You can actually skip that part because the 3 params
mapScalar
will "burn" the adapters in the codegen
It's slightly faster because there's no need to lookup the adapter at runtime
c

Colton Idle

03/06/2023, 2:04 PM
oh. interesting!
m

mbonnin

03/06/2023, 2:05 PM
(there's also a more simple
mapScalar(String, String)
without the third parameter that leaves the adapter to be registered at runtime)
c

Colton Idle

03/06/2023, 2:17 PM
Is there a way to turn on debugging or logging or something for apollo. after enabling all of this, im not getting any responses anymore and im wondering if something is crashing internally in apollo?
m

mbonnin

03/06/2023, 2:19 PM
We've been trying to stay clear of log statements for these reasons. But there are a few places where you can register hooks. One of the useful ones is adding an OkHttp HttpLogginInterceptor
c

Colton Idle

03/06/2023, 2:20 PM
Okay. but any sort of crashes in apollo would likely just crash my app too right?
m

mbonnin

03/06/2023, 2:21 PM
Unless you catch it 😄
Maybe try the query in a unit test? You can then debug
(but you can debug on device too actually)
Just unit tests remove coroutines stuff, lifecycle, etc... could be easier to isolate the issue
c

Colton Idle

03/06/2023, 2:23 PM
weird. uninstalled my app and reinstalled and now everything seems to be good to go again
m

mbonnin

03/06/2023, 2:24 PM
I blame cosmic rays 💫 🙂
c

Colton Idle

03/06/2023, 2:27 PM
Thought I had it working... 😭 "Cannot access class 'kotlinx.datetime.Instant'. Check your module classpath for missing or conflicting dependencies"
Only dep I added was apollo adapters. hmmmm.
m

mbonnin

03/06/2023, 2:29 PM
apollo-adapters
has an API dependency on
kotlinx-datetime
so it "should" be seen
Is that an Android Studio thing or command line thing?
s

Stylianos Gakis

03/06/2023, 2:30 PM
adapters dependency exposes kotlinx-datetime dependency https://github.com/apollographql/apollo-kotlin/blob/main/libraries/apollo-adapters/build.gradle.kts#L18 so not adding the dep yourself shouldn’t be an issue 👀
c

Colton Idle

03/06/2023, 2:30 PM
looks good in studio. but only get that error when i run.
Hm. maybe because my apollo kotlin stuff is in another module, thats used by my main app module. let me change implementation to api for apollo adapters dep 🤞
oooh. that did it. WOOT WOOT
very cool! Thanks everyone for teaching!
while im here. kotlin instant doesn't have
isBefore
method? I gotta use toJavaInstant? interesting...
m

mbonnin

03/06/2023, 2:37 PM
There is
.compareTo
I'm pretty sure you can do evrything without going to Java
At least haven't bumped into anything like that just yet
c

Colton Idle

03/06/2023, 2:38 PM
Cool. just making sure. new to apollo and kotlin datetime so thats fun. but yeah. im sure google can take it from here. thanks for the quick sanity check!
Had to move to JavaLocalDateTimeAdapter instead. But now getting an exception during runtime from apollo. JavaLocalDateTimeAdapter has an example of: Examples: • "2010-06-01T221944.475" but my date is
2023-02-22T20:32:04.000Z
so I guess the Z is the culprit making it crash?
hm. Switching to instant makes it work again. Any chance theres an easy way to jsut skip the conversion to a date or time or anything and just go right from a Date to a String?
b

bod

03/10/2023, 6:56 AM
Haha, dates are hard 😅 Well yes you could map it to a String directly with
mapScalarToKotlinString
in the grade configuration. But in the end you really don't want a string but some kind of date object, right? If you're going to do a conversion, you might as well do it inside an Apollo adapter (if none of the provided ones work for you, you can write a custom adapter).
s

Stylianos Gakis

03/10/2023, 6:59 AM
Had to move to JavaLocalDateTimeAdapter instead.
Why?
c

Colton Idle

03/11/2023, 8:41 PM
@Stylianos Gakis idk. i kept asking, them seemed to "know" better. lol And @bod thanks! cool about being able to map to a string. teammate suggested that because we apparently have string to date converters in our mapping layer
139 Views