Whither ApolloHttpNetworkTransport?

I’m attempting to follow the Multiplatform example for Apollo.

No matter what I do, ApolloHttpNetworkTransport can’t be resolved in my sources.

Here’s the code snippet:

import com.apollographql.apollo3.ApolloClient

    val apolloClient = ApolloClient(
        networkTransport = ApolloHttpNetworkTransport(
            serverUrl = "https://foo.com/graphql",
            headers = mapOf(
                "Accept" to "application/json",
                "Content-Type" to "application/json",
                "X-API-KEY" to "blah",
            )
        )
    )

Searching google and SO yields no applicable result.

Here’s the build.gradle.kts from the root of the project:

buildscript {
    val kotlinVersion: String by project

    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
        maven {
            url = uri("https://oss.sonatype.org/content/repositories/snapshots/")
        }
    }

    dependencies {
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion")
        classpath("com.android.tools.build:gradle:7.0.0")
        classpath("com.apollographql.apollo3:apollo-gradle-plugin:${Versions.apollo}")
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

tasks.register("clean", Delete::class) {
    delete(rootProject.buildDir)
}

and from the shared directory:

import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget

plugins {
    kotlin("multiplatform")
    id("com.android.library")
    id("com.apollographql.apollo3")
}

kotlin {
    android()

    val iosTarget: (String, KotlinNativeTarget.() -> Unit) -> KotlinNativeTarget =
        if (System.getenv("SDK_NAME")?.startsWith("iphoneos") == true)
            ::iosArm64
        else
            ::iosX64

    iosTarget("ios") {
        binaries {
            framework {
                baseName = "shared"
            }
        }
    }
    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:${Versions.kotlinxCoroutines}") {
                    isForce = true
                }

                api("com.apollographql.apollo3:apollo-runtime:${Versions.apollo}")
                runtimeOnly("com.apollographql.apollo3:apollo-api:${Versions.apollo}")
                implementation("com.apollographql.apollo:apollo-android-support:2.5.9")
                implementation("io.ktor:ktor-client-core:${Versions.ktor}")
                implementation("io.ktor:ktor-client-cio:${Versions.ktor}")
                implementation("io.ktor:ktor-client-logging:${Versions.ktor}")
                implementation("io.ktor:ktor-client-auth:${Versions.ktor}")
            //                implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:${Versions.kotlinCoroutines}")
            }
        }
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test-common"))
                implementation(kotlin("test-annotations-common"))
            }
        }
        val androidMain by getting {
            dependencies {
                implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:${Versions.kotlinxCoroutines}")
                implementation("io.ktor:ktor-client-android:${Versions.ktor}")
            }
        }
        val androidTest by getting {
            dependencies {
                implementation(kotlin("test-junit"))
                implementation("junit:junit:4.13.2")
            }
        }
        val iosMain by getting {
            dependencies{
                implementation("io.ktor:ktor-client-ios:${Versions.ktor}")
            }
        }
        val iosTest by getting
    }
}

android {
    compileSdkVersion(30)
    sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
    defaultConfig {
        minSdkVersion(23)
        targetSdkVersion(30)
    }
}
//dependencies {
//    implementation(project(mapOf("path" to ":")))
//    implementation(project(mapOf("path" to ":")))
//}

kotlin.sourceSets.all {
    languageSettings.apply {
        useExperimentalAnnotation("kotlin.RequiresOptIn")
        useExperimentalAnnotation("kotlinx.coroutines.ExperimentalCoroutinesApi")
        useExperimentalAnnotation("com.apollographql.apollo3.api.ApolloExperimental")
    }
}

apollo {
    generateKotlinModels.set(true)
    codegenModels.set("operationBased")
}

Ignore the ktor stuff. I was originally going to use ktor, but found out it doesn’t support TLS in native projects.

Any pointers?

Hi ! Can you share your Gradle configuration? You’ll most likely have to include the apollo-runtime-kotlin artifact (and not the apollo-runtime one). If you’re using an older version of Gradle, maybe try apollo-runtime-kotlin-jvm but if that’s the case, I’d highly recommend moving to a more recent version of Gradle for multiplatform development.
Hope this helps, let us know how that goes!

I’ve update my question to include the grade files.

Thanks for sending this! You’re using the 3.x alpha that renamed ApolloHttpNetworkTransport to HttpNetworkTransport. The docs are available at Introduction to Apollo Kotlin - Apollo GraphQL Docs

It’s very likely that you don’t need a custom HttpNetworkTransport so you should be able to do something like

val apolloClient = ApolloClient(serverUrl)

Cool, thanks for the answer. One last question. The V2 docs have explicit instructions for Kotlin Multiplatform projects. Does this still apply to V3, even though there is no mention of Multiplatform in the V3 docs? Or should I stick with V2 for now?

V3 is a rewrite of Apollo Android using 100% Kotlin and designed for multiplatform. While V2 had 2 different runtimes, V3 is multiplatform from the ground up and uses the same Kotlin code on the JVM and native.

We strongly recommend using V3 as it has way more multiplatform features than V2 (normalized cache, batching, etc…)

1 Like