Update graphql-playground endpoint

This commit is contained in:
Valter Martinek
2022-11-11 11:31:38 +01:00
committed by Syer10
parent bf7f1a04b3
commit 0c555e88d3
3 changed files with 24 additions and 8 deletions
@@ -7,16 +7,14 @@
package suwayomi.tachidesk.graphql
import io.javalin.apibuilder.ApiBuilder.get
import io.javalin.apibuilder.ApiBuilder.post
import io.javalin.apibuilder.ApiBuilder.*
import suwayomi.tachidesk.graphql.controller.GraphQLController
object GraphQL {
fun defineEndpoints() {
post("graphql", GraphQLController.execute)
get("graphql") { ctx ->
val html = javaClass.getResource("/graphql.html")?.readText()
ctx.html(html ?: "Could not load playground")
}
// graphql playground
get("graphql", GraphQLController::playground)
}
}
@@ -8,6 +8,7 @@
package suwayomi.tachidesk.graphql.controller
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import io.javalin.http.Context
import io.javalin.http.HttpCode
import suwayomi.tachidesk.graphql.impl.getGraphQLServer
import suwayomi.tachidesk.server.JavalinSetup.future
@@ -38,4 +39,19 @@ object GraphQLController {
json<Any>(HttpCode.OK)
}
)
fun playground(ctx: Context) {
val playgroundHtml = javaClass.getResource("/graphql-playground.html")
val body = playgroundHtml.openStream().bufferedReader().use { reader ->
val graphQLEndpoint = "graphql"
val subscriptionsEndpoint = "graphql"
reader.readText()
.replace("\${graphQLEndpoint}", graphQLEndpoint)
.replace("\${subscriptionsEndpoint}", subscriptionsEndpoint)
}
ctx.html(body ?: "Could not load playground")
}
}
@@ -50,9 +50,11 @@
</div>
<script>window.addEventListener('load', function (event) {
GraphQLPlayground.init(document.getElementById('root'), {
// options as 'endpoint' belong here
settings: {'request.credentials': 'same-origin'},
endpoint: '/${graphQLEndpoint}',
subscriptionEndpoint: '/${subscriptionsEndpoint}'
})
})</script>
</body>
</html>
</html>