apollographql / apollo-kotlin

:rocket:  A strongly-typed, caching GraphQL client for the JVM, Android, and Kotlin multiplatform.
https://www.apollographql.com/docs/kotlin
MIT License
3.75k stars 651 forks source link

Dagger2 not working with AutoGenerated-Models #1255

Closed TayfunCesur closed 5 years ago

TayfunCesur commented 5 years ago

Hello everyone. I've been struggling with this issue for two days. It seems, Dagger2 not recognized Apollo's autogenerated model classes. Here is my ApiService, injects apollo client and makes query observable.

class ApiService @Inject constructor(private var apolloClient: ApolloClient) {
//Not works with PokemonRepositoryQuery.Pokemon

    fun getPokemons(count: Int): Observable<List<PokemonRepositoryQuery.Pokemon>> {
        return Rx2Apollo.from(apolloClient.query(PokemonRepositoryQuery.builder().first(count).build())).map {
            it.data()?.pokemons()
        }
    }
}

Error log

[ComponentProcessor:MiscError] dagger.internal.codegen.ComponentProcessor was unable to process this class because not all of its dependencies could be resolved. Check for compilation errors or a circular dependency with generated code.
public final class ApiService {

But if I use with my own Model class, like below, it works as expected.

class ApiService @Inject constructor(private var apolloClient: ApolloClient) {
    // Works with my own model
    private val mapper = PokemonMapper()

    fun getPokemons(count: Int): Observable<List<Pokemon>> {
        return Rx2Apollo.from(apolloClient.query(PokemonRepositoryQuery.builder().first(count).build())).map {
            it.data()?.pokemons()?.map { 
                mapper.mapToPokemon(it)
            }
        }
    }

}

My Query

query PokemonRepository($first:Int!){
  pokemons(first:$first) {
    id
    image
    name
  }
}

But as you think, I don't want to create my own model. That is why I'm learning Graphql :)

Any ideas ?

Thanks

yankeppey commented 5 years ago

Have you tried

kapt {
    correctErrorTypes = true
}

? https://kotlinlang.org/docs/reference/kapt.html

TayfunCesur commented 5 years ago

I've just tried .Didn't worked :/

TayfunCesur commented 5 years ago

Found the solution! I was wrong. I put the my .graphql and schema.json under graphql folder. I was working and Android Studio was recognizing. But If you want to use with Dagger2, you must put them under the graphql/{yourpackagename}/.graphql file. Otherwise, dagger2 won't recognize them.

Thanks

rusmichal commented 4 years ago

confirm, rootPackageName.set("com.packagename") in gradle file solved the problem