Hello! Whats the approach for handling validation ...
# apollo-kotlin
m
Hello! Whats the approach for handling validation erros in apollo kotlin? I used to catch refresh token issues in
customAttributes
Now Im trying to update to apollo 3.4 and switched to
nonStandardFields
as suggested. But now when getting refresh token errors,
nonStandardFields
come as null and the actual erros come as:
Copy code
extensions = {errorClass=VALIDATION, validationErrors=[{message=Please make sure the refresh token is correct., inputPath=[input, input]}]}
extension being :
Map<String, Any?>?
🤔
m
Hi 👋 that's interesting! do you have the returned json by any chance?
m
Copy code
NetworkException(errors=[Error(message = Invalid data, locations = [Location(line = 1, column = 65)], path=[obtainCoconutToken], extensions = {errorClass=VALIDATION, validationErrors=[{message=Please make sure the refresh token is correct., inputPath=[input, input]}]}, nonStandardFields = null)])
would this work?
m
That helps!
Let me check
🙌 1
Looks like you can get the errorClass with something like this:
Copy code
response.errors[0].extensions["errorClass"] as? String
More recent versions of the spec now support "extensions". I guess this is why Apollo 2 didn't have support for this: https://spec.graphql.org/draft/#sel-GAPHRPfCCBCEoD2xG
m
Yeah that gives me the error class, but what about If I need to get the validationErrors message
Feels like it can become quite convoluted ( and am also tied to a string
errorClass
or
validationsError
that could easily change on an update an break my app?
m
Then you should be able to "cast" your way down:
Copy code
response.errors[0].extensions["validationErrors"] as? List<Map<String, Any?>>
Copy code
(response.errors[0].extensions["validationErrors"] as? List<Map<String, Any?>>).get(0).get("message")
m
yeah exactly, woudl this be the recommended way to go?
m
Yep.
m
https://github.com/apollographql/apollo-kotlin/issues/1964 I know its not the same but on apollo 2 and before seems like we could look for it in customAttributes so maybe it should be showing in non standardFields on 3.x and above?
Martin Bonnin [4:28 PM]
Yep.
Ah ok
well thanks then
m
I usually define helpers like this in my projects
Copy code
inline fun <reified T> Any?.cast() = this as T

val Any?.asMap: Map<String, Any?>
  get() = this.cast()
val Any?.asList: List<Any?>
  get() = this.cast()
val Any?.asString: String
  get() = this.cast()
val Any?.asBoolean: String
  get() = this.cast()
val Any?.asNumber: Number
  get() = this.cast()
This makes it easier to cast without breaking the flow
Copy code
response.errors[0].extensions["validationErrors"].asList[0].asMap["message"]
I know its not the same but on apollo 2 and before seems like we could look for it in customAttributes so maybe it should be showing in non standardFields on 3.x and above?
The ways your server answers is actually standard 🙂 . It's all in "extensions" so it's all good
m
yeah it looks better I guess
cool thanks again!
m
Sure thing!