Sort gql queries by multiple columns (#963)

This commit is contained in:
schroda
2024-06-28 02:34:21 +02:00
committed by GitHub
parent aee9f1032c
commit 7c54ad54fc
8 changed files with 227 additions and 105 deletions

View File

@@ -7,6 +7,7 @@
package suwayomi.tachidesk.graphql.queries
import com.expediagroup.graphql.generator.annotations.GraphQLDeprecated
import com.expediagroup.graphql.server.extensions.getValueFromDataLoader
import graphql.schema.DataFetchingEnvironment
import org.jetbrains.exposed.sql.Column
@@ -30,6 +31,7 @@ import suwayomi.tachidesk.graphql.queries.filter.andFilterWithCompareEntity
import suwayomi.tachidesk.graphql.queries.filter.andFilterWithCompareString
import suwayomi.tachidesk.graphql.queries.filter.applyOps
import suwayomi.tachidesk.graphql.server.primitives.Cursor
import suwayomi.tachidesk.graphql.server.primitives.Order
import suwayomi.tachidesk.graphql.server.primitives.OrderBy
import suwayomi.tachidesk.graphql.server.primitives.PageInfo
import suwayomi.tachidesk.graphql.server.primitives.QueryResults
@@ -105,6 +107,11 @@ class ChapterQuery {
}
}
data class ChapterOrder(
override val by: ChapterOrderBy,
override val byType: SortOrder? = null,
) : Order<ChapterOrderBy>
data class ChapterCondition(
val id: Int? = null,
val url: String? = null,
@@ -195,8 +202,17 @@ class ChapterQuery {
fun chapters(
condition: ChapterCondition? = null,
filter: ChapterFilter? = null,
@GraphQLDeprecated(
"Replaced with order",
replaceWith = ReplaceWith("order"),
)
orderBy: ChapterOrderBy? = null,
@GraphQLDeprecated(
"Replaced with order",
replaceWith = ReplaceWith("order"),
)
orderByType: SortOrder? = null,
order: List<ChapterOrder>? = null,
before: Cursor? = null,
after: Cursor? = null,
first: Int? = null,
@@ -217,17 +233,15 @@ class ChapterQuery {
res.applyOps(condition, filter)
if (orderBy != null || (last != null || before != null)) {
val orderByColumn = orderBy?.column ?: ChapterTable.id
val orderType = orderByType.maybeSwap(last ?: before)
if (order != null || (last != null || before != null)) {
val baseSort = listOf(ChapterOrder(ChapterOrderBy.ID, SortOrder.ASC))
val deprecatedSort = listOfNotNull(orderBy?.let { ChapterOrder(orderBy, orderByType) })
val actualSort = (order.orEmpty() + deprecatedSort + baseSort)
actualSort.forEach { (orderBy, orderByType) ->
val orderByColumn = orderBy.column
val orderType = orderByType.maybeSwap(last ?: before)
if (orderBy == ChapterOrderBy.ID || orderBy == null) {
res.orderBy(orderByColumn to orderType)
} else {
res.orderBy(
orderByColumn to orderType,
ChapterTable.id to SortOrder.ASC,
)
}
}
@@ -238,8 +252,8 @@ class ChapterQuery {
res.applyBeforeAfter(
before = before,
after = after,
orderBy = orderBy ?: ChapterOrderBy.ID,
orderByType = orderByType,
orderBy = order?.firstOrNull()?.by ?: ChapterOrderBy.ID,
orderByType = order?.firstOrNull()?.byType,
)
if (first != null) {
@@ -251,7 +265,7 @@ class ChapterQuery {
QueryResults(total, firstResult, lastResult, res.toList())
}
val getAsCursor: (ChapterType) -> Cursor = (orderBy ?: ChapterOrderBy.ID)::asCursor
val getAsCursor: (ChapterType) -> Cursor = (order?.firstOrNull()?.by ?: ChapterOrderBy.ID)::asCursor
val resultsAsType = queryResults.results.map { ChapterType(it) }