Skip to content

Commit

Permalink
Fix/graphql subscriptions logging (#704)
Browse files Browse the repository at this point in the history
* Only log operationMessage in case gql logging is enabled

* Always log message type and operation name
  • Loading branch information
schroda authored Oct 5, 2023
1 parent feead10 commit 3cd3cb0
Showing 1 changed file with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import suwayomi.tachidesk.graphql.server.subscriptions.SubscriptionOperationMess
import suwayomi.tachidesk.graphql.server.subscriptions.SubscriptionOperationMessage.ServerMessages.GQL_ERROR
import suwayomi.tachidesk.graphql.server.subscriptions.SubscriptionOperationMessage.ServerMessages.GQL_NEXT
import suwayomi.tachidesk.graphql.server.toGraphQLContext
import suwayomi.tachidesk.server.serverConfig

/**
* Implementation of the `graphql-ws` protocol defined by Apollo
Expand All @@ -52,9 +53,30 @@ class ApolloSubscriptionProtocolHandler(
private val basicConnectionErrorMessage = SubscriptionOperationMessage(type = GQL_ERROR.type)
private val acknowledgeMessage = SubscriptionOperationMessage(GQL_CONNECTION_ACK.type)

private fun getOperationName(payload: Any?): String {
val unknownOperationName = "__UNKNOWN__"

try {
@Suppress("UNCHECKED_CAST")
return (payload as Map<String, String>)["operationName"] ?: unknownOperationName
} catch (e: Exception) {
return unknownOperationName
}
}

fun handleMessage(context: WsMessageContext): Flow<SubscriptionOperationMessage> {
val operationMessage = convertToMessageOrNull(context.message()) ?: return flowOf(basicConnectionErrorMessage)
logger.debug { "GraphQL subscription client message, sessionId=${context.sessionId} operationMessage=$operationMessage" }
logger.debug {
"GraphQL subscription client message, sessionId=${context.sessionId} type=${operationMessage.type} operationName=${
getOperationName(operationMessage.payload)
} ${
if (serverConfig.gqlDebugLogsEnabled.value) {
"operationMessage=$operationMessage"
} else {
""
}
}"
}

return try {
when (operationMessage.type) {
Expand Down

0 comments on commit 3cd3cb0

Please sign in to comment.