How can I access the *_TestBuilder.Data from another Gradle module?

In my Android project I’m generating all the Apollo classes in a module called library-network-api. This module is generating also the Data builders and Test builders:

apollo {
    service("example") {
        packageName = "com.example.library.network.api.graphql"
        generateApolloMetadata = true
        codegenModels = "experimental_operationBasedWithInterfaces"
        decapitalizeFields = true
        generateDataBuilders = true
        generateTestBuilders = true
    }
}

I’m now trying to access the generated *_TestBuilder.Data classes from another Gradle module that has the library-network-api as dependency:

apollo {
    service("example") {
        packageNamesFromFilePaths()
    }
}

dependencies {
    apolloMetadata projects.modules.libraryNetworkApi
    implementation projects.modules.libraryNetworkApi
}

I guess that the issue is that the Test builds are generated inside the generated/source/apolloTest directory instead of the usual generated/source/apollo:

modules/library-network-api/build/generated/source/apolloTest/link/com/example/library/network/api/graphql/test/ActivityQuery_TestBuilder.kt

Everything in this folder doesn’t seem to be visible from the tests in another Gradle module.

Is there a known way to access this files from tests in another Gradle module?

2 Likes

Hi! :wave:

You are correct that they’re generated there by default, which is why your other modules can’t reference them. This is configurable however, like this:

apollo {
  service("exemple") {
    packageName = "com.example.library.network.api.graphql"
    // etc.

    testDirConnection {
      connectToAllAndroidVariants()
    }
  }
}

Although, may I ask if the data builders (which aim to replace the now deprecated test builders) are not enough for your use cases? (Data builders are already generated in the main sourceSet by default).

Hi @Benoit_Lubek, thank you for the reply!

Yeah, I think that the Test builders won’t actually help with my issue: while collecting some info to share here regarding my issue, I realized that most of the builders that I needed for the Data builders are actually generated.

The only one that is missing is the one for the Other case of a sealed interface.

For example, if you look at this auto generated class: //// AUTO-GENERATED FILE. DO NOT MODIFY.//// This class was automatically - Pastebin.com

I can’t find a builder to build the OtherActivity for the enqueueApolloSuccessUnknownTypeResponse using a Data builder like I did for the enqueueApolloSuccessGuidelineTypeResponse:

    private fun enqueueApolloSuccessUnknownTypeResponse() {
        val userQuery = ActivityQuery("docId", "expertId")
        val mockActivityUnknownTypeQuery: ActivityQuery.OtherActivity = mockk(relaxed = true)
        val data = ActivityQuery.Data(mockActivityUnknownTypeQuery)
        apolloClient.enqueueTestResponse(userQuery, data)
    }

    private fun enqueueApolloSuccessGuidelineTypeResponse() {
        val userQuery = ActivityQuery("docId", "expertId")
        val dataGuideline = ActivityQuery.Data(LinkFakeResolver()) { activity = buildGuideline { } }
        apolloClient.enqueueTestResponse(userQuery, dataGuideline)
    }

It’s true that currently only the builders for the known types (found in the schema) are generated. Here the Other models exist for the case where the server has a more recent version of the schema than the one used to generate the code on the client.
Is this this case you are trying to test here?

Yep, we want to test that, if we get a type that we don’t know, we get a specific result and not a crash or something else.

Thanks for confirming!

Interesting! We’ll have a look at adding data builders for this case (I’ve just opened this issue).
In the meantime you can instantiate manually (or continue using Mockk if that worked well for you).

For this case it could also be helpful to have your test use a JSON payload, either by using MockServer, or by parsing it directly in the test, like so:

    val data = userQuery.adapter().fromJson(
        Buffer().writeUtf8("""
        {
          "activity": {
            "__typename": "xyz",
            "docId": "123",
            // ...
          }
        }          
        """).jsonReader(),
        CustomScalarAdapters.Empty)
1 Like

Thank you! For now I’ll keep using the mock, but I’m subscribed to the GitHub issue :slight_smile:

1 Like