forked from pgutkowski/KGraphQL
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
111 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ allprojects { | |
|
||
repositories { | ||
jcenter() | ||
maven { url "https://jitpack.io" } | ||
} | ||
|
||
dependencies { | ||
|
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
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 |
---|---|---|
@@ -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" | ||
} |
9 changes: 9 additions & 0 deletions
9
kgraphql-ktor/src/main/kotlin/com/apurebase/kgraphql/GraphqlRequest.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,9 @@ | ||
package com.apurebase.kgraphql | ||
|
||
data class GraphqlRequest( | ||
val query: String?, | ||
val mutation: String?, | ||
val variables: Map<String, Any?>? | ||
) { | ||
val finalQuery get() = query ?: mutation!! | ||
} |
55 changes: 53 additions & 2 deletions
55
kgraphql-ktor/src/main/kotlin/com/apurebase/kgraphql/KtorFeature.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 |
---|---|---|
@@ -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() | ||
} |
42 changes: 39 additions & 3 deletions
42
kgraphql-ktor/src/test/kotlin/com/apurebase/kgraphql/KtorFeatureTest.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 |
---|---|---|
@@ -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!\"}}" | ||
|
||
} | ||
} |
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