Skip to content

Commit

Permalink
just playing around with the idea
Browse files Browse the repository at this point in the history
  • Loading branch information
jeggy committed Feb 24, 2020
1 parent 377b081 commit 82c6203
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 6 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ allprojects {

repositories {
jcenter()
maven { url "https://jitpack.io" }
}

dependencies {
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ jackson_version=2.9.7
caffeine_version=2.8.0
serialization_version=0.14.0
kDataLoader_version=0.1.1
ktor_version=1.3.1

# Test-Dependencies
kotlin_html_version=0.6.12
Expand Down
7 changes: 7 additions & 0 deletions kgraphql-ktor/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
dependencies {
compile project(':kgraphql')

compile "io.ktor:ktor-server-core:$ktor_version"
compile "io.ktor:ktor-gson:$ktor_version"
compile 'com.github.salomonbrys.kotson:kotson:2.5.0'

testImplementation "com.github.makarenkoanton:kraph:0.6.1"
testImplementation "io.ktor:ktor-server-test-host:$ktor_version"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.apurebase.kgraphql

data class GraphqlRequest(
val query: String?,
val mutation: String?,
val variables: Map<String, Any?>?
) {
val finalQuery get() = query ?: mutation!!
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,55 @@
package com.apurebase.kgraphql

// TODO: Implement
fun ktorFeature() = println("Hello World")
import com.apurebase.kgraphql.schema.dsl.SchemaBuilder
import com.github.salomonbrys.kotson.fromJson
import com.google.gson.Gson
import io.ktor.application.ApplicationCall
import io.ktor.application.call
import io.ktor.http.ContentType
import io.ktor.http.charset
import io.ktor.request.contentType
import io.ktor.request.receiveStream
import io.ktor.response.respond
import io.ktor.routing.Route
import io.ktor.routing.post
import java.nio.charset.Charset


fun Route.graphql(block: SchemaBuilder.() -> Unit) {

val schema = KGraphQL.schema(block)
val gson = Gson()

post("graphql") {
val raw = context.receiveTextWithCorrectEncoding()
val req = gson.fromJson<GraphqlRequest>(raw)

// TODO: Support for Context!


val result = schema.execute(req.finalQuery, gson.toJson(req.variables))

call.respond(result)
}
}




/**
* Receive the request as String.
* If there is no Content-Type in the HTTP header specified use ISO_8859_1 as default charset, see https://www.w3.org/International/articles/http-charset/index#charset.
* But use UTF-8 as default charset for application/json, see https://tools.ietf.org/html/rfc4627#section-3
*
* Taken from github: https://github.com/ktorio/ktor/issues/384#issuecomment-458542686
*/
suspend fun ApplicationCall.receiveTextWithCorrectEncoding(): String {
fun ContentType.defaultCharset(): Charset = when (this) {
ContentType.Application.Json -> Charsets.UTF_8
else -> Charsets.ISO_8859_1
}

val contentType = request.contentType()
val suitableCharset = contentType.charset() ?: contentType.defaultCharset()
return receiveStream().bufferedReader(charset = suitableCharset).readText()
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,48 @@
package com.apurebase.kgraphql

import org.amshove.kluent.shouldEqual
import com.apurebase.kgraphql.schema.dsl.SchemaBuilder
import io.ktor.application.install
import io.ktor.http.HttpHeaders
import io.ktor.http.HttpMethod
import io.ktor.routing.Routing
import io.ktor.server.testing.setBody
import io.ktor.server.testing.withTestApplication
import me.lazmaid.kraph.Kraph
import org.amshove.kluent.shouldBeEqualTo
import org.junit.jupiter.api.Test

class KtorFeatureTest {

private fun withServer(block: SchemaBuilder.() -> Unit): (Kraph.() -> Unit) -> String {
return {
withTestApplication({
install(Routing) {
graphql { block() }
}
}) {
handleRequest {
uri = "graphql"
method = HttpMethod.Post
addHeader(HttpHeaders.ContentType, "application/json;charset=UTF-8")
setBody(Kraph { it(this) }.toRequestString())
}.response.content!!
}
}
}

@Test
fun `Some random test`() {
ktorFeature()
1 shouldEqual 1
val server = withServer {
query("hello") {
resolver { -> "World!" }
}
}

server {
query {
field("hello")
}
} shouldBeEqualTo "{\"data\":{\"hello\":\"World!\"}}"

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers


class SchemaConfigurationDSL {
open class SchemaConfigurationDSL {
var useDefaultPrettyPrinter: Boolean = false
var useCachingDocumentParser: Boolean = true
var objectMapper: ObjectMapper = jacksonObjectMapper()
Expand Down

0 comments on commit 82c6203

Please sign in to comment.