From 3db7d80c00107633e2ff54c22d2f14740ab57e7d Mon Sep 17 00:00:00 2001 From: c Date: Tue, 9 Apr 2024 13:50:38 -0700 Subject: [PATCH] address pr comments --- .../examples/server/ktor/GraphQLModule.kt | 7 +------ .../build.gradle.kts | 1 + .../graphql/server/ktor/GraphQLStatusPages.kt | 13 +++++++++++++ .../graphql/server/ktor/GraphQLPluginTest.kt | 19 ++++++++++++------- .../docs/server/ktor-server/ktor-overview.mdx | 7 +++++++ 5 files changed, 34 insertions(+), 13 deletions(-) create mode 100644 servers/graphql-kotlin-ktor-server/src/main/kotlin/com/expediagroup/graphql/server/ktor/GraphQLStatusPages.kt diff --git a/examples/server/ktor-server/src/main/kotlin/com/expediagroup/graphql/examples/server/ktor/GraphQLModule.kt b/examples/server/ktor-server/src/main/kotlin/com/expediagroup/graphql/examples/server/ktor/GraphQLModule.kt index e6fc3fc4c7..fe0a5a3d3d 100644 --- a/examples/server/ktor-server/src/main/kotlin/com/expediagroup/graphql/examples/server/ktor/GraphQLModule.kt +++ b/examples/server/ktor-server/src/main/kotlin/com/expediagroup/graphql/examples/server/ktor/GraphQLModule.kt @@ -47,12 +47,7 @@ fun Application.graphQLModule() { contentConverter = JacksonWebsocketContentConverter() } install(StatusPages) { - exception { call, cause -> - when (cause) { - is UnsupportedOperationException -> call.respond(HttpStatusCode.MethodNotAllowed) - else -> call.respond(HttpStatusCode.BadRequest) - } - } + defaultGraphQLStatusPages() } install(CORS) { anyHost() diff --git a/servers/graphql-kotlin-ktor-server/build.gradle.kts b/servers/graphql-kotlin-ktor-server/build.gradle.kts index 92ec8eeac7..d7ab4c2457 100644 --- a/servers/graphql-kotlin-ktor-server/build.gradle.kts +++ b/servers/graphql-kotlin-ktor-server/build.gradle.kts @@ -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) diff --git a/servers/graphql-kotlin-ktor-server/src/main/kotlin/com/expediagroup/graphql/server/ktor/GraphQLStatusPages.kt b/servers/graphql-kotlin-ktor-server/src/main/kotlin/com/expediagroup/graphql/server/ktor/GraphQLStatusPages.kt new file mode 100644 index 0000000000..ae9b945885 --- /dev/null +++ b/servers/graphql-kotlin-ktor-server/src/main/kotlin/com/expediagroup/graphql/server/ktor/GraphQLStatusPages.kt @@ -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 { call, cause -> + when (cause) { + is UnsupportedOperationException -> call.respond(HttpStatusCode.MethodNotAllowed) + else -> call.respond(HttpStatusCode.BadRequest) } } + return this +} diff --git a/servers/graphql-kotlin-ktor-server/src/test/kotlin/com/expediagroup/graphql/server/ktor/GraphQLPluginTest.kt b/servers/graphql-kotlin-ktor-server/src/test/kotlin/com/expediagroup/graphql/server/ktor/GraphQLPluginTest.kt index 331c974da2..a4337060f4 100644 --- a/servers/graphql-kotlin-ktor-server/src/test/kotlin/com/expediagroup/graphql/server/ktor/GraphQLPluginTest.kt +++ b/servers/graphql-kotlin-ktor-server/src/test/kotlin/com/expediagroup/graphql/server/ktor/GraphQLPluginTest.kt @@ -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) @@ -234,12 +244,7 @@ class GraphQLPluginTest { fun Application.testGraphQLModule() { install(StatusPages) { - exception { call, cause -> - when (cause) { - is UnsupportedOperationException -> call.respond(HttpStatusCode.MethodNotAllowed) - else -> call.respond(HttpStatusCode.BadRequest) - } - } + defaultGraphQLStatusPages() } install(GraphQL) { schema { diff --git a/website/docs/server/ktor-server/ktor-overview.mdx b/website/docs/server/ktor-server/ktor-overview.mdx index 66798dac42..2cad8ac812 100644 --- a/website/docs/server/ktor-server/ktor-overview.mdx +++ b/website/docs/server/ktor-server/ktor-overview.mdx @@ -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/)