-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The popular TanStack Query library in the React ecosystem has a feature known as [Dependent Query](https://tanstack.com/query/v5/docs/framework/react/guides/dependent-queries). Currently, Soil Query does not have an API for conditionally executing queries. Therefore, we have implemented the Optional Query feature to enable queries based on certain conditions, such as the results of other queries. Previously, developers could achieve similar functionality by manually implementing conditional branching, but this new feature can replace that approach.
- Loading branch information
1 parent
d027a36
commit adbd2fb
Showing
9 changed files
with
613 additions
and
4 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
soil-query-compose/src/commonMain/kotlin/soil/query/compose/InfiniteQueryComposables.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright 2024 Soil Contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package soil.query.compose | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
import soil.query.InfiniteQueryKey | ||
import soil.query.QueryChunks | ||
import soil.query.QueryClient | ||
|
||
/** | ||
* Provides a conditional [rememberInfiniteQuery]. | ||
* | ||
* Calls [rememberInfiniteQuery] only if [keyFactory] returns a [InfiniteQueryKey] from [value]. | ||
* | ||
* @see rememberInfiniteQuery | ||
*/ | ||
@Composable | ||
fun <T, S, V> rememberInfiniteQueryIf( | ||
value: V, | ||
keyFactory: (value: V) -> InfiniteQueryKey<T, S>?, | ||
config: InfiniteQueryConfig = InfiniteQueryConfig.Default, | ||
client: QueryClient = LocalQueryClient.current | ||
): InfiniteQueryObject<QueryChunks<T, S>, S>? { | ||
val key = remember(value) { keyFactory(value) } ?: return null | ||
return rememberInfiniteQuery(key, config, client) | ||
} | ||
|
||
/** | ||
* Provides a conditional [rememberInfiniteQuery]. | ||
* | ||
* Calls [rememberInfiniteQuery] only if [keyFactory] returns a [InfiniteQueryKey] from [value]. | ||
* | ||
* @see rememberInfiniteQuery | ||
*/ | ||
@Composable | ||
fun <T, S, U, V> rememberInfiniteQueryIf( | ||
value: V, | ||
keyFactory: (value: V) -> InfiniteQueryKey<T, S>?, | ||
select: (chunks: QueryChunks<T, S>) -> U, | ||
config: InfiniteQueryConfig = InfiniteQueryConfig.Default, | ||
client: QueryClient = LocalQueryClient.current | ||
): InfiniteQueryObject<U, S>? { | ||
val key = remember(value) { keyFactory(value) } ?: return null | ||
return rememberInfiniteQuery(key, select, config, client) | ||
} |
27 changes: 27 additions & 0 deletions
27
soil-query-compose/src/commonMain/kotlin/soil/query/compose/MutationComposables.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2024 Soil Contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package soil.query.compose | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
import soil.query.MutationClient | ||
import soil.query.MutationKey | ||
|
||
/** | ||
* Provides a conditional [rememberMutation]. | ||
* | ||
* Calls [rememberMutation] only if [keyFactory] returns a [MutationKey] from [value]. | ||
* | ||
* @see rememberMutation | ||
*/ | ||
@Composable | ||
fun <T, S, V> rememberMutationIf( | ||
value: V, | ||
keyFactory: (value: V) -> MutationKey<T, S>?, | ||
config: MutationConfig = MutationConfig.Default, | ||
client: MutationClient = LocalMutationClient.current | ||
): MutationObject<T, S>? { | ||
val key = remember(value) { keyFactory(value) } ?: return null | ||
return rememberMutation(key, config, client) | ||
} |
107 changes: 107 additions & 0 deletions
107
soil-query-compose/src/commonMain/kotlin/soil/query/compose/QueryComposables.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// Copyright 2024 Soil Contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package soil.query.compose | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
import soil.query.QueryClient | ||
import soil.query.QueryKey | ||
import kotlin.jvm.JvmName | ||
|
||
/** | ||
* Provides a conditional [rememberQuery]. | ||
* | ||
* Calls [rememberQuery] only if [keyFactory] returns a [QueryKey] from [value]. | ||
* | ||
* @see rememberQuery | ||
*/ | ||
@Composable | ||
fun <T, V> rememberQueryIf( | ||
value: V, | ||
keyFactory: (value: V) -> QueryKey<T>?, | ||
config: QueryConfig = QueryConfig.Default, | ||
client: QueryClient = LocalQueryClient.current | ||
): QueryObject<T>? { | ||
val key = remember(value) { keyFactory(value) } ?: return null | ||
return rememberQuery(key, config, client) | ||
} | ||
|
||
/** | ||
* Provides a conditional [rememberQuery]. | ||
* | ||
* Calls [rememberQuery] only if [keyFactory] returns a [QueryKey] from [value]. | ||
* | ||
* @see rememberQuery | ||
*/ | ||
@Composable | ||
fun <T, U, V> rememberQueryIf( | ||
value: V, | ||
keyFactory: (value: V) -> QueryKey<T>?, | ||
select: (T) -> U, | ||
config: QueryConfig = QueryConfig.Default, | ||
client: QueryClient = LocalQueryClient.current | ||
): QueryObject<U>? { | ||
val key = remember(value) { keyFactory(value) } ?: return null | ||
return rememberQuery(key, select, config, client) | ||
} | ||
|
||
/** | ||
* Provides a conditional [rememberQuery]. | ||
* | ||
* Calls [rememberQuery] only if [keyPairFactory] returns a [Pair] of [QueryKey]s from [value]. | ||
* | ||
* @see rememberQuery | ||
*/ | ||
@JvmName("rememberQueryIfWithPair") | ||
@Composable | ||
fun <T1, T2, R, V> rememberQueryIf( | ||
value: V, | ||
keyPairFactory: (value: V) -> Pair<QueryKey<T1>, QueryKey<T2>>?, | ||
transform: (T1, T2) -> R, | ||
config: QueryConfig = QueryConfig.Default, | ||
client: QueryClient = LocalQueryClient.current | ||
): QueryObject<R>? { | ||
val keyPair = remember(value) { keyPairFactory(value) } ?: return null | ||
return rememberQuery(keyPair.first, keyPair.second, transform, config, client) | ||
} | ||
|
||
/** | ||
* Provides a conditional [rememberQuery]. | ||
* | ||
* Calls [rememberQuery] only if [keyTripleFactory] returns a [Triple] of [QueryKey]s from [value]. | ||
* | ||
* @see rememberQuery | ||
*/ | ||
@JvmName("rememberQueryIfWithTriple") | ||
@Composable | ||
fun <T1, T2, T3, R, V> rememberQueryIf( | ||
value: V, | ||
keyTripleFactory: (value: V) -> Triple<QueryKey<T1>, QueryKey<T2>, QueryKey<T3>>?, | ||
transform: (T1, T2, T3) -> R, | ||
config: QueryConfig = QueryConfig.Default, | ||
client: QueryClient = LocalQueryClient.current | ||
): QueryObject<R>? { | ||
val keyTriple = remember(value) { keyTripleFactory(value) } ?: return null | ||
return rememberQuery(keyTriple.first, keyTriple.second, keyTriple.third, transform, config, client) | ||
} | ||
|
||
/** | ||
* Provides a conditional [rememberQuery]. | ||
* | ||
* Calls [rememberQuery] only if [keyListFactory] returns a [List] of [QueryKey]s from [value]. | ||
* | ||
* @see rememberQuery | ||
*/ | ||
@JvmName("rememberQueryIfWithList") | ||
@Composable | ||
fun <T, R, V> rememberQueryIf( | ||
value: V, | ||
keyListFactory: (value: V) -> List<QueryKey<T>>?, | ||
transform: (List<T>) -> R, | ||
config: QueryConfig = QueryConfig.Default, | ||
client: QueryClient = LocalQueryClient.current | ||
): QueryObject<R>? { | ||
val keys = remember(value) { keyListFactory(value) } ?: return null | ||
return rememberQuery(keys, transform, config, client) | ||
} |
49 changes: 49 additions & 0 deletions
49
soil-query-compose/src/commonMain/kotlin/soil/query/compose/SubscriptionComposables.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright 2024 Soil Contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package soil.query.compose | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
import soil.query.SubscriptionClient | ||
import soil.query.SubscriptionKey | ||
import soil.query.annotation.ExperimentalSoilQueryApi | ||
|
||
/** | ||
* Provides a conditional [rememberSubscription]. | ||
* | ||
* Calls [rememberSubscription] only if [keyFactory] returns a [SubscriptionKey] from [value]. | ||
* | ||
* @see rememberSubscription | ||
*/ | ||
@ExperimentalSoilQueryApi | ||
@Composable | ||
fun <T, V> rememberSubscriptionIf( | ||
value: V, | ||
keyFactory: (value: V) -> SubscriptionKey<T>?, | ||
config: SubscriptionConfig = SubscriptionConfig.Default, | ||
client: SubscriptionClient = LocalSubscriptionClient.current | ||
): SubscriptionObject<T>? { | ||
val key = remember(value) { keyFactory(value) } ?: return null | ||
return rememberSubscription(key, config, client) | ||
} | ||
|
||
/** | ||
* Provides a conditional [rememberSubscription]. | ||
* | ||
* Calls [rememberSubscription] only if [keyFactory] returns a [SubscriptionKey] from [value]. | ||
* | ||
* @see rememberSubscription | ||
*/ | ||
@ExperimentalSoilQueryApi | ||
@Composable | ||
fun <T, U, V> rememberSubscriptionIf( | ||
value: V, | ||
keyFactory: (value: V) -> SubscriptionKey<T>?, | ||
select: (T) -> U, | ||
config: SubscriptionConfig = SubscriptionConfig.Default, | ||
client: SubscriptionClient = LocalSubscriptionClient.current | ||
): SubscriptionObject<U>? { | ||
val key = remember(value) { keyFactory(value) } ?: return null | ||
return rememberSubscription(key, select, config, client) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.