Chris Fillmore
08/03/2023, 8:49 PMChris Fillmore
08/03/2023, 8:50 PMChris Fillmore
08/03/2023, 8:53 PMID
typesChris Fillmore
08/03/2023, 8:54 PMmbonnin
08/03/2023, 10:14 PMmbonnin
08/03/2023, 10:14 PMmbonnin
08/03/2023, 10:23 PMobject GraphQLIDAdapter: Adapter<ID> {
override fun fromJson(reader: JsonReader, customScalarAdapters: CustomScalarAdapters): ID {
return ID(reader.nextString()!!)
}
override fun toJson(writer: JsonWriter, customScalarAdapters: CustomScalarAdapters, value: ID) {
writer.value(value.value)
}
}
If you go that route, make sure to register your adapter at build time to avoid boxing
mapScalar("ID", "ID", "GraphQLIDAdapter")
Chris Fillmore
08/03/2023, 10:42 PMChris Fillmore
08/03/2023, 10:43 PMStylianos Gakis
08/03/2023, 10:44 PMChris Fillmore
08/03/2023, 10:45 PMStylianos Gakis
08/03/2023, 10:45 PMID
then 1 adapter is enough. Or am I misunderstanding you here?Stylianos Gakis
08/03/2023, 10:45 PMmbonnin
08/03/2023, 11:04 PMtype Query {
user(id: UserId!): User
}
No risk of passing the "wrong" id. But I don't know of any API that does thismbonnin
08/03/2023, 11:06 PMNode
base type:
type Query {
node(id: ID): Node
}
Here the UserId
thing breaks. Unless you start adding inheritance on IDs but certainly not worth the troubleChris Fillmore
08/04/2023, 1:41 PMkotlinx.serialization
, so my value classes are all marked @Serializable
, so it’s not possible to confuse two fields like, for example productId: String
and productVariantId: String
, because they are typed as productId: ProductId
and productVariantId: VariantId
. This is genuinely useful.Chris Fillmore
08/04/2023, 1:45 PMChris Fillmore
08/04/2023, 1:57 PMapollo {
service("my-app") {
mapScalar("MyScalarId", "com.example.MyValueClass")
}
}
And so long as MyValueClass
conformed to an expected format, like this:
value class MyValueClass(val value: String)
Then an adapter could be generated automatically.Chris Fillmore
08/04/2023, 1:58 PMmbonnin
08/04/2023, 1:59 PMmbonnin
08/04/2023, 1:59 PM# generate adapters for those
scalar UserId
scalar ProductId
# but not for those
scalar Date
mbonnin
08/04/2023, 2:00 PMnode(id: ID): Node
argument makes it very unlikely that your backend ever uses different types for different idsChris Fillmore
08/04/2023, 2:00 PMChris Fillmore
08/04/2023, 2:01 PMAlso theAh ok. This part I’m not really clear onargument makes it very unlikely that your backend ever uses different types for different idsnode(id: ID): Node
mbonnin
08/04/2023, 2:01 PMChris Fillmore
08/04/2023, 2:02 PMmbonnin
08/04/2023, 2:02 PMmbonnin
08/04/2023, 2:02 PMApolloStore.read<T>(id: ID)
mbonnin
08/04/2023, 2:03 PMT
) to the id ID
but it sounds like a lot of complicationsChris Fillmore
08/04/2023, 2:03 PMmbonnin
08/04/2023, 2:04 PMChris Fillmore
08/04/2023, 2:05 PMmbonnin
08/04/2023, 2:05 PMgraphql.typed.ids
Gradle plugin that sits on top of Apollombonnin
08/04/2023, 2:06 PMChris Fillmore
08/04/2023, 2:07 PMmbonnin
08/04/2023, 2:07 PM