GraphQLTransportCodec
Backend-neutral contract for serializing GraphQL requests and responses.
GraphQLTransportCodec is the seam between the neutral protocol contracts in :api and a concrete JSON backend (Gson, kotlinx.serialization, Moshi, and so on). It deliberately references no serializer, Android, Retrofit, or OkHttp types: implementations only see GraphQLOperationRequest, raw String bodies, and Type tokens.
This is the contract future runtime code (APQ support, converter rewiring) will invoke. Implementations should be stateless and thread-safe.
Exception policy
Implementations must never leak backend-specific exceptions. Any failure while encoding or decoding must be wrapped in GraphQLRequestEncodingException or GraphQLResponseDecodingException respectively, so callers can attach operation and Type context to the failure without depending on the backend.
Usage
class MyCodec(private val json: MyJson) : GraphQLTransportCodec {
override fun encodeRequest(
request: GraphQLOperationRequest<*>,
requestType: Type,
): String = try {
json.encode(request, requestType)
} catch (cause: Exception) {
throw GraphQLRequestEncodingException(
operationName = request.operationName,
requestType = requestType,
message = "Failed to encode GraphQL request",
cause = cause,
)
}
override fun <T : Any> decodeResponse(
body: String,
responseType: Type,
): T = try {
json.decode(body, responseType)
} catch (cause: Exception) {
throw GraphQLResponseDecodingException(
responseType = responseType,
message = "Failed to decode GraphQL response",
cause = cause,
)
}
}