-
Notifications
You must be signed in to change notification settings - Fork 345
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(server): generic GraphQL WS subscription support (#1810)
Add [GraphQL WS subscription protocol](https://github.com/enisdenjo/graphql-ws/blob/master/PROTOCOL.md) implementation to the `graphql-kotlin-server`. Both Ktor and Spring servers can now use the same `Flow` based implementation. Spring server defaults to the new `graphql-transport-ws` protocol. In order to opt-in to use the legacy Apollo `subscription-transport-ws` you need to explicitly specify `graphql.subscriptions.protocol=APOLLO_SUBSCRIPTIONS_WS ` property. Deprecated all existing Spring classes related to [Apollo subscription-transport-ws](https://github.com/apollographql/subscriptions-transport-ws) protocol.
- Loading branch information
1 parent
707731a
commit 74ddab7
Showing
58 changed files
with
1,931 additions
and
1,332 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
3 changes: 3 additions & 0 deletions
3
examples/server/spring-server/src/main/resources/application.yml
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,6 +1,9 @@ | ||
graphql: | ||
packages: | ||
- "com.expediagroup.graphql.examples.server.spring" | ||
playground: | ||
enabled: true | ||
subscriptions: | ||
# Send a ka message every 1000 ms (1 second) | ||
keepAliveInterval: 1000 | ||
protocol: "APOLLO_SUBSCRIPTIONS_WS" |
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
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
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
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
60 changes: 60 additions & 0 deletions
60
...om/expediagroup/graphql/server/ktor/subscriptions/KtorGraphQLSubscriptionRequestParser.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,60 @@ | ||
/* | ||
* Copyright 2023 Expedia, Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.expediagroup.graphql.server.ktor.subscriptions | ||
|
||
import com.expediagroup.graphql.server.execution.subscription.GraphQLSubscriptionRequestParser | ||
import com.expediagroup.graphql.server.types.GraphQLSubscriptionStatus | ||
import io.ktor.server.websocket.WebSocketServerSession | ||
import io.ktor.websocket.CloseReason | ||
import io.ktor.websocket.Frame | ||
import io.ktor.websocket.close | ||
import io.ktor.websocket.readText | ||
import kotlinx.coroutines.channels.ClosedReceiveChannelException | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.flow | ||
import kotlinx.coroutines.isActive | ||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
|
||
/** | ||
* Ktor specific version of WebSocket subscription request parser. | ||
*/ | ||
interface KtorGraphQLSubscriptionRequestParser : GraphQLSubscriptionRequestParser<WebSocketServerSession> | ||
|
||
class DefaultKtorGraphQLSubscriptionRequestParser : KtorGraphQLSubscriptionRequestParser { | ||
|
||
private val logger: Logger = LoggerFactory.getLogger(DefaultKtorGraphQLSubscriptionRequestParser::class.java) | ||
|
||
override suspend fun parseRequestFlow(session: WebSocketServerSession): Flow<String> = | ||
flow { | ||
try { | ||
while (session.isActive) { | ||
val frame = session.incoming.receive() | ||
if (frame !is Frame.Text) { | ||
val invalidStatus = GraphQLSubscriptionStatus.INVALID_MESSAGE | ||
session.close(CloseReason(code = invalidStatus.code.toShort(), message = invalidStatus.reason)) | ||
continue | ||
} else { | ||
val messageString = frame.readText() | ||
emit(messageString) | ||
} | ||
} | ||
} catch (e: ClosedReceiveChannelException) { | ||
logger.debug("Client disconnected") | ||
} | ||
} | ||
} |
Oops, something went wrong.