Search code examples
androidbuild.gradleapolloapollo-clientgradle-kotlin-dsl

Set different schema for different build types in Apollo v3 Android


I have 2 GraphQl schemas for debug and prod versions of my app. Also I have 2 build types: debug and release. Obviously, I want to use the debug-schema with the debug build-type, and the production-schema with the release one.

According to this issue, Apollo v2 allows set such configuration using onCompilationUnit, and probably I can write smth like this:

apollo {
    onCompilationUnit {
        graphqlSourceDirectorySet.srcDirs += "src/main/graphql"
        def buildTypeName =(androidVariant as BaseVariant).buildType.name
        if (buildTypeName.contains('debug')) {
            graphqlSourceDirectorySet.srcDirs += "src/debug/graphql"
        } else if (buildTypeName.contains('release')) {
            graphqlSourceDirectorySet.srcDirs += "src/release/graphql"
        }
    }
}

The problem is that I didn't find onCompilationUnit in Apollo v3 (which is used in my project).

The main question is: How to configure gradle to force Apollo v3 use different schemas for different build-types? I think analogue of onCompilationUnit in Apollo v3 will resolve this question. Please suggest me one if it exists in Apollo v3.

If there are some other ways to resolve the issue, you are welcome to answer :)


Solution

  • According to the @BoD answer in the GitHub issue mentioned in the question, the new way to take Android flavors into accounts is createAllAndroidVariantServices, which basically specifies different sources for the different build-types. So, as documentation says, I have created src/debug/graphql/$sourceFolder with my debug-schema, and correspondingly src/release/graphql/$sourceFolder with the prod-schema. I have also added next config to my build.gradle.kts:

    apollo {
        createAllAndroidVariantServices(".", "") {
            if (name.contains("staging")) {
                // add if you have staging build-variant, which uses the same scheme as prod
                srcDir(file("src/release/graphql/"))
            }
    
            packageName.set("com.example.myapp")
        }
    }