Skip to content
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

No apollo code generation for gql operations? #5550

Closed
rb090 opened this issue Jan 22, 2024 · 5 comments
Closed

No apollo code generation for gql operations? #5550

rb090 opened this issue Jan 22, 2024 · 5 comments
Labels
⌛ Waiting for info More information is required ❓ Type: Question

Comments

@rb090
Copy link

rb090 commented Jan 22, 2024

Question

I have a Kotlin Multiplatform project and I setup my Apollo Client within my shared/build.gradle.kts like this:

plugins {
    alias(libs.plugins.kotlinMultiplatform)
    alias(libs.plugins.androidLibrary)
    id("com.apollographql.apollo3") version "4.0.0-beta.4"
}

apollo {

    // For each API endpoint we have a different apollo service setup
    service("backendApi") {
        generateFragmentImplementations.set(true)
        generateSchema.set(true)
        generateKotlinModels.set(true)
        generateApolloMetadata.set(true)
        sourceFolder.set("src/commonMain/graphql/backend-api/schema.graphqls")
        schemaFiles.setFrom("src/commonMain/graphql/backend-api/schema.graphqls")
        packageName.set("com.backend.api.apollo.codegen")
    }
}

kotlin {
    androidTarget {
        compilations.all {
            kotlinOptions {
                jvmTarget = "16"
            }
        }
    }
    
    listOf(
        iosX64(),
        iosArm64(),
        iosSimulatorArm64()
    ).forEach {
        it.binaries.framework {
            baseName = "shared"
            isStatic = true
        }
    }

    sourceSets {
        commonMain.dependencies {
            implementation("com.apollographql.apollo3:apollo-runtime:4.0.0-beta.4")
            // optional: if you want to use the normalized cache
            implementation("com.apollographql.apollo3:apollo-normalized-cache-sqlite:4.0.0-beta.4")
            // optional: if you just want the generated models and parsers and write your own HTTP code/cache code, you can remove apollo-runtime
            // and use apollo-api instead
            implementation("com.apollographql.apollo3:apollo-api:4.0.0-beta.4")
        }
        commonTest.dependencies {
            implementation(libs.kotlin.test)
        }
    }
}

android {
    namespace = "com.sth.playground.mobile"
    compileSdk = 34
    defaultConfig {
        minSdk = 29
    }
    kotlin {
        jvmToolchain(jdkVersion = 16)
    }
}

Running ./gradlew build generates me all the types as expected. But no operations written in src/commonMain/graphql/backend-api are generated.

I have a query operation within src/commonMain/graphql/backend-api/GetUserTest.graphql looking like this:

query GetUserTest {
    getMyUser {
        ...MyUser
    }
}

fragment MyUser on TheUser {
    id
    firstName
    lastName
}

Can you please tell me what I am doing wrong and why there is no code generation for GetUserTest like in your samples?

@rb090 rb090 changed the title No apollo code generation for mutations? No apollo code generation for gql operations? Jan 22, 2024
@martinbonnin
Copy link
Contributor

Hi 👋

sourceFolder is relative to the src/commonMain directory so you should be able to do

apollo {

    // For each API endpoint we have a different apollo service setup
    service("backendApi") {
        sourceFolder.set("graphql/backend-api")
    }
}

to have the compiler pick up the GraphQL queries. Alternatively, you can also remove sourceFolder.set and schemaFiles.setFrom and the plugin will use src/commonMain/graphql by default.

Let me know how that works

@martinbonnin martinbonnin added the ⌛ Waiting for info More information is required label Jan 22, 2024
@rb090
Copy link
Author

rb090 commented Jan 22, 2024

Hi @martinbonnin 👋,

thank you so much for your response ❤️. After disabling sourceFolder code generation worked as expected. But when adding another service like this:

...
apollo {
    service("backendApi") {
        generateFragmentImplementations.set(true)
        generateSchema.set(true)
        generateKotlinModels.set(true)
        generateApolloMetadata.set(true)
        sourceFolder.set("src/commonMain/graphql/backend-api/schema.graphqls")
        schemaFiles.setFrom("src/commonMain/graphql/backend-api/schema.graphqls")
        packageName.set("com.backend.api.apollo.codegen")
    }
    service("cmsApi") {
        generateFragmentImplementations.set(true)
        generateSchema.set(true)
        generateKotlinModels.set(true)
        generateApolloMetadata.set(true)
        sourceFolder.set("src/commonMain/graphql/cms-api/schema.graphqls")
        schemaFiles.setFrom("src/commonMain/graphql/cms-api/schema.graphqls")
        packageName.set("com.cms.api.apollo.codegen")

        introspection {
            endpointUrl.set("https://url.to.my.cms/graphql")
            schemaFile.set(file("src/commonMain/graphql/cms-api/schema.graphqls"))
        }
    }
}
...

Codegen for backendApi seems to stop working 😞. Can you please tell me how to deal with multiple services? I thought that this is what sourceFolder.set("...") is good for.

@martinbonnin
Copy link
Contributor

Indeed, if you want multiple services, you'll need sourceFolder or else everything under src/commonMain/graphql will be merged (which is not what you want). You can do something like below:

apollo {
    service("backendApi") {
        generateKotlinModels.set(true)
        sourceFolder.set("backend-api")
        packageName.set("com.backend.api.apollo.codegen")
    }
    service("cmsApi") {
        generateKotlinModels.set(true)
        sourceFolder.set("cms-api")
        packageName.set("com.cms.api.apollo.codegen")

        introspection {
            endpointUrl.set("https://url.to.my.cms/graphql")
            schemaFile.set(file("src/commonMain/graphql/cms-api/schema.graphqls"))
        }
    }
}

Hope this helps!

@rb090
Copy link
Author

rb090 commented Jan 22, 2024

Thanks a lot @martinbonnin. Your proposal solves 100% of the issues I had 🙂🙌. You saved my day.

@martinbonnin
Copy link
Contributor

Awesome, glad to hear that and thanks for the follow up 🎉 !
I'll close this issue for now. Feel free to open new ones for additional questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⌛ Waiting for info More information is required ❓ Type: Question
Projects
None yet
Development

No branches or pull requests

2 participants