-
Notifications
You must be signed in to change notification settings - Fork 362
Fix Apollo subscription message handling #371
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
smyrick
merged 4 commits into
ExpediaGroup:master
from
smyrick:kotlin-flow-subscriptions
Sep 24, 2019
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,3 @@ | ||
graphql: | ||
packages: | ||
- "com.expediagroup.graphql.sample" | ||
This file contains hidden or 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
22 changes: 22 additions & 0 deletions
22
.../main/kotlin/com/expediagroup/graphql/spring/exception/UknownSubscriptionOperationType.kt
This file contains hidden or 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,22 @@ | ||
/* | ||
* Copyright 2019 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.spring.exception | ||
|
||
import com.expediagroup.graphql.spring.model.SubscriptionOperationMessage | ||
|
||
class UknownSubscriptionOperationType(operationMessage: SubscriptionOperationMessage) : | ||
IllegalArgumentException("Unknown subscription operation $operationMessage") |
100 changes: 100 additions & 0 deletions
100
...ain/kotlin/com/expediagroup/graphql/spring/execution/ApolloSubscriptionProtocolHandler.kt
This file contains hidden or 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,100 @@ | ||
/* | ||
* Copyright 2019 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.spring.execution | ||
|
||
import com.expediagroup.graphql.spring.model.GraphQLRequest | ||
import com.expediagroup.graphql.spring.model.SubscriptionOperationMessage | ||
import com.expediagroup.graphql.spring.model.SubscriptionOperationMessage.ClientMessages.GQL_CONNECTION_INIT | ||
import com.expediagroup.graphql.spring.model.SubscriptionOperationMessage.ClientMessages.GQL_CONNECTION_TERMINATE | ||
import com.expediagroup.graphql.spring.model.SubscriptionOperationMessage.ClientMessages.GQL_START | ||
import com.expediagroup.graphql.spring.model.SubscriptionOperationMessage.ClientMessages.GQL_STOP | ||
import com.expediagroup.graphql.spring.model.SubscriptionOperationMessage.ServerMessages.GQL_COMPLETE | ||
import com.expediagroup.graphql.spring.model.SubscriptionOperationMessage.ServerMessages.GQL_CONNECTION_ACK | ||
import com.expediagroup.graphql.spring.model.SubscriptionOperationMessage.ServerMessages.GQL_CONNECTION_ERROR | ||
import com.expediagroup.graphql.spring.model.SubscriptionOperationMessage.ServerMessages.GQL_DATA | ||
import com.expediagroup.graphql.spring.model.SubscriptionOperationMessage.ServerMessages.GQL_ERROR | ||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.fasterxml.jackson.module.kotlin.convertValue | ||
import com.fasterxml.jackson.module.kotlin.readValue | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.web.reactive.socket.WebSocketSession | ||
import reactor.core.publisher.Flux | ||
|
||
class ApolloSubscriptionProtocolHandler( | ||
private val subscriptionHandler: SubscriptionHandler, | ||
private val objectMapper: ObjectMapper | ||
) { | ||
|
||
private val logger = LoggerFactory.getLogger(ApolloSubscriptionProtocolHandler::class.java) | ||
|
||
@Suppress("Detekt.TooGenericExceptionCaught") | ||
fun handle(payload: String, session: WebSocketSession): Flux<SubscriptionOperationMessage> { | ||
try { | ||
val operationMessage: SubscriptionOperationMessage = objectMapper.readValue(payload) | ||
|
||
return when { | ||
operationMessage.type == GQL_CONNECTION_INIT.type -> Flux.just(SubscriptionOperationMessage(GQL_CONNECTION_ACK.type)) | ||
operationMessage.type == GQL_START.type -> startSubscription(operationMessage, session) | ||
operationMessage.type == GQL_CONNECTION_TERMINATE.type || operationMessage.type == GQL_STOP.type -> { | ||
session.close() | ||
Flux.empty() | ||
} | ||
else -> { | ||
logger.error("Unknown subscription operation $operationMessage") | ||
Flux.just(SubscriptionOperationMessage(type = GQL_CONNECTION_ERROR.type, id = operationMessage.id)) | ||
} | ||
} | ||
} catch (exception: Exception) { | ||
logger.error("Error parsing the subscription message", exception) | ||
return Flux.just(SubscriptionOperationMessage(type = GQL_CONNECTION_ERROR.type)) | ||
} | ||
} | ||
|
||
@Suppress("Detekt.TooGenericExceptionCaught") | ||
private fun startSubscription(operationMessage: SubscriptionOperationMessage, session: WebSocketSession): Flux<SubscriptionOperationMessage> { | ||
val payload = operationMessage.payload | ||
|
||
if (payload == null) { | ||
logger.error("Payload was null instead of a GraphQLRequest object") | ||
return Flux.just(SubscriptionOperationMessage(type = GQL_CONNECTION_ERROR.type, id = operationMessage.id)) | ||
} | ||
|
||
return try { | ||
val request = objectMapper.convertValue<GraphQLRequest>(payload) | ||
subscriptionHandler.executeSubscription(request) | ||
.map { | ||
if (it.errors?.isNotEmpty() == true) { | ||
SubscriptionOperationMessage(type = GQL_ERROR.type, id = operationMessage.id, payload = it) | ||
} else { | ||
SubscriptionOperationMessage(type = GQL_DATA.type, id = operationMessage.id, payload = it) | ||
} | ||
} | ||
.doOnSubscribe { logger.trace("WebSocket GraphQL subscription subscribe, WebSocketID=${session.id} OperationMessageID=${operationMessage.id}") } | ||
.doOnCancel { logger.trace("WebSocket GraphQL subscription cancel, WebSocketID=${session.id} OperationMessageID=${operationMessage.id}") } | ||
.doOnComplete { logger.trace("WebSocket GraphQL subscription complete, WebSocketID=${session.id} OperationMessageID=${operationMessage.id}") } | ||
.doFinally { | ||
val completeMessage = SubscriptionOperationMessage(type = GQL_COMPLETE.type, id = operationMessage.id) | ||
val text = objectMapper.writeValueAsString(completeMessage) | ||
session.send(Flux.just(session.textMessage(text))) | ||
session.close() | ||
} | ||
} catch (exception: Exception) { | ||
logger.error("Error running graphql subscription", exception) | ||
Flux.just(SubscriptionOperationMessage(type = GQL_CONNECTION_ERROR.type, id = operationMessage.id)) | ||
} | ||
} | ||
} |
This file contains hidden or 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
48 changes: 48 additions & 0 deletions
48
...ver/src/main/kotlin/com/expediagroup/graphql/spring/model/SubscriptionOperationMessage.kt
This file contains hidden or 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,48 @@ | ||
/* | ||
* Copyright 2019 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.spring.model | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties | ||
|
||
/** | ||
* The `graphql-ws` protocol from Apollo Client has some special text messages to signal events. | ||
* Along with the HTTP WebSocket event handling we need to have some extra logic | ||
* | ||
* https://github.com/apollographql/subscriptions-transport-ws/blob/master/PROTOCOL.md | ||
*/ | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
data class SubscriptionOperationMessage( | ||
val type: String, | ||
val id: String? = null, | ||
val payload: Any? = null | ||
) { | ||
enum class ClientMessages(val type: String) { | ||
GQL_CONNECTION_INIT("connection_init"), | ||
GQL_START("start"), | ||
GQL_STOP("stop"), | ||
GQL_CONNECTION_TERMINATE("connection_terminate") | ||
} | ||
|
||
enum class ServerMessages(val type: String) { | ||
GQL_CONNECTION_ACK("connection_ack"), | ||
GQL_CONNECTION_ERROR("connection_error"), | ||
GQL_DATA("data"), | ||
GQL_ERROR("error"), | ||
GQL_COMPLETE("complete"), | ||
GQL_CONNECTION_KEEP_ALIVE("ka") | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.