-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
169 additions
and
6 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
server/src/main/kotlin/suwayomi/tachidesk/graphql/dataLoaders/CategoryDataLoader.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 (C) Contributors to the Suwayomi project | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */ | ||
|
||
package suwayomi.tachidesk.graphql.dataLoaders | ||
|
||
import com.expediagroup.graphql.dataloader.KotlinDataLoader | ||
import org.dataloader.DataLoader | ||
import org.dataloader.DataLoaderFactory | ||
import org.jetbrains.exposed.sql.StdOutSqlLogger | ||
import org.jetbrains.exposed.sql.addLogger | ||
import org.jetbrains.exposed.sql.select | ||
import org.jetbrains.exposed.sql.transactions.transaction | ||
import suwayomi.tachidesk.graphql.types.CategoryType | ||
import suwayomi.tachidesk.manga.model.table.CategoryMangaTable | ||
import suwayomi.tachidesk.manga.model.table.CategoryTable | ||
import java.util.concurrent.CompletableFuture | ||
|
||
class CategoryDataLoader : KotlinDataLoader<Int, CategoryType> { | ||
override val dataLoaderName = "CategoryDataLoader" | ||
override fun getDataLoader(): DataLoader<Int, CategoryType> = DataLoaderFactory.newDataLoader<Int, CategoryType> { ids -> | ||
CompletableFuture.supplyAsync { | ||
transaction { | ||
addLogger(StdOutSqlLogger) | ||
CategoryTable.select { CategoryTable.id inList ids } | ||
.map { CategoryType(it) } | ||
} | ||
} | ||
} | ||
} | ||
|
||
class CategoriesForMangaDataLoader : KotlinDataLoader<Int, List<CategoryType>> { | ||
override val dataLoaderName = "CategoriesForMangaDataLoader" | ||
override fun getDataLoader(): DataLoader<Int, List<CategoryType>> = DataLoaderFactory.newDataLoader<Int, List<CategoryType>> { ids -> | ||
CompletableFuture.supplyAsync { | ||
transaction { | ||
addLogger(StdOutSqlLogger) | ||
val itemsByRef = CategoryMangaTable.innerJoin(CategoryTable).select { CategoryMangaTable.manga inList ids } | ||
.map { Pair(it[CategoryMangaTable.manga].value, CategoryType(it)) } | ||
.groupBy { it.first } | ||
.mapValues { it.value.map { pair -> pair.second } } | ||
ids.map { itemsByRef[it] ?: emptyList() } | ||
} | ||
} | ||
} | ||
} |
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
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
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
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
30 changes: 30 additions & 0 deletions
30
server/src/main/kotlin/suwayomi/tachidesk/graphql/queries/CategoryQuery.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,30 @@ | ||
/* | ||
* Copyright (C) Contributors to the Suwayomi project | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */ | ||
|
||
package suwayomi.tachidesk.graphql.queries | ||
|
||
import com.expediagroup.graphql.server.extensions.getValueFromDataLoader | ||
import graphql.schema.DataFetchingEnvironment | ||
import org.jetbrains.exposed.sql.selectAll | ||
import org.jetbrains.exposed.sql.transactions.transaction | ||
import suwayomi.tachidesk.graphql.types.CategoryType | ||
import suwayomi.tachidesk.manga.model.table.CategoryTable | ||
import java.util.concurrent.CompletableFuture | ||
|
||
class CategoryQuery { | ||
fun category(dataFetchingEnvironment: DataFetchingEnvironment, id: Int): CompletableFuture<CategoryType> { | ||
return dataFetchingEnvironment.getValueFromDataLoader<Int, CategoryType>("CategoryDataLoader", id) | ||
} | ||
|
||
fun categories(): List<CategoryType> { | ||
val results = transaction { | ||
CategoryTable.selectAll().toList() | ||
} | ||
|
||
return results.map { CategoryType(it) } | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
server/src/main/kotlin/suwayomi/tachidesk/graphql/types/CategoryType.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,36 @@ | ||
/* | ||
* Copyright (C) Contributors to the Suwayomi project | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */ | ||
|
||
package suwayomi.tachidesk.graphql.types | ||
|
||
import com.expediagroup.graphql.server.extensions.getValueFromDataLoader | ||
import graphql.schema.DataFetchingEnvironment | ||
import org.jetbrains.exposed.sql.ResultRow | ||
import suwayomi.tachidesk.manga.model.table.CategoryTable | ||
import java.util.concurrent.CompletableFuture | ||
|
||
class CategoryType( | ||
val id: Int, | ||
val order: Int, | ||
val name: String, | ||
val default: Boolean | ||
) { | ||
constructor(row: ResultRow) : this( | ||
row[CategoryTable.id].value, | ||
row[CategoryTable.order], | ||
row[CategoryTable.name], | ||
row[CategoryTable.isDefault] | ||
) | ||
|
||
fun manga(dataFetchingEnvironment: DataFetchingEnvironment): CompletableFuture<List<MangaType>> { | ||
return dataFetchingEnvironment.getValueFromDataLoader<Int, List<MangaType>>("MangaForCategoryDataLoader", id) | ||
} | ||
|
||
fun meta(dataFetchingEnvironment: DataFetchingEnvironment): CompletableFuture<MetaType> { | ||
return dataFetchingEnvironment.getValueFromDataLoader<Int, MetaType>("CategoryMetaDataLoader", id) | ||
} | ||
} |
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
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