mirror of
https://github.com/Suwayomi/Tachidesk.git
synced 2026-01-31 16:04:15 +01:00
Add Sources to Graphql
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* 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.SourceType
|
||||
import suwayomi.tachidesk.manga.impl.Source
|
||||
import suwayomi.tachidesk.manga.model.table.MangaTable
|
||||
import suwayomi.tachidesk.server.JavalinSetup.future
|
||||
|
||||
class SourceDataLoader : KotlinDataLoader<String, SourceType> {
|
||||
override val dataLoaderName = "SourceDataLoader"
|
||||
override fun getDataLoader(): DataLoader<String, SourceType> = DataLoaderFactory.newDataLoader<String, SourceType> { ids ->
|
||||
future {
|
||||
Source.getSourceList().filter { it.id in ids }
|
||||
.map { SourceType(it) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class SourceForMangaDataLoader : KotlinDataLoader<Int, SourceType?> {
|
||||
override val dataLoaderName = "SourceForMangaDataLoader"
|
||||
override fun getDataLoader(): DataLoader<Int, SourceType?> = DataLoaderFactory.newDataLoader<Int, SourceType?> { ids ->
|
||||
future {
|
||||
transaction {
|
||||
addLogger(StdOutSqlLogger)
|
||||
val mangaSourceMap = MangaTable
|
||||
.select { MangaTable.id inList ids }
|
||||
.associate { it[MangaTable.id].value to it[MangaTable.sourceReference] }
|
||||
|
||||
val sourceIds = mangaSourceMap
|
||||
.values
|
||||
.distinct()
|
||||
.map { it.toString() }
|
||||
|
||||
val sources = Source.getSourceList()
|
||||
.filter { it.id in sourceIds }
|
||||
.map { SourceType(it) }
|
||||
.associateBy { it.id }
|
||||
|
||||
val mangaSourceTypeMap = mangaSourceMap.mapValues {
|
||||
sources[it.value]
|
||||
}
|
||||
|
||||
ids.map {
|
||||
mangaSourceTypeMap[it]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* 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 suwayomi.tachidesk.graphql.types.SourceType
|
||||
import suwayomi.tachidesk.manga.impl.Source
|
||||
import java.util.concurrent.CompletableFuture
|
||||
|
||||
class SourceQuery {
|
||||
fun source(dataFetchingEnvironment: DataFetchingEnvironment, id: Long): CompletableFuture<SourceType> {
|
||||
return dataFetchingEnvironment.getValueFromDataLoader<Long, SourceType>("SourceDataLoader", id)
|
||||
}
|
||||
|
||||
fun sources(): List<SourceType> {
|
||||
return Source.getSourceList().map { SourceType(it) }
|
||||
}
|
||||
}
|
||||
@@ -21,7 +21,9 @@ class TachideskDataLoaderRegistryFactory {
|
||||
MangaMetaDataLoader(),
|
||||
MangaForCategoryDataLoader(),
|
||||
CategoryMetaDataLoader(),
|
||||
CategoriesForMangaDataLoader()
|
||||
CategoriesForMangaDataLoader(),
|
||||
SourceDataLoader(),
|
||||
SourceForMangaDataLoader()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ import suwayomi.tachidesk.graphql.mutations.ChapterMutation
|
||||
import suwayomi.tachidesk.graphql.queries.CategoryQuery
|
||||
import suwayomi.tachidesk.graphql.queries.ChapterQuery
|
||||
import suwayomi.tachidesk.graphql.queries.MangaQuery
|
||||
import suwayomi.tachidesk.graphql.queries.SourceQuery
|
||||
import suwayomi.tachidesk.graphql.subscriptions.DownloadSubscription
|
||||
import kotlin.reflect.KClass
|
||||
import kotlin.reflect.KType
|
||||
@@ -37,7 +38,8 @@ val schema = toSchema(
|
||||
queries = listOf(
|
||||
TopLevelObject(MangaQuery()),
|
||||
TopLevelObject(ChapterQuery()),
|
||||
TopLevelObject(CategoryQuery())
|
||||
TopLevelObject(CategoryQuery()),
|
||||
TopLevelObject(SourceQuery())
|
||||
),
|
||||
mutations = listOf(
|
||||
TopLevelObject(ChapterMutation())
|
||||
|
||||
@@ -95,4 +95,8 @@ class MangaType(
|
||||
fun categories(dataFetchingEnvironment: DataFetchingEnvironment): CompletableFuture<List<CategoryType>> {
|
||||
return dataFetchingEnvironment.getValueFromDataLoader<Int, List<CategoryType>>("CategoriesForMangaDataLoader", id)
|
||||
}
|
||||
|
||||
fun source(dataFetchingEnvironment: DataFetchingEnvironment): CompletableFuture<SourceType> {
|
||||
return dataFetchingEnvironment.getValueFromDataLoader<Int, SourceType>("SourceForMangaDataLoader", id)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* 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 suwayomi.tachidesk.manga.model.dataclass.SourceDataClass
|
||||
import java.util.concurrent.CompletableFuture
|
||||
|
||||
class SourceType(
|
||||
val id: Long,
|
||||
val name: String,
|
||||
val lang: String,
|
||||
val iconUrl: String,
|
||||
val supportsLatest: Boolean,
|
||||
val isConfigurable: Boolean,
|
||||
val isNsfw: Boolean,
|
||||
val displayName: String
|
||||
) {
|
||||
constructor(source: SourceDataClass) : this(
|
||||
id = source.id.toLong(),
|
||||
name = source.name,
|
||||
lang = source.lang,
|
||||
iconUrl = source.iconUrl,
|
||||
supportsLatest = source.supportsLatest,
|
||||
isConfigurable = source.isConfigurable,
|
||||
isNsfw = source.isNsfw,
|
||||
displayName = source.displayName
|
||||
)
|
||||
|
||||
fun manga(dataFetchingEnvironment: DataFetchingEnvironment): CompletableFuture<List<MangaType>> {
|
||||
return dataFetchingEnvironment.getValueFromDataLoader<Long, List<MangaType>>("MangaForSourceDataLoader", id)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user