Search code examples
javaandroidgraphqlexpress-graphqlapollo-android

apollo-android does not provide variables in request


I used apollo-server-express to set up a GraphQL server with the following schema:

input IGetUser {
    email: String
    ID: String
    UserID: Int!
}
interface Entity {
    createdAt: String
    updatedAt: String
}
schema {
    query: Query
}
type Query {
    getUser(params:IGetUser): User!
}
type User implements Entity {
    UserID: Int!
    firstName: String!
    lastName: String
    email: String!
    ID: String
    ...
    confirmed: Boolean!
    createdAt: String!
    updatedAt: String!
}

Basically the User type represents a table in my MySQL database. The server is fully operational. Here is my query:

query GetUserQuery($UserID: Int!, $email: String, $ID: String) {
    getUser(params:{ email: $email, ID: $ID, UserID: $UserID }) {
        ...UserDetails
        ...EntityDetails
    }
}
fragment EntityDetails on Entity {
    createdAt
    updatedAt
}
fragment UserDetails on User {
    UserID
    firstName
    lastName
    email
    ID
    ...
    confirmed
}

Note that I have shortened the User type and the UserDetails fragment.

The query is located in my Android Studio Project and the apollo-android plugin generates the Java objects correctly. What I am trying is to fetch a User with the ID 1. My Java code looks like the following:

RestClient.getInstance(getApplicationContext())
    .getApolloClient(getString(R.string.urlGraphQLEndpoint))
    .query(
        GetUserQuery.builder()
            .userID(userID) // userID = 1 is a local variable
            .build()
    )
    .httpCachePolicy(HttpCachePolicy.NETWORK_ONLY)
    .enqueue(new ApolloCall.Callback<GetUserQuery.Data>() {
        @Override
        public void onFailure(@Nonnull ApolloException e) {
            Log.e(TAG, e.getMessage(), e);
        }

        @Override
        public void onResponse(@Nonnull Response<GetUserQuery.Data> response) {
            // Doesn't matter
        }
    })

The RestClient class is a Singleton that provides access to the ApolloClient instance and the resource string urlGraphQLEndpoint points to the IP address and port of my local GraphQL server: http://10.0.2.2:3000/graphql. When I try to execute my Android app I receive the following error message:

HTTP 400 Bad Request
com.apollographql.apollo.exception.ApolloHttpException: HTTP 400 Bad Request
    at com.apollographql.apollo.internal.interceptor.ApolloParseInterceptor.parse(ApolloParseInterceptor.java:105)
    at com.apollographql.apollo.internal.interceptor.ApolloParseInterceptor.access$100(ApolloParseInterceptor.java:28)
    at com.apollographql.apollo.internal.interceptor.ApolloParseInterceptor$1.onResponse(ApolloParseInterceptor.java:53)
    at com.apollographql.apollo.internal.interceptor.ApolloServerInterceptor$1$1.onResponse(ApolloServerInterceptor.java:93)
    at okhttp3.RealCall$AsyncCall.execute(RealCall.java:153)
    at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
    at java.util.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
    at java.lang.Thread.run(Thread.java:764)

On the server-side I receive the following console log:

GraphQLError: Variable "$UserID" of requiredtype "Int!" was not provided!
    at getVariableValues ...

When I try the exact same query, that should be used by the apollo-android client with GraphiQL everything is working as I am expecting and I receive the User with the ID 1. It seems like an internal mistake of the apollo-android lib for me, but because my Java code and my GraphQL query doesn't look much different to the apollo-android example Generate Code using Apollo and Consuming Code I don't really know where my mistake is.

Thanks in advance, I appreciate every help!


Solution

  • I found the mistake... As you can see in my query I'm using capital letters at the beginning of some of my variables. apollo-android is case-sensitive, like it should be, but it ignores capital letters at the beginning of variables and converts them to small letters. With the help of the Postman proxy I was able to inspect the network requests of my app and saw that the variables were still small letters.