Apollo-android: annotation type not applicable to this kind of declaration

Created on 5 May 2020  路  11Comments  路  Source: apollographql/apollo-android

Summary
While running ./gradlew generateApolloSources the generated files for one of my mutations seems to a type class incorrectly. It builds with ./gradlew succesfully, but when Making Project it fails.
Build fails with error: annotation type not applicable to this kind of declaration
Description
I build with four queries.

mutation SignUpBarber($input: NewBarber!) {
    signUpBarber(input: $input) {
        response
        error
    }
}

query GetAllBarbers{
    getAllBarbers{
        barberID
        firstName
        lastName
    }
}

mutation RefreshToken($input: RefreshTokenInput!) {
    refreshToken(input: $input) {
        response
        error
    }
}

mutation LoginBarber($input: Login!) {
    login(input: $input) {
        response
        error
    }
}

The mutation that throws the error is LoginBarber.
RefreshToken mutationis quite similar, but it does not throw the error in question.

The error lies in this code

 public LoginBarberMutation(com.snipsnap.android.barbershop.type. @NotNull Login input) {
    Utils.checkNotNull(input, "input == null");
    variables = new LoginBarberMutation.Variables(input);
  }

While the mutation for RefreshToken looks like this and produces no error

 public RefreshTokenMutation(@NotNull RefreshTokenInput input) {
    Utils.checkNotNull(input, "input == null");
    variables = new RefreshTokenMutation.Variables(input);
  }

public LoginBarberMutation(com.snipsnap.android.barbershop.type. @NotNull Login input)
throws the annotation type not applicable to this kind of declaration error.

Version
Provide a version of library on which the problem occurred.

Android Studio 3.6.3
classpath("com.apollographql.apollo:apollo-gradle-plugin:1.3.3")
implementation("com.apollographql.apollo:apollo-runtime:1.3.3")

Let me know if y'all need additional information. Ty in advanced!

compiler Bug

All 11 comments

Can you try with version2.0.1 to make sure it's not already fixed in the latest version?

It looks like there might be a name clash between the Login input and potentially another Login class in the same file or package. Are there other Login classes in your project?

I have upgraded to 2.0.1
I have deleted the generated files and initiated a shift+shift search for files matching Login.
The result is attached as an image.
login_error
This was before I ran ./gradlew generateApolloSources to make sure there were no other Login classes. However, the same errors with Login persist post generating the Apollo sources.

So I just went ahead and renamed the input to UserLogin, downloaded the new schema, and generated apollo sources, and everything seems to be working as expected.

Thanks for the assistance!

I don't know if I should close since it's not like this bug is fixed. Maybe it's common sense not to name the input and mutation the same? I'll leave that up to y'all!

Thanks for the follow up. That does sound weird indeed. Let's keep this open for the time being.

It turns out a field of the mutation is called login and that will generate an inner class named Login:

mutation LoginBarber($input: Login!) {
    login(input: $input) { // This here will create a `Login` inner class that will clash with the input type
        response
        error
    }
}

I tried to reproduce by tweaking the schema but somehow it generates working code:

https://github.com/martinbonnin/apollo-android/blob/bug-2246-annotation-in-package-name/apollo-compiler/src/test/graphql/com/snipsnap/android/barbershop/LoginBarberMutation.java#L61

Was there an additional mistake on my side then?
I was under the impression that the input type should not be named the same as the field. But, from your code, it looks like that will still generate working code.

Javapoet is smart enough to use qualified names to avoid name clashes so it "should" work. But that space before the annotation looks a bit suspicious.

The same bug happened with me when I added "androidx.navigation:navigation-safe-args-gradle-plugin" classpath to the project gradle file

@LobnaMazhar good catch! Looks like it happens with JavaPoet 1.12.1 (apollo-android depends on JavaPoet 1.9.0). I'll investigate a bit more.

Hello :) Just ran into this while adding the "androidx.navigation:navigation-safe-args-gradle-plugin" classpath to my project. Do you know if there's any way to work around it?

Thanks!

@rcosteira79 switching to Kotlin models (generateKotlinModels.set(true)) will certainly be the easiest workaround.

Was this page helpful?
0 / 5 - 0 ratings