Add Sources to Graphql

This commit is contained in:
Syer10
2023-03-31 19:44:21 -04:00
parent 00370a81fa
commit 007d20d417
6 changed files with 135 additions and 2 deletions

View File

@@ -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]
}
}
}
}
}

View File

@@ -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) }
}
}

View File

@@ -21,7 +21,9 @@ class TachideskDataLoaderRegistryFactory {
MangaMetaDataLoader(),
MangaForCategoryDataLoader(),
CategoryMetaDataLoader(),
CategoriesForMangaDataLoader()
CategoriesForMangaDataLoader(),
SourceDataLoader(),
SourceForMangaDataLoader()
)
}
}

View File

@@ -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())

View File

@@ -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)
}
}

View File

@@ -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)
}
}