-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #547 from Suwayomi/graphql
add graphql
- Loading branch information
Showing
45 changed files
with
4,389 additions
and
2 deletions.
There are no files selected for viewing
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
23 changes: 23 additions & 0 deletions
23
server/src/main/kotlin/suwayomi/tachidesk/graphql/GraphQL.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,23 @@ | ||
/* | ||
* 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 | ||
|
||
import io.javalin.apibuilder.ApiBuilder.get | ||
import io.javalin.apibuilder.ApiBuilder.post | ||
import io.javalin.apibuilder.ApiBuilder.ws | ||
import suwayomi.tachidesk.graphql.controller.GraphQLController | ||
|
||
object GraphQL { | ||
fun defineEndpoints() { | ||
post("graphql", GraphQLController::execute) | ||
ws("graphql", GraphQLController::webSocket) | ||
|
||
// graphql playground | ||
get("graphql", GraphQLController::playground) | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
server/src/main/kotlin/suwayomi/tachidesk/graphql/controller/GraphQLController.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,42 @@ | ||
/* | ||
* 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.controller | ||
|
||
import io.javalin.http.Context | ||
import io.javalin.websocket.WsConfig | ||
import suwayomi.tachidesk.graphql.server.TachideskGraphQLServer | ||
import suwayomi.tachidesk.server.JavalinSetup.future | ||
|
||
object GraphQLController { | ||
private val server = TachideskGraphQLServer.create() | ||
|
||
/** execute graphql query */ | ||
fun execute(ctx: Context) { | ||
ctx.future( | ||
future { | ||
server.execute(ctx) | ||
} | ||
) | ||
} | ||
|
||
fun playground(ctx: Context) { | ||
val body = javaClass.getResourceAsStream("/graphql-playground.html")!!.bufferedReader().use { reader -> | ||
reader.readText() | ||
} | ||
ctx.html(body) | ||
} | ||
|
||
fun webSocket(ws: WsConfig) { | ||
ws.onMessage { ctx -> | ||
server.handleSubscriptionMessage(ctx) | ||
} | ||
ws.onClose { ctx -> | ||
server.handleSubscriptionDisconnect(ctx) | ||
} | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
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,54 @@ | ||
/* | ||
* 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.Slf4jSqlDebugLogger | ||
import org.jetbrains.exposed.sql.addLogger | ||
import org.jetbrains.exposed.sql.select | ||
import org.jetbrains.exposed.sql.transactions.transaction | ||
import suwayomi.tachidesk.graphql.types.CategoryNodeList | ||
import suwayomi.tachidesk.graphql.types.CategoryNodeList.Companion.toNodeList | ||
import suwayomi.tachidesk.graphql.types.CategoryType | ||
import suwayomi.tachidesk.manga.model.table.CategoryMangaTable | ||
import suwayomi.tachidesk.manga.model.table.CategoryTable | ||
import suwayomi.tachidesk.server.JavalinSetup.future | ||
|
||
class CategoryDataLoader : KotlinDataLoader<Int, CategoryType?> { | ||
override val dataLoaderName = "CategoryDataLoader" | ||
override fun getDataLoader(): DataLoader<Int, CategoryType?> = DataLoaderFactory.newDataLoader { ids -> | ||
future { | ||
transaction { | ||
addLogger(Slf4jSqlDebugLogger) | ||
val categories = CategoryTable.select { CategoryTable.id inList ids } | ||
.map { CategoryType(it) } | ||
.associateBy { it.id } | ||
ids.map { categories[it] } | ||
} | ||
} | ||
} | ||
} | ||
|
||
class CategoriesForMangaDataLoader : KotlinDataLoader<Int, CategoryNodeList> { | ||
override val dataLoaderName = "CategoriesForMangaDataLoader" | ||
override fun getDataLoader(): DataLoader<Int, CategoryNodeList> = DataLoaderFactory.newDataLoader<Int, CategoryNodeList> { ids -> | ||
future { | ||
transaction { | ||
addLogger(Slf4jSqlDebugLogger) | ||
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()).toNodeList() } | ||
} | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
server/src/main/kotlin/suwayomi/tachidesk/graphql/dataLoaders/ChapterDataLoader.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,51 @@ | ||
/* | ||
* 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.Slf4jSqlDebugLogger | ||
import org.jetbrains.exposed.sql.addLogger | ||
import org.jetbrains.exposed.sql.select | ||
import org.jetbrains.exposed.sql.transactions.transaction | ||
import suwayomi.tachidesk.graphql.types.ChapterNodeList | ||
import suwayomi.tachidesk.graphql.types.ChapterNodeList.Companion.toNodeList | ||
import suwayomi.tachidesk.graphql.types.ChapterType | ||
import suwayomi.tachidesk.manga.model.table.ChapterTable | ||
import suwayomi.tachidesk.server.JavalinSetup.future | ||
|
||
class ChapterDataLoader : KotlinDataLoader<Int, ChapterType?> { | ||
override val dataLoaderName = "ChapterDataLoader" | ||
override fun getDataLoader(): DataLoader<Int, ChapterType?> = DataLoaderFactory.newDataLoader<Int, ChapterType> { ids -> | ||
future { | ||
transaction { | ||
addLogger(Slf4jSqlDebugLogger) | ||
val chapters = ChapterTable.select { ChapterTable.id inList ids } | ||
.map { ChapterType(it) } | ||
.associateBy { it.id } | ||
ids.map { chapters[it] } | ||
} | ||
} | ||
} | ||
} | ||
|
||
class ChaptersForMangaDataLoader : KotlinDataLoader<Int, ChapterNodeList> { | ||
override val dataLoaderName = "ChaptersForMangaDataLoader" | ||
override fun getDataLoader(): DataLoader<Int, ChapterNodeList> = DataLoaderFactory.newDataLoader<Int, ChapterNodeList> { ids -> | ||
future { | ||
transaction { | ||
addLogger(Slf4jSqlDebugLogger) | ||
val chaptersByMangaId = ChapterTable.select { ChapterTable.manga inList ids } | ||
.map { ChapterType(it) } | ||
.groupBy { it.mangaId } | ||
ids.map { (chaptersByMangaId[it] ?: emptyList()).toNodeList() } | ||
} | ||
} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
server/src/main/kotlin/suwayomi/tachidesk/graphql/dataLoaders/ExtensionDataLoader.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,63 @@ | ||
/* | ||
* 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.Slf4jSqlDebugLogger | ||
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(Slf4jSqlDebugLogger) | ||
val extensions = ExtensionTable.select { ExtensionTable.pkgName inList ids } | ||
.map { ExtensionType(it) } | ||
.associateBy { it.pkgName } | ||
ids.map { extensions[it] } | ||
} | ||
} | ||
} | ||
} | ||
|
||
class ExtensionForSourceDataLoader : KotlinDataLoader<Long, ExtensionType?> { | ||
override val dataLoaderName = "ExtensionForSourceDataLoader" | ||
override fun getDataLoader(): DataLoader<Long, ExtensionType?> = DataLoaderFactory.newDataLoader { ids -> | ||
future { | ||
transaction { | ||
addLogger(Slf4jSqlDebugLogger) | ||
val extensions = ExtensionTable.innerJoin(SourceTable) | ||
.select { SourceTable.id inList ids } | ||
.toList() | ||
.map { Triple(it[SourceTable.id].value, it[ExtensionTable.pkgName], it) } | ||
.let { triples -> | ||
val sources = buildMap { | ||
triples.forEach { | ||
if (!containsKey(it.second)) { | ||
put(it.second, ExtensionType(it.third)) | ||
} | ||
} | ||
} | ||
triples.associate { | ||
it.first to sources[it.second] | ||
} | ||
} | ||
ids.map { extensions[it] } | ||
} | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
server/src/main/kotlin/suwayomi/tachidesk/graphql/dataLoaders/MangaDataLoader.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,53 @@ | ||
/* | ||
* 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.Slf4jSqlDebugLogger | ||
import org.jetbrains.exposed.sql.addLogger | ||
import org.jetbrains.exposed.sql.select | ||
import org.jetbrains.exposed.sql.transactions.transaction | ||
import suwayomi.tachidesk.graphql.types.MangaNodeList | ||
import suwayomi.tachidesk.graphql.types.MangaNodeList.Companion.toNodeList | ||
import suwayomi.tachidesk.graphql.types.MangaType | ||
import suwayomi.tachidesk.manga.model.table.CategoryMangaTable | ||
import suwayomi.tachidesk.manga.model.table.MangaTable | ||
import suwayomi.tachidesk.server.JavalinSetup.future | ||
|
||
class MangaDataLoader : KotlinDataLoader<Int, MangaType?> { | ||
override val dataLoaderName = "MangaDataLoader" | ||
override fun getDataLoader(): DataLoader<Int, MangaType?> = DataLoaderFactory.newDataLoader { ids -> | ||
future { | ||
transaction { | ||
addLogger(Slf4jSqlDebugLogger) | ||
val manga = MangaTable.select { MangaTable.id inList ids } | ||
.map { MangaType(it) } | ||
.associateBy { it.id } | ||
ids.map { manga[it] } | ||
} | ||
} | ||
} | ||
} | ||
|
||
class MangaForCategoryDataLoader : KotlinDataLoader<Int, MangaNodeList> { | ||
override val dataLoaderName = "MangaForCategoryDataLoader" | ||
override fun getDataLoader(): DataLoader<Int, MangaNodeList> = DataLoaderFactory.newDataLoader<Int, MangaNodeList> { ids -> | ||
future { | ||
transaction { | ||
addLogger(Slf4jSqlDebugLogger) | ||
val itemsByRef = CategoryMangaTable.innerJoin(MangaTable).select { CategoryMangaTable.category inList ids } | ||
.map { Pair(it[CategoryMangaTable.category].value, MangaType(it)) } | ||
.groupBy { it.first } | ||
.mapValues { it.value.map { pair -> pair.second } } | ||
ids.map { (itemsByRef[it] ?: emptyList()).toNodeList() } | ||
} | ||
} | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
server/src/main/kotlin/suwayomi/tachidesk/graphql/dataLoaders/MetaDataLoader.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,80 @@ | ||
package suwayomi.tachidesk.graphql.dataLoaders | ||
|
||
import com.expediagroup.graphql.dataloader.KotlinDataLoader | ||
import org.dataloader.DataLoader | ||
import org.dataloader.DataLoaderFactory | ||
import org.jetbrains.exposed.sql.Slf4jSqlDebugLogger | ||
import org.jetbrains.exposed.sql.addLogger | ||
import org.jetbrains.exposed.sql.select | ||
import org.jetbrains.exposed.sql.transactions.transaction | ||
import suwayomi.tachidesk.global.model.table.GlobalMetaTable | ||
import suwayomi.tachidesk.graphql.types.CategoryMetaItem | ||
import suwayomi.tachidesk.graphql.types.ChapterMetaItem | ||
import suwayomi.tachidesk.graphql.types.GlobalMetaItem | ||
import suwayomi.tachidesk.graphql.types.MangaMetaItem | ||
import suwayomi.tachidesk.graphql.types.MetaItem | ||
import suwayomi.tachidesk.graphql.types.MetaNodeList | ||
import suwayomi.tachidesk.graphql.types.MetaNodeList.Companion.toNodeList | ||
import suwayomi.tachidesk.manga.model.table.ChapterMetaTable | ||
import suwayomi.tachidesk.manga.model.table.MangaMetaTable | ||
import suwayomi.tachidesk.server.JavalinSetup.future | ||
|
||
class GlobalMetaDataLoader : KotlinDataLoader<String, MetaItem?> { | ||
override val dataLoaderName = "GlobalMetaDataLoader" | ||
override fun getDataLoader(): DataLoader<String, MetaItem?> = DataLoaderFactory.newDataLoader<String, MetaItem?> { ids -> | ||
future { | ||
transaction { | ||
addLogger(Slf4jSqlDebugLogger) | ||
val metasByRefId = GlobalMetaTable.select { GlobalMetaTable.key inList ids } | ||
.map { GlobalMetaItem(it) } | ||
.associateBy { it.key } | ||
ids.map { metasByRefId[it] } | ||
} | ||
} | ||
} | ||
} | ||
|
||
class ChapterMetaDataLoader : KotlinDataLoader<Int, MetaNodeList> { | ||
override val dataLoaderName = "ChapterMetaDataLoader" | ||
override fun getDataLoader(): DataLoader<Int, MetaNodeList> = DataLoaderFactory.newDataLoader<Int, MetaNodeList> { ids -> | ||
future { | ||
transaction { | ||
addLogger(Slf4jSqlDebugLogger) | ||
val metasByRefId = ChapterMetaTable.select { ChapterMetaTable.ref inList ids } | ||
.map { ChapterMetaItem(it) } | ||
.groupBy { it.ref } | ||
ids.map { (metasByRefId[it] ?: emptyList()).toNodeList() } | ||
} | ||
} | ||
} | ||
} | ||
|
||
class MangaMetaDataLoader : KotlinDataLoader<Int, MetaNodeList> { | ||
override val dataLoaderName = "MangaMetaDataLoader" | ||
override fun getDataLoader(): DataLoader<Int, MetaNodeList> = DataLoaderFactory.newDataLoader<Int, MetaNodeList> { ids -> | ||
future { | ||
transaction { | ||
addLogger(Slf4jSqlDebugLogger) | ||
val metasByRefId = MangaMetaTable.select { MangaMetaTable.ref inList ids } | ||
.map { MangaMetaItem(it) } | ||
.groupBy { it.ref } | ||
ids.map { (metasByRefId[it] ?: emptyList()).toNodeList() } | ||
} | ||
} | ||
} | ||
} | ||
|
||
class CategoryMetaDataLoader : KotlinDataLoader<Int, MetaNodeList> { | ||
override val dataLoaderName = "CategoryMetaDataLoader" | ||
override fun getDataLoader(): DataLoader<Int, MetaNodeList> = DataLoaderFactory.newDataLoader<Int, MetaNodeList> { ids -> | ||
future { | ||
transaction { | ||
addLogger(Slf4jSqlDebugLogger) | ||
val metasByRefId = MangaMetaTable.select { MangaMetaTable.ref inList ids } | ||
.map { CategoryMetaItem(it) } | ||
.groupBy { it.ref } | ||
ids.map { (metasByRefId[it] ?: emptyList()).toNodeList() } | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.