Skip to content
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ docs.json

# Local Netlify folder
.netlify
/ref-docs/
3 changes: 3 additions & 0 deletions build-logic/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ dependencies {
compileOnly(libs.dgp)

implementation(libs.okhttp)
implementation(libs.ktor.server.netty)
implementation(libs.ktor.server.cors)
implementation(libs.ktor.server.websockets)

implementation(libs.kotlinx.benchmark)
implementation(libs.dokka)
Expand Down
1 change: 1 addition & 0 deletions build-logic/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ org.gradle.jvmargs=-Xmx8g
org.gradle.caching=true
# Enable the configuration cache
# org.gradle.unsafe.configuration-cache=true
ksp.allow.all.target.configuration=false
38 changes: 10 additions & 28 deletions build-logic/src/main/kotlin/Mpp.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import org.gradle.api.Action
import org.gradle.api.Project
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
Expand Down Expand Up @@ -34,42 +33,27 @@ private val enableJs = System.getenv("APOLLO_JVM_ONLY")?.toBoolean()?.not() ?: t
private val enableApple = System.getenv("APOLLO_JVM_ONLY")?.toBoolean()?.not() ?: true


fun Project.configureMpp(
fun defaultTargets(
withJvm: Boolean,
withJs: Boolean,
withLinux: Boolean,
withAndroid: Boolean,
withWasm: Boolean,
appleTargets: Collection<String>,
browserTest: Boolean,
) {
val kotlinExtension = extensions.findByName("kotlin") as? KotlinMultiplatformExtension
check(kotlinExtension != null) {
"No multiplatform extension found"
}
kotlinExtension.apply {
): KotlinMultiplatformExtension.() -> Unit {
return {
if (withJvm) {
jvm()
}

if (enableJs && withJs) {
js(IR) {
if (browserTest) {
browser {
testTask(Action {
useKarma {
useChromeHeadless()
}
})
}
} else {
nodejs {
testTask(Action {
useMocha {
// Override default 2s timeout
timeout = "120s"
}
})
nodejs {
testTask {
useMocha {
// Override default 2s timeout
timeout = "120s"
}
}
}
}
Expand Down Expand Up @@ -108,8 +92,6 @@ fun Project.configureMpp(
}
}
}

configureSourceSetGraph()
}
}

Expand Down Expand Up @@ -143,7 +125,7 @@ fun Project.configureMpp(
* ```
*/
@OptIn(ExperimentalKotlinGradlePluginApi::class)
private fun KotlinMultiplatformExtension.configureSourceSetGraph() {
internal fun KotlinMultiplatformExtension.configureSourceSetGraph() {
applyDefaultHierarchyTemplate {
group("common") {
group("filesystem") {
Expand Down
76 changes: 51 additions & 25 deletions build-logic/src/main/kotlin/api.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ fun Project.version(): String {
fun Project.apolloLibrary(
namespace: String,
jvmTarget: Int? = null,
withJs: Boolean = true,
withLinux: Boolean = true,
withApple: Boolean = true,
withJvm: Boolean = true,
withWasm: Boolean = true,
defaultTargets: (KotlinMultiplatformExtension.() -> Unit),
androidOptions: AndroidOptions? = null,
publish: Boolean = true,
kotlinCompilerOptions: KotlinCompilerOptions = KotlinCompilerOptions(),
Expand Down Expand Up @@ -69,15 +65,11 @@ fun Project.apolloLibrary(
}

if (extensions.findByName("kotlin") is KotlinMultiplatformExtension) {
configureMpp(
withJvm = withJvm,
withJs = withJs,
browserTest = false,
withLinux = withLinux,
appleTargets = if (!withApple) emptySet() else allAppleTargets,
withAndroid = extensions.findByName("android") != null,
withWasm = withWasm
)
val kotlinExtension = extensions.findByName("kotlin") as? KotlinMultiplatformExtension
if (kotlinExtension != null) {
kotlinExtension.defaultTargets()
kotlinExtension.configureSourceSetGraph()
}
}

configureTesting()
Expand Down Expand Up @@ -105,12 +97,52 @@ fun Project.apolloLibrary(
}
}

fun Project.apolloLibrary(
namespace: String,
jvmTarget: Int? = null,
withJs: Boolean = true,
withLinux: Boolean = true,
withApple: Boolean = true,
withJvm: Boolean = true,
withWasm: Boolean = true,
androidOptions: AndroidOptions? = null,
publish: Boolean = true,
kotlinCompilerOptions: KotlinCompilerOptions = KotlinCompilerOptions(),
) {
val defaultTargets = defaultTargets(
withJvm = withJvm,
withJs = withJs,
withLinux = withLinux,
appleTargets = if (!withApple) emptySet() else allAppleTargets,
withAndroid = androidOptions != null,
withWasm = withWasm
)

apolloLibrary(
namespace,
jvmTarget,
defaultTargets,
androidOptions,
publish,
kotlinCompilerOptions
)
}

fun Project.apolloTest(
withJs: Boolean = true,
withJvm: Boolean = true,
appleTargets: Set<String> = setOf(hostTarget),
browserTest: Boolean = false,
kotlinCompilerOptions: KotlinCompilerOptions = KotlinCompilerOptions(),
) {
apolloTest(
kotlinCompilerOptions,
defaultTargets(withJvm = withJvm, withJs = withJs, withLinux = false, withAndroid = false, withWasm = false, appleTargets = appleTargets),
)
}

fun Project.apolloTest(
kotlinCompilerOptions: KotlinCompilerOptions = KotlinCompilerOptions(),
block: KotlinMultiplatformExtension.() -> Unit,
) {
commonSetup()
configureJavaAndKotlinCompilers(
Expand All @@ -123,16 +155,10 @@ fun Project.apolloTest(
)
)

if (extensions.findByName("kotlin") is KotlinMultiplatformExtension) {
configureMpp(
withJvm = withJvm,
withJs = withJs,
browserTest = browserTest,
withLinux = false,
withAndroid = false,
appleTargets = appleTargets,
withWasm = false
)
val kotlinExtension = extensions.findByName("kotlin") as? KotlinMultiplatformExtension
if (kotlinExtension != null) {
kotlinExtension.block()
kotlinExtension.configureSourceSetGraph()
}
configureTesting()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package websocket_server

import io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import io.ktor.server.plugins.cors.routing.*
import io.ktor.server.routing.routing
import io.ktor.server.websocket.WebSockets
import io.ktor.server.websocket.webSocket
import io.ktor.websocket.CloseReason
import io.ktor.websocket.Frame
import io.ktor.websocket.close
import io.ktor.websocket.readText
import io.ktor.websocket.send
import kotlinx.coroutines.cancel
import kotlinx.coroutines.coroutineScope
import kotlinx.io.files.Path
import org.gradle.api.services.BuildService
import org.gradle.api.services.BuildServiceParameters
import java.lang.AutoCloseable

private val port = 18923

private fun server(): EmbeddedServer<NettyApplicationEngine, NettyApplicationEngine.Configuration> {
return embeddedServer(Netty, port = port) {
install(CORS) {
/**
* This is very permissive. It's only for tests but feel free to restrict if needed.
*/
anyMethod()
anyHost()
allowHeaders { true }
allowNonSimpleContentTypes = true
}
install(WebSockets)
routing {
webSocket("/echo") {
for (frame in incoming) {
if (frame is Frame.Text && frame.readText() == "bye") {
close(CloseReason(4400, "bye"))
} else {
if (frame is Frame.Text) {
send(frame.readText())
} else {
send(frame.data)
}
}
}
}
}
}
}

abstract class ServerBuildService : BuildService<BuildServiceParameters.None>, AutoCloseable {
private var server: EmbeddedServer<NettyApplicationEngine, NettyApplicationEngine.Configuration>? = null
private var refcount = 0

@Synchronized
fun startServer() {
if (refcount == 0) {
println("Starting echo server")
server = server().start(wait = false)
}
refcount++
}

fun stopServer() {
check(refcount > 0) {
"Server not started"
}
refcount--
if (refcount == 0) {
close()
}
}

@Synchronized
override fun close() {
if (server != null) {
println("Stopping echo server")
server!!.stop(1000, 1000)
server = null
refcount = 0
}
}
}
28 changes: 28 additions & 0 deletions build-logic/src/main/kotlin/websocket_server/server-tasks.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package websocket_server

import org.gradle.api.DefaultTask
import org.gradle.api.provider.Property
import org.gradle.api.services.ServiceReference
import org.gradle.api.tasks.TaskAction


abstract class StartServerTask: DefaultTask() {
@get:ServiceReference
abstract val buildService: Property<ServerBuildService>

@TaskAction
fun taskAction() {
buildService.get().startServer()
}
}

abstract class StopServerTask: DefaultTask() {
@get:ServiceReference
abstract val buildService: Property<ServerBuildService>

@TaskAction
fun taskAction() {
buildService.get().stopServer()
}
}

23 changes: 23 additions & 0 deletions build-logic/src/main/kotlin/websocket_server/wiring.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package websocket_server

import org.gradle.api.Project
import org.gradle.api.tasks.testing.AbstractTestTask
import org.jetbrains.kotlin.gradle.targets.js.testing.KotlinJsTest
import org.jetbrains.kotlin.gradle.targets.js.webpack.KotlinWebpack
import org.jetbrains.kotlin.gradle.tasks.KotlinTest

fun Project.configureWebSocketServer() {
val startServer = tasks.register("startServer", StartServerTask::class.java)
val stopServer = tasks.register("stopServer", StartServerTask::class.java)

gradle.sharedServices.registerIfAbsent("websocketServer", ServerBuildService::class.java)
tasks.withType(AbstractTestTask::class.java).configureEach {
dependsOn(startServer)
finalizedBy(stopServer)
}
// For development
tasks.withType(KotlinWebpack::class.java).configureEach {
dependsOn(startServer)
finalizedBy(stopServer)
}
}
2 changes: 2 additions & 0 deletions gradle/libraries.toml
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ ktor-client-darwin = { group = "io.ktor", name = "ktor-client-darwin", version.r
ktor-client-js = { group = "io.ktor", name = "ktor-client-js", version.ref = "ktor" }
ktor-client-websockets = { group = "io.ktor", name = "ktor-client-websockets", version.ref = "ktor" }
ktor-server-core = { group = "io.ktor", name = "ktor-server-core", version.ref = "ktor" }
ktor-server-cors = { group = "io.ktor", name = "ktor-server-cors", version.ref = "ktor" }
ktor-server-cio = { group = "io.ktor", name = "ktor-server-cio", version.ref = "ktor" }
ktor-server-netty = { group = "io.ktor", name = "ktor-server-netty", version.ref = "ktor" }
ktor-server-websockets = { group = "io.ktor", name = "ktor-server-websockets", version.ref = "ktor" }
ktor-network = { module = "io.ktor:ktor-network", version.ref = "ktor" }
okhttp = { group = "com.squareup.okhttp3", name = "okhttp", version.ref = "okhttp" }
Expand Down
2 changes: 2 additions & 0 deletions libraries/apollo-runtime/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
import org.jetbrains.kotlin.gradle.plugin.KotlinDependencyHandler
import websocket_server.configureWebSocketServer

plugins {
id("org.jetbrains.kotlin.multiplatform")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.apollographql.apollo.network.internal

/**
* Converts an url to a WebSocket url as expected by the browser WebSocket API.
*/
internal fun String.toWebSocketUrl(): String {
return when {
startsWith("http://") -> "ws://${substring(7)}"
startsWith("https://") -> "wss://${substring(8)}"
else -> this
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ class GraphQLWsProtocol(
"pong" -> PongServerMessage
"next", "complete", "error" -> {
val id = map["id"] as? String

/**
* See https://youtrack.jetbrains.com/issue/KT-79673/Kotlin-Wasm-object-WebAssembly.Exception-in-production-mode
* and https://github.com/apollographql/apollo-kotlin/pull/6637#discussion_r2240372931 for why this `if` block is needed.
*/
if (type == "next") {
return ResponseServerMessage(id.toString(), map["payload"])
}
when {
id == null -> ParseErrorServerMessage("No 'id' found in message: '$text'")
type == "next" -> ResponseServerMessage(id, map["payload"])
Expand Down
Loading
Loading