KtorReceiver

A receiver that uses Ktor to send queries and mutations.

The Receiver interface can be specified as a parameter for soil.query.SwrCachePolicy when creating an instance of soil.query.SwrCache.

val ktorClient = HttpClient {
install(ContentNegotiation) {
json()
}
}
val receiver = KtorReceiver(client = ktorClient)
val swrCache = SwrCache(policy = SwrCachePolicy(
..,
queryReceiver = receiver,
mutationReceiver = receiver,
..
))

If you use multiple receivers, create a single receiver that inherits from each of them.

class CustomReceiver(
ktorClient: HttpClient,
anotherClient: AnotherClient
): KtorReceiver by KtorReceiver(ktorClient), AnotherReceiver by AnotherReceiver(anotherClient)

By setting the receiver, you can use the builder functions designed for KtorReceiver when defining query and mutation keys. The fetch/mutate function block changes to the receiver type of the HttpClient, allowing direct calls to HttpClient's API.

class MyQueryKey: QueryKey<String> by buildKtorQueryKey(
id = QueryId("myQuery"),
fetch = { /* HttpClient.() -> String */
get("https://example.com").body()
}
)

See also

Properties

Link copied to clipboard
abstract val ktorClient: HttpClient