public interface CustomTypeAdapter<T>
Date:
CustomTypeAdapter<Date> dateCustomTypeAdapter = new CustomTypeAdapter<Date>() {
public Date decode(CustomTypeValue value) {
try {
return ISO8601_DATE_FORMAT.parse(value);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
public CustomTypeValue encode(Date value) {
return ISO8601_DATE_FORMAT.format(value);
}
};
To use a custom type adapter with Apollo, you must first define the mapping in the build.gradle file:
apollo {
customTypeMapping['DateTime'] = "java.util.Date"
}
The lines above tell the compiler which type to use while generating the classes. Once this is done, register the
type adapter with com.apollographql.apollo.ApolloClient.Builder:
ApolloClient.Builder builder = ApolloClient.builder()
.addCustomTypeAdapter(CustomType.DATE, dateCustomTypeAdapter);
In the example code above, the CustomType.DATE is the class generated by the compiler.| Modifier and Type | Method and Description |
|---|---|
T |
decode(CustomTypeValue value)
De-serializes the value to the custom scalar type.
|
CustomTypeValue |
encode(T value)
Serializes the custom scalar type to the corresponding
CustomTypeValue value. |
T decode(@NotNull CustomTypeValue value)
value - the value to de-serialize@NotNull CustomTypeValue encode(@NotNull T value)
CustomTypeValue value. Usually used in serializing variables or input
values.value - the custom scalar type to serialize