Join us from October 8-10 in New York City to learn the latest tips, trends, and news about GraphQL Federation and API platform engineering.Join us for GraphQL Summit 2024 in NYC
Docs
Start for Free
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
Rate articleRateEdit on GitHubEditForumsDiscord

© 2024 Apollo Graph Inc., d/b/a Apollo GraphQL.

Privacy Policy

Company