mirror of
https://github.com/Suwayomi/Tachidesk.git
synced 2026-02-01 08:24:19 +01:00
Add Extensions to Graphql
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* 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.ExtensionType
|
||||
import suwayomi.tachidesk.manga.model.table.ExtensionTable
|
||||
import suwayomi.tachidesk.manga.model.table.SourceTable
|
||||
import suwayomi.tachidesk.server.JavalinSetup.future
|
||||
|
||||
class ExtensionDataLoader : KotlinDataLoader<String, ExtensionType> {
|
||||
override val dataLoaderName = "ExtensionDataLoader"
|
||||
override fun getDataLoader(): DataLoader<String, ExtensionType> = DataLoaderFactory.newDataLoader { ids ->
|
||||
future {
|
||||
transaction {
|
||||
addLogger(StdOutSqlLogger)
|
||||
ExtensionTable.select { ExtensionTable.pkgName inList ids }
|
||||
.map { ExtensionType(it) }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class ExtensionForSourceDataLoader : KotlinDataLoader<Long, ExtensionType> {
|
||||
override val dataLoaderName = "ExtensionForSourceDataLoader"
|
||||
override fun getDataLoader(): DataLoader<Long, ExtensionType> = DataLoaderFactory.newDataLoader { ids ->
|
||||
future {
|
||||
transaction {
|
||||
addLogger(StdOutSqlLogger)
|
||||
ExtensionTable.innerJoin(SourceTable)
|
||||
.select { SourceTable.id inList ids }
|
||||
.map { ExtensionType(it) }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,7 @@ 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.model.table.ExtensionTable
|
||||
import suwayomi.tachidesk.manga.model.table.MangaTable
|
||||
import suwayomi.tachidesk.manga.model.table.SourceTable
|
||||
import suwayomi.tachidesk.server.JavalinSetup.future
|
||||
@@ -61,3 +62,22 @@ class SourceForMangaDataLoader : KotlinDataLoader<Int, SourceType?> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class SourcesForExtensionDataLoader : KotlinDataLoader<String, List<SourceType>> {
|
||||
override val dataLoaderName = "SourcesForExtensionDataLoader"
|
||||
override fun getDataLoader(): DataLoader<String, List<SourceType>> = DataLoaderFactory.newDataLoader { ids ->
|
||||
future {
|
||||
transaction {
|
||||
addLogger(StdOutSqlLogger)
|
||||
|
||||
val sourcesByExtensionPkg = SourceTable.innerJoin(ExtensionTable)
|
||||
.select { ExtensionTable.pkgName inList ids }
|
||||
.map { Pair(it[ExtensionTable.pkgName], SourceType(it)) }
|
||||
.groupBy { it.first }
|
||||
.mapValues { it.value.mapNotNull { pair -> pair.second } }
|
||||
|
||||
ids.map { sourcesByExtensionPkg[it] ?: emptyList() }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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.ExtensionType
|
||||
import suwayomi.tachidesk.manga.model.table.ExtensionTable
|
||||
import java.util.concurrent.CompletableFuture
|
||||
|
||||
class ExtensionQuery {
|
||||
fun extension(dataFetchingEnvironment: DataFetchingEnvironment, pkgName: String): CompletableFuture<ExtensionType> {
|
||||
return dataFetchingEnvironment.getValueFromDataLoader<String, ExtensionType>("ExtensionDataLoader", pkgName)
|
||||
}
|
||||
|
||||
fun extensions(): List<ExtensionType> {
|
||||
val results = transaction {
|
||||
ExtensionTable.selectAll().toList()
|
||||
}
|
||||
|
||||
return results.map { ExtensionType(it) }
|
||||
}
|
||||
}
|
||||
@@ -23,7 +23,10 @@ class TachideskDataLoaderRegistryFactory {
|
||||
CategoryMetaDataLoader(),
|
||||
CategoriesForMangaDataLoader(),
|
||||
SourceDataLoader(),
|
||||
SourceForMangaDataLoader()
|
||||
SourceForMangaDataLoader(),
|
||||
SourcesForExtensionDataLoader(),
|
||||
ExtensionDataLoader(),
|
||||
ExtensionForSourceDataLoader()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ import graphql.schema.GraphQLType
|
||||
import suwayomi.tachidesk.graphql.mutations.ChapterMutation
|
||||
import suwayomi.tachidesk.graphql.queries.CategoryQuery
|
||||
import suwayomi.tachidesk.graphql.queries.ChapterQuery
|
||||
import suwayomi.tachidesk.graphql.queries.ExtensionQuery
|
||||
import suwayomi.tachidesk.graphql.queries.MangaQuery
|
||||
import suwayomi.tachidesk.graphql.queries.SourceQuery
|
||||
import suwayomi.tachidesk.graphql.subscriptions.DownloadSubscription
|
||||
@@ -39,7 +40,8 @@ val schema = toSchema(
|
||||
TopLevelObject(MangaQuery()),
|
||||
TopLevelObject(ChapterQuery()),
|
||||
TopLevelObject(CategoryQuery()),
|
||||
TopLevelObject(SourceQuery())
|
||||
TopLevelObject(SourceQuery()),
|
||||
TopLevelObject(ExtensionQuery())
|
||||
),
|
||||
mutations = listOf(
|
||||
TopLevelObject(ChapterMutation())
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* 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.ExtensionTable
|
||||
import java.util.concurrent.CompletableFuture
|
||||
|
||||
class ExtensionType(
|
||||
val apkName: String,
|
||||
val iconUrl: String,
|
||||
|
||||
val name: String,
|
||||
val pkgName: String,
|
||||
val versionName: String,
|
||||
val versionCode: Int,
|
||||
val lang: String,
|
||||
val isNsfw: Boolean,
|
||||
|
||||
val installed: Boolean,
|
||||
val hasUpdate: Boolean,
|
||||
val obsolete: Boolean
|
||||
) {
|
||||
constructor(row: ResultRow) : this(
|
||||
apkName = row[ExtensionTable.apkName],
|
||||
iconUrl = row[ExtensionTable.iconUrl],
|
||||
name = row[ExtensionTable.name],
|
||||
pkgName = row[ExtensionTable.pkgName],
|
||||
versionName = row[ExtensionTable.versionName],
|
||||
versionCode = row[ExtensionTable.versionCode],
|
||||
lang = row[ExtensionTable.lang],
|
||||
isNsfw = row[ExtensionTable.isNsfw],
|
||||
installed = row[ExtensionTable.isInstalled],
|
||||
hasUpdate = row[ExtensionTable.hasUpdate],
|
||||
obsolete = row[ExtensionTable.isObsolete]
|
||||
)
|
||||
|
||||
fun source(dataFetchingEnvironment: DataFetchingEnvironment): CompletableFuture<List<SourceType>> {
|
||||
return dataFetchingEnvironment.getValueFromDataLoader<String, List<SourceType>>("SourcesForExtensionDataLoader", pkgName)
|
||||
}
|
||||
}
|
||||
@@ -55,14 +55,23 @@ class SourceType(
|
||||
fun manga(dataFetchingEnvironment: DataFetchingEnvironment): CompletableFuture<List<MangaType>> {
|
||||
return dataFetchingEnvironment.getValueFromDataLoader<Long, List<MangaType>>("MangaForSourceDataLoader", id)
|
||||
}
|
||||
|
||||
fun extension(dataFetchingEnvironment: DataFetchingEnvironment): CompletableFuture<ExtensionType> {
|
||||
return dataFetchingEnvironment.getValueFromDataLoader<Long, ExtensionType>("ExtensionForSourceDataLoader", id)
|
||||
}
|
||||
}
|
||||
|
||||
fun SourceType(row: ResultRow): SourceType? {
|
||||
val catalogueSource = GetCatalogueSource
|
||||
.getCatalogueSourceOrNull(row[SourceTable.id].value)
|
||||
?: return null
|
||||
val sourceExtension = ExtensionTable
|
||||
.select { ExtensionTable.id eq row[SourceTable.extension] }
|
||||
.first()
|
||||
val sourceExtension = if (row.hasValue(ExtensionTable.id)) {
|
||||
row
|
||||
} else {
|
||||
ExtensionTable
|
||||
.select { ExtensionTable.id eq row[SourceTable.extension] }
|
||||
.first()
|
||||
}
|
||||
|
||||
return SourceType(row, sourceExtension, catalogueSource)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user