Skip to content

Commit

Permalink
address pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
c committed Apr 9, 2024
1 parent bc93fed commit 3db7d80
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,7 @@ fun Application.graphQLModule() {
contentConverter = JacksonWebsocketContentConverter()
}
install(StatusPages) {
exception<Throwable> { call, cause ->
when (cause) {
is UnsupportedOperationException -> call.respond(HttpStatusCode.MethodNotAllowed)
else -> call.respond(HttpStatusCode.BadRequest)
}
}
defaultGraphQLStatusPages()
}
install(CORS) {
anyHost()
Expand Down
1 change: 1 addition & 0 deletions servers/graphql-kotlin-ktor-server/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ dependencies {
api(libs.ktor.server.core)
api(libs.ktor.server.content)
api(libs.ktor.server.websockets)
api(libs.ktor.server.statuspages)
testImplementation(libs.kotlinx.coroutines.test)
testImplementation(libs.ktor.client.content)
testImplementation(libs.ktor.client.websockets)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.expediagroup.graphql.server.ktor

import io.ktor.http.HttpStatusCode
import io.ktor.server.plugins.statuspages.StatusPagesConfig
import io.ktor.server.response.respond

fun StatusPagesConfig.defaultGraphQLStatusPages(): StatusPagesConfig {
exception<Throwable> { call, cause ->
when (cause) {
is UnsupportedOperationException -> call.respond(HttpStatusCode.MethodNotAllowed)
else -> call.respond(HttpStatusCode.BadRequest) } }
return this
}
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,17 @@ class GraphQLPluginTest {
}

@Test
fun `server should return Unsupported Media Type for invalid POST requests`() {
fun `server should return Bad Request for invalid POST requests with correct content type`() {
testApplication {
val response = client.post("/graphql") {
contentType(ContentType.Application.Json)
}
assertEquals(HttpStatusCode.BadRequest, response.status)
}
}

@Test
fun `server should return Unsupported Media Type for POST requests with invalid content type`() {
testApplication {
val response = client.post("/graphql")
assertEquals(HttpStatusCode.UnsupportedMediaType, response.status)
Expand Down Expand Up @@ -234,12 +244,7 @@ class GraphQLPluginTest {

fun Application.testGraphQLModule() {
install(StatusPages) {
exception<Throwable> { call, cause ->
when (cause) {
is UnsupportedOperationException -> call.respond(HttpStatusCode.MethodNotAllowed)
else -> call.respond(HttpStatusCode.BadRequest)
}
}
defaultGraphQLStatusPages()
}
install(GraphQL) {
schema {
Expand Down
7 changes: 7 additions & 0 deletions website/docs/server/ktor-server/ktor-overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ GraphQL plugin provides following `Route` extension functions
- `Route#graphQLSDLRoute` - GraphQL route for exposing schema in Schema Definition Language (SDL) format
- `Route#graphiQLRoute` - GraphQL route for exposing [an official IDE](https://github.com/graphql/graphiql) from the GraphQL Foundation

## StatusPages

`graphql-kotlin-ktor-server` plugin differs from Spring as it relies on Ktor's StatusPages plugin to perform error handling.
It is recommended to use the default settings, however, if you would like to customize your error handling you can create
your own handler. One example might be if you need to catch a custom Authorization error to return a 401 status code.
Please see [Ktor's Official Documentation for StatusPages](https://ktor.io/docs/server-status-pages.html)

## GraalVm Native Image Support

GraphQL Kotlin Ktor Server can be compiled to a [native image](https://www.graalvm.org/latest/reference-manual/native-image/)
Expand Down

0 comments on commit 3db7d80

Please sign in to comment.