Here's your chance to speak at GraphQL Summit in New York City, October 8 - 10, 2024! 🏙️ Submit your proposal by May 31.
Docs
Launch GraphOS Studio
You're viewing documentation for a previous version of this software. Switch to the latest stable version.

Custom scalar types


Apollo supports custom scalar types, such as Date.

You first need to define the mapping in your build.gradle file. This maps from the type to the Java/Kotlin class to use in code.

apollo {
customTypeMapping = [
"Date" : "java.util.Date"
]
}
apollo {
customTypeMapping.set(mapOf(
"Date" to "java.util.Date"
))
}

Next, register your custom adapter and add it to your ApolloClient builder:

val dateCustomTypeAdapter = object : CustomTypeAdapter<Date> {
override fun decode(value: CustomTypeValue<*>): Date {
return try {
DATE_FORMAT.parse(value.value.toString())
} catch (e: ParseException) {
throw RuntimeException(e)
}
}
override fun encode(value: Date): CustomTypeValue<*> {
return GraphQLString(DATE_FORMAT.format(value))
}
}
ApolloClient.builder()
.serverUrl(serverUrl)
.addCustomTypeAdapter(CustomType.DATE, dateCustomTypeAdapter)
.build()
Previous
UI Tests
Next
Using without apollo-runtime
Edit on GitHubEditForumsDiscord

© 2024 Apollo Graph Inc.

Privacy Policy

Company