Apollo-android: Apollo files are not generated after upgrading to 1.3.0.

Created on 5 Feb 2020  路  17Comments  路  Source: apollographql/apollo-android

Summary
Apollo files are not generated with Apollo 1.3.0.

Description
Hello,

I'm struggling to upgrade to v1.3.0, apollo files are not generated with it. I have the following tree:

- app
- moduleA => module where I want to have apollo generated classes with "my.package.common" as package
- someGitModule
    schema.json
    - FeatureA
        - Fragment
            fragmentA.qraphql
        - Query
            queryA.graphql
    - FeatureB
        - Fragment
            fragmentB.qraphql
        - Query
            queryB.graphql

Apollo v1.2.x: Works fine, I get all files from all features generated in moduleA with "my.package.common" as package

android {
    sourceSets {
        main.graphql.srcDirs += "../../someGitModule"
    }
}

apollo {
    generateKotlinModels.set(true)

    sourceSet {
        schemaFile = "../../someGitModule/schema.json"
    }
    outputPackageName = "my.package.common"
}

Apollo v1.3: files are not generated

apollo {
    onCompilationUnits { // and not "onCompilationUnit " like in ReadMe, how come?
        schemaFile = file("../../someGitModule/schema.json")
        rootPackageName = "my.package.common"
        graphqlSourceDirectorySet.srcDirs += "../../someGitModule/.."
    }

    generateKotlinModels.set(true)
}
  • Should the "apollo {}" be inside "android {}"?
  • What would be the proper syntax with graphqlSourceDirectorySet.srcDir to have same result as in v1.2.x? With my current syntax, it seems to generate apollo files but only for test (folders debugUnitTest/debugAndroidTest/releaseUnitTest and debug is missing), did I miss something?

Version
1.3.0

Bug

Most helpful comment

I would like to also note that defining schema.json location is enough since the new plugin already searches for all .graphql files within the same folder (including nested folders).

So this might also work for you:

apollo {
    schemaFile = file("../../someGitModule/schema.json")
    rootPackageName = "my.package.common"
    generateKotlinModels.set(true)
}

All 17 comments

Assuming you have your apollo configuration in moduleA/build.gradle, you only need to move up once ("../someGitModule" and not "../../someGitModule") to point to your git module. I'm not sure why you had to move up twice before.

Can you try the following:

apollo {
    onCompilationUnits { // this will become `onCompilationUnit` in 1.3.1
        schemaFile = file("../someGitModule/schema.json")
        rootPackageName = "my.package.common"
        graphqlSourceDirectorySet.srcDirs += file("../someGitModule/")
        graphqlSourceDirectorySet.include("**/*.graphql")
    }

    generateKotlinModels.set(true)
}

If that still doesn't work, can you make sure file("../someGitModule/") points to an existing directory ?

I would like to also note that defining schema.json location is enough since the new plugin already searches for all .graphql files within the same folder (including nested folders).

So this might also work for you:

apollo {
    schemaFile = file("../../someGitModule/schema.json")
    rootPackageName = "my.package.common"
    generateKotlinModels.set(true)
}

Also pls check migration doc: https://github.com/apollographql/apollo-android#migrating-to-13x

If it's missing something let us know.

Thank you all for your answer !

@martinbonnin: Sorry, there was a mistake in my tree, moduleA has a parent that's why I need to move up twice. I tried your solution (with "../../" to move up twice) but it didn't work, only test files were generated. However it is working with @tasomaniac solution.

@martinbonnin The most common usecase is to keep schema.json and GraphQL file together even when the user wants to keep them in a separate place. That's how we also use it at our project since we share them with iOS codebase.

I'm not sure if users would have schema in one place and GraphQL files in a completely different place.

That's why maybe we can adjust documentation to move sourceSet piece into a separate section and make it clear that it is only needed if you want to do something custom. Maybe even with an example where you had a Gradle task that downloads the files etc...

If you are ok with the idea, maybe I can work on a change.

I'm not sure if users would have schema in one place and GraphQL files in a completely different place.

Yep, I'm not sure I remember someone doing this.

That's why maybe we can adjust documentation to move sourceSet piece into a separate section and make it clear that it is only needed if you want to do something custom. Maybe even with an example where you had a Gradle task that downloads the files etc...

Sounds good to me. I wouldn't go too far in the gradle task thing etc until we actually find someone needing this :sweat_smile: . Adding too much details might confuse users more.

I'm specifying the location as so

apollo {
    onCompilationUnit {
        schemaFile = file("../schema.json")
        rootPackageName = "com.company.module"
        generateKotlinModels = true
    }
}

But I'm getting the error

* What went wrong:
Execution failed for task ':module:generateDebugServiceApolloSources'.
> ApolloGraphQL: cannot find schema.json. Please specify it explicitely. Looked under:
  /home/jacob/git/project/module/src/main/graphql
  /home/jacob/git/project/module/src/debug/graphql

Also would be good for the docs to use the file method as it's currently just using a string directly which doesn't work.

Hey, are you sure file("../schema.json") points to the correct location?

You can try debugging by calling print or even exists property to check if this file exists

Something like

print(file("../schema.json").exists)

This will print when you call any of the tasks or when you hit build/sync on Android Studio

Yep I've checked that it logs /home/jacob/git/project/schema.json, which is the correct path, I just checked the exists method and that returns true as well.

Could that be a symlink or hardlink ?

It's not a symlink, the file is at that location.

@japplin I added a testcase there: https://github.com/apollographql/apollo-android/pull/2032/files. If you could modify it or make a separate repo that demonstrate the issue that'll help a lot.

Something else I was thinking about is maybe the plugin is applied to multiple modules (i.e. gradle projects) ? Then the error would come from another project than the one you're looking at ?

Yea that's a good idea, I checked and the module with the errors matches the one I'm configuring. I do have apollo installed in several sibling modules though.

Can you try without onCompilationUnit? Just under apollo {}

That fixed the problem thank you!

I think I got it. It's a chicken an egg problem that'll only happen for android projects:

  • onCompilationUnit {} needs to happen after the codegenTask is registered because compilationUnionUnit.outputDir should carry the task dependency
  • but onCompilationUnit {} needs also to happen before the codegenTask is configured because the codegenTask uses compilationUnit.schemaFile

This works well for non-android projects because everything happens lazily but android requires eager configuration hence configures the task before the user had any chance to set schemaFile :(.

Good catch. That sounds about right.

Was this page helpful?
0 / 5 - 0 ratings