Skip to content

Add graphql-js support #60

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions apollo-execution-ktor/api/apollo-execution-ktor.api
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@ public final class com/apollographql/execution/ktor/MainKt {
public static synthetic fun apolloModule$default (Lio/ktor/server/application/Application;Lcom/apollographql/apollo/execution/ExecutableSchema;Ljava/lang/String;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)V
public static final fun apolloSandboxModule (Lio/ktor/server/application/Application;Ljava/lang/String;Ljava/lang/String;Lkotlin/jvm/functions/Function1;)V
public static synthetic fun apolloSandboxModule$default (Lio/ktor/server/application/Application;Ljava/lang/String;Ljava/lang/String;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)V
public static final fun apolloSubscriptionModule (Lio/ktor/server/application/Application;Lcom/apollographql/apollo/execution/ExecutableSchema;Ljava/lang/String;Lkotlin/jvm/functions/Function1;)V
public static synthetic fun apolloSubscriptionModule$default (Lio/ktor/server/application/Application;Lcom/apollographql/apollo/execution/ExecutableSchema;Ljava/lang/String;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)V
public static final fun apolloSubscriptionModule (Lio/ktor/server/application/Application;Lcom/apollographql/apollo/execution/ExecutableSchema;Ljava/lang/String;Lcom/apollographql/execution/ktor/WsProtocol;Lkotlin/jvm/functions/Function1;)V
public static synthetic fun apolloSubscriptionModule$default (Lio/ktor/server/application/Application;Lcom/apollographql/apollo/execution/ExecutableSchema;Ljava/lang/String;Lcom/apollographql/execution/ktor/WsProtocol;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)V
public static final fun parseAsGraphQLRequest (Lio/ktor/server/request/ApplicationRequest;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static final fun respondGraphQL (Lio/ktor/server/application/ApplicationCall;Lcom/apollographql/apollo/execution/ExecutableSchema;Lcom/apollographql/apollo/api/ExecutionContext;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static synthetic fun respondGraphQL$default (Lio/ktor/server/application/ApplicationCall;Lcom/apollographql/apollo/execution/ExecutableSchema;Lcom/apollographql/apollo/api/ExecutionContext;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
}

public final class com/apollographql/execution/ktor/WsProtocol : java/lang/Enum {
public static final field GraphqlWS Lcom/apollographql/execution/ktor/WsProtocol;
public static final field Legacy Lcom/apollographql/execution/ktor/WsProtocol;
public static fun getEntries ()Lkotlin/enums/EnumEntries;
public static fun valueOf (Ljava/lang/String;)Lcom/apollographql/execution/ktor/WsProtocol;
public static fun values ()[Lcom/apollographql/execution/ktor/WsProtocol;
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import com.apollographql.apollo.execution.GraphQLResponse
import com.apollographql.apollo.execution.parseAsGraphQLRequest
import com.apollographql.execution.*
import com.apollographql.execution.websocket.ConnectionInitAck
import com.apollographql.execution.websocket.GraphQLWsWebSocketHandler
import com.apollographql.execution.websocket.SubscriptionWebSocketHandler
import com.apollographql.execution.websocket.WebSocketBinaryMessage
import com.apollographql.execution.websocket.WebSocketTextMessage
import com.apollographql.execution.websocket.WsConnectionInitAck
import io.ktor.http.*
import io.ktor.http.content.*
import io.ktor.server.application.*
Expand Down Expand Up @@ -99,30 +101,59 @@ fun Application.apolloModule(
}
}

enum class WsProtocol {
GraphqlWS,
Legacy
}
fun Application.apolloSubscriptionModule(
executableSchema: ExecutableSchema,
path: String = "/subscription",
protocol: WsProtocol = WsProtocol.GraphqlWS,
executionContext: (ApplicationRequest) -> ExecutionContext = { ExecutionContext.Empty }
) {
install(WebSockets)

routing {
webSocket(path, "graphql-ws") {
val transport = when (protocol) {
WsProtocol.GraphqlWS -> "graphql-transport-ws"
WsProtocol.Legacy -> "graphql-ws"
}
webSocket(path, transport) {
coroutineScope {
val handler = SubscriptionWebSocketHandler(
executableSchema = executableSchema,
scope = this,
executionContext = executionContext(call.request),
sendMessage = {
when (it) {
is WebSocketBinaryMessage -> send(Frame.Binary(true, it.data))
is WebSocketTextMessage -> send(Frame.Text(it.data))
}
},
connectionInitHandler = {
ConnectionInitAck
val handler = when(protocol) {
WsProtocol.GraphqlWS -> {
GraphQLWsWebSocketHandler(
executableSchema = executableSchema,
scope = this,
executionContext = executionContext(call.request),
sendMessage = {
when (it) {
is WebSocketBinaryMessage -> send(Frame.Binary(true, it.data))
is WebSocketTextMessage -> send(Frame.Text(it.data))
}
},
connectionInitHandler = {
WsConnectionInitAck
}
)
}
)
WsProtocol.Legacy -> {
SubscriptionWebSocketHandler(
executableSchema = executableSchema,
scope = this,
executionContext = executionContext(call.request),
sendMessage = {
when (it) {
is WebSocketBinaryMessage -> send(Frame.Binary(true, it.data))
is WebSocketTextMessage -> send(Frame.Text(it.data))
}
},
connectionInitHandler = {
ConnectionInitAck
}
)
}
}

for (frame in incoming) {
if (frame !is Frame.Text) {
Expand Down
26 changes: 23 additions & 3 deletions apollo-execution-runtime/api/apollo-execution-runtime.api
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,18 @@ public final class com/apollographql/execution/websocket/ConnectionInitError : c
public abstract interface class com/apollographql/execution/websocket/ConnectionInitResult {
}

public final class com/apollographql/execution/websocket/SubscriptionWebSocketHandler : com/apollographql/execution/websocket/WebSocketHandler {
public final class com/apollographql/execution/websocket/GraphQLWsWebSocketHandler : com/apollographql/execution/websocket/WebSocketHandler {
public fun <init> (Lcom/apollographql/apollo/execution/ExecutableSchema;Lkotlinx/coroutines/CoroutineScope;Lcom/apollographql/apollo/api/ExecutionContext;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;)V
public synthetic fun <init> (Lcom/apollographql/apollo/execution/ExecutableSchema;Lkotlinx/coroutines/CoroutineScope;Lcom/apollographql/apollo/api/ExecutionContext;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun close ()V
public fun handleMessage (Lcom/apollographql/execution/websocket/WebSocketMessage;)V
}

public final class com/apollographql/execution/websocket/SubscriptionWebSocketHandlerKt {
public static final fun subscriptionId (Lcom/apollographql/apollo/api/ExecutionContext;)Ljava/lang/String;
public final class com/apollographql/execution/websocket/SubscriptionWebSocketHandler : com/apollographql/execution/websocket/WebSocketHandler {
public fun <init> (Lcom/apollographql/apollo/execution/ExecutableSchema;Lkotlinx/coroutines/CoroutineScope;Lcom/apollographql/apollo/api/ExecutionContext;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;)V
public synthetic fun <init> (Lcom/apollographql/apollo/execution/ExecutableSchema;Lkotlinx/coroutines/CoroutineScope;Lcom/apollographql/apollo/api/ExecutionContext;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun close ()V
public fun handleMessage (Lcom/apollographql/execution/websocket/WebSocketMessage;)V
}

public final class com/apollographql/execution/websocket/WebSocketBinaryMessage : com/apollographql/execution/websocket/WebSocketMessage {
Expand All @@ -90,3 +93,20 @@ public final class com/apollographql/execution/websocket/WebSocketTextMessage :
public final fun getData ()Ljava/lang/String;
}

public final class com/apollographql/execution/websocket/WsConnectionInitAck : com/apollographql/execution/websocket/WsConnectionInitResult {
public static final field INSTANCE Lcom/apollographql/execution/websocket/WsConnectionInitAck;
public fun equals (Ljava/lang/Object;)Z
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}

public final class com/apollographql/execution/websocket/WsConnectionInitError : com/apollographql/execution/websocket/WsConnectionInitResult {
public fun <init> ()V
public fun <init> (Lcom/apollographql/apollo/api/Optional;)V
public synthetic fun <init> (Lcom/apollographql/apollo/api/Optional;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun getPayload ()Lcom/apollographql/apollo/api/Optional;
}

public abstract interface class com/apollographql/execution/websocket/WsConnectionInitResult {
}

23 changes: 22 additions & 1 deletion apollo-execution-runtime/api/apollo-execution-runtime.klib.api
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,22 @@ sealed interface com.apollographql.execution.websocket/ConnectionInitResult // c

sealed interface com.apollographql.execution.websocket/WebSocketMessage // com.apollographql.execution.websocket/WebSocketMessage|null[0]

sealed interface com.apollographql.execution.websocket/WsConnectionInitResult // com.apollographql.execution.websocket/WsConnectionInitResult|null[0]

final class com.apollographql.execution.websocket/ConnectionInitError : com.apollographql.execution.websocket/ConnectionInitResult { // com.apollographql.execution.websocket/ConnectionInitError|null[0]
constructor <init>(com.apollographql.apollo.api/Optional<kotlin/Any?> = ...) // com.apollographql.execution.websocket/ConnectionInitError.<init>|<init>(com.apollographql.apollo.api.Optional<kotlin.Any?>){}[0]

final val payload // com.apollographql.execution.websocket/ConnectionInitError.payload|{}payload[0]
final fun <get-payload>(): com.apollographql.apollo.api/Optional<kotlin/Any?> // com.apollographql.execution.websocket/ConnectionInitError.payload.<get-payload>|<get-payload>(){}[0]
}

final class com.apollographql.execution.websocket/GraphQLWsWebSocketHandler : com.apollographql.execution.websocket/WebSocketHandler { // com.apollographql.execution.websocket/GraphQLWsWebSocketHandler|null[0]
constructor <init>(com.apollographql.apollo.execution/ExecutableSchema, kotlinx.coroutines/CoroutineScope, com.apollographql.apollo.api/ExecutionContext, kotlin.coroutines/SuspendFunction1<com.apollographql.execution.websocket/WebSocketMessage, kotlin/Unit>, kotlin.coroutines/SuspendFunction1<kotlin/Any?, com.apollographql.execution.websocket/WsConnectionInitResult> = ...) // com.apollographql.execution.websocket/GraphQLWsWebSocketHandler.<init>|<init>(com.apollographql.apollo.execution.ExecutableSchema;kotlinx.coroutines.CoroutineScope;com.apollographql.apollo.api.ExecutionContext;kotlin.coroutines.SuspendFunction1<com.apollographql.execution.websocket.WebSocketMessage,kotlin.Unit>;kotlin.coroutines.SuspendFunction1<kotlin.Any?,com.apollographql.execution.websocket.WsConnectionInitResult>){}[0]

final fun close() // com.apollographql.execution.websocket/GraphQLWsWebSocketHandler.close|close(){}[0]
final fun handleMessage(com.apollographql.execution.websocket/WebSocketMessage) // com.apollographql.execution.websocket/GraphQLWsWebSocketHandler.handleMessage|handleMessage(com.apollographql.execution.websocket.WebSocketMessage){}[0]
}

final class com.apollographql.execution.websocket/SubscriptionWebSocketHandler : com.apollographql.execution.websocket/WebSocketHandler { // com.apollographql.execution.websocket/SubscriptionWebSocketHandler|null[0]
constructor <init>(com.apollographql.apollo.execution/ExecutableSchema, kotlinx.coroutines/CoroutineScope, com.apollographql.apollo.api/ExecutionContext, kotlin.coroutines/SuspendFunction1<com.apollographql.execution.websocket/WebSocketMessage, kotlin/Unit>, kotlin.coroutines/SuspendFunction1<kotlin/Any?, com.apollographql.execution.websocket/ConnectionInitResult> = ...) // com.apollographql.execution.websocket/SubscriptionWebSocketHandler.<init>|<init>(com.apollographql.apollo.execution.ExecutableSchema;kotlinx.coroutines.CoroutineScope;com.apollographql.apollo.api.ExecutionContext;kotlin.coroutines.SuspendFunction1<com.apollographql.execution.websocket.WebSocketMessage,kotlin.Unit>;kotlin.coroutines.SuspendFunction1<kotlin.Any?,com.apollographql.execution.websocket.ConnectionInitResult>){}[0]

Expand All @@ -83,6 +92,13 @@ final class com.apollographql.execution.websocket/WebSocketTextMessage : com.apo
final fun <get-data>(): kotlin/String // com.apollographql.execution.websocket/WebSocketTextMessage.data.<get-data>|<get-data>(){}[0]
}

final class com.apollographql.execution.websocket/WsConnectionInitError : com.apollographql.execution.websocket/WsConnectionInitResult { // com.apollographql.execution.websocket/WsConnectionInitError|null[0]
constructor <init>(com.apollographql.apollo.api/Optional<kotlin/Any?> = ...) // com.apollographql.execution.websocket/WsConnectionInitError.<init>|<init>(com.apollographql.apollo.api.Optional<kotlin.Any?>){}[0]

final val payload // com.apollographql.execution.websocket/WsConnectionInitError.payload|{}payload[0]
final fun <get-payload>(): com.apollographql.apollo.api/Optional<kotlin/Any?> // com.apollographql.execution.websocket/WsConnectionInitError.payload.<get-payload>|<get-payload>(){}[0]
}

final class com.apollographql.execution/CompositeResolverBuilder { // com.apollographql.execution/CompositeResolverBuilder|null[0]
constructor <init>() // com.apollographql.execution/CompositeResolverBuilder.<init>|<init>(){}[0]

Expand All @@ -103,6 +119,11 @@ final object com.apollographql.execution.websocket/ConnectionInitAck : com.apoll
final fun toString(): kotlin/String // com.apollographql.execution.websocket/ConnectionInitAck.toString|toString(){}[0]
}

final fun (com.apollographql.apollo.api/ExecutionContext).com.apollographql.execution.websocket/subscriptionId(): kotlin/String // com.apollographql.execution.websocket/subscriptionId|[email protected](){}[0]
final object com.apollographql.execution.websocket/WsConnectionInitAck : com.apollographql.execution.websocket/WsConnectionInitResult { // com.apollographql.execution.websocket/WsConnectionInitAck|null[0]
final fun equals(kotlin/Any?): kotlin/Boolean // com.apollographql.execution.websocket/WsConnectionInitAck.equals|equals(kotlin.Any?){}[0]
final fun hashCode(): kotlin/Int // com.apollographql.execution.websocket/WsConnectionInitAck.hashCode|hashCode(){}[0]
final fun toString(): kotlin/String // com.apollographql.execution.websocket/WsConnectionInitAck.toString|toString(){}[0]
}

final fun (com.apollographql.apollo.execution/ExecutableSchema.Builder).com.apollographql.execution/compositeResolver(kotlin/Function1<com.apollographql.execution/CompositeResolverBuilder, kotlin/Unit>): com.apollographql.apollo.execution/ExecutableSchema.Builder // com.apollographql.execution/compositeResolver|compositeResolver@com.apollographql.apollo.execution.ExecutableSchema.Builder(kotlin.Function1<com.apollographql.execution.CompositeResolverBuilder,kotlin.Unit>){}[0]
final fun com.apollographql.execution/sandboxHtml(kotlin/String, kotlin/String): kotlin/String // com.apollographql.execution/sandboxHtml|sandboxHtml(kotlin.String;kotlin.String){}[0]
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.apollographql.execution

import com.apollographql.apollo.api.Error
import com.apollographql.apollo.api.json.BufferedSinkJsonWriter
import com.apollographql.apollo.api.json.JsonWriter
import com.apollographql.apollo.api.json.writeAny
import com.apollographql.apollo.api.json.writeArray
import com.apollographql.apollo.api.json.writeObject
import okio.BufferedSink
import okio.Sink
import okio.buffer

internal fun JsonWriter.writeError(error: Error) {
writeObject {
name("message")
value(error.message)
if (error.locations != null) {
name("locations")
writeArray {
error.locations!!.forEach {
writeObject {
name("line")
value(it.line)
name("column")
value(it.column)
}
}
}
}
if (error.path != null) {
name("path")
writeArray {
error.path!!.forEach {
when (it) {
is Int -> value(it)
is String -> value(it)
else -> error("path can only contain Int and Double (found '${it::class.simpleName}')")
}
}
}
}
if (error.extensions != null) {
name("extensions")
writeObject {
error.extensions!!.entries.forEach {
name(it.key)
writeAny(it.value)
}
}
}
}
}

internal fun Sink.jsonWriter(): JsonWriter = BufferedSinkJsonWriter(if (this is BufferedSink) this else this.buffer())
Loading
Loading