Skip to content

Commit 951ddb8

Browse files
authored
Ktor Server: set "graphql-transport-ws" as default protocol to graphQLSubscriptionsRoute (#1804)
### 📝 Description This is a small fix to set `graphql-transport-ws` subprotocol as (sensible) default for Ktor server `graphQLSubscriptionsRoute`. Without that line, users may encounter issues when sending requests using popular GQL clients like Apollo or even bundled GraphiQL UI, because of they are sending `Sec-Websocket-Protocol=graphql-transport-ws` header and that is being rejected by Ktor. Users may still override this value, if needed ### 🔗 Related Issues #1756
1 parent 2520867 commit 951ddb8

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

servers/graphql-kotlin-ktor-server/src/main/kotlin/com/expediagroup/graphql/server/ktor/GraphQLRoutes.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ fun Route.graphQLPostRoute(endpoint: String = "graphql", streamingResponse: Bool
8080
*/
8181
fun Route.graphQLSubscriptionsRoute(
8282
endpoint: String = "subscriptions",
83-
protocol: String? = null,
83+
protocol: String? = "graphql-transport-ws",
8484
handlerOverride: KtorGraphQLSubscriptionHandler? = null,
8585
) {
8686
val handler = handlerOverride ?: run {

servers/graphql-kotlin-ktor-server/src/test/kotlin/com/expediagroup/graphql/server/ktor/GraphQLPluginTest.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ import io.ktor.client.request.get
2727
import io.ktor.client.request.parameter
2828
import io.ktor.client.request.post
2929
import io.ktor.client.request.setBody
30+
import io.ktor.client.request.url
3031
import io.ktor.client.statement.bodyAsText
3132
import io.ktor.http.ContentType
33+
import io.ktor.http.HttpHeaders
3234
import io.ktor.http.HttpStatusCode
3335
import io.ktor.http.contentType
3436
import io.ktor.serialization.jackson.jackson
@@ -195,7 +197,10 @@ class GraphQLPluginTest {
195197
install(WebSockets)
196198
}
197199

198-
client.webSocket("/subscriptions") {
200+
client.webSocket({
201+
url("/subscriptions")
202+
headers[HttpHeaders.SecWebSocketProtocol] = "graphql-transport-ws"
203+
}) {
199204
outgoing.send(Frame.Text("""{"type": "connection_init"}"""))
200205

201206
val ack = incoming.receive()

0 commit comments

Comments
 (0)