Apollo-android: 0.1.0: Getting "Must provide query string." Response From Server

Created on 14 Feb 2017  路  15Comments  路  Source: apollographql/apollo-android

Given this GraphQL document:

query getAllTrips {
  allTrips {
    id
    title
    startTime
    duration
    creationTime
  }
}

...and this Retrofit interface:

interface ServerApi {
  @POST("graphql")
  Call<Response<GetAllTrips.Data>> _allTrips(@Body GetAllTrips query);
}

...and this Java code to execute the query in the document:

        Retrofit retrofit=new Retrofit.Builder()
          .baseUrl("https://graphql-demo.commonsware.com")
          .addConverterFactory(new ApolloConverterFactory.Builder().build())
          .addConverterFactory(MoshiConverterFactory.create())
          .build();
        ServerApi api=retrofit.create(ServerApi.class);

        try {
          retrofit2.Response<Response<GetAllTrips.Data>> response=
            api._allTrips(new GetAllTrips()).execute();
          Log.e("20170214", response.errorBody().string());
        }
        catch (IOException e) {
          Log.e("20170214", "Exception making query", e);
        }

...I get a 400 response from my server, and the error body of the response is:

{"errors":[{"message":"Must provide query string."}]}

This feels like a bug, insofar as providing the query string is what the generated code is supposed to be doing, if I understand how this is all supposed to work.

The attached ZIP archive contains a complete sample project for reproducing this.

If I screwed up somewhere, I apologize.
StaticList.zip

Most helpful comment

No worries. My job is to wield the machete, blazing a trail through the underbrush for all the Android developers that follow. Besides, machetes are cool. :-)

All 15 comments

looking. I think its a formatting issue.

It's something with the project setup. let me find the right dependency version from sample. When I moved your query/schema into the sample project it worked correctly.

@commonsguy could please catch the http logs for your request, to see what http body has been sent to server?

problem was wrong version of plugin...I'm confirming and then reuploading

please correct to the following, theres some added fluff in there that can be removed once it is working:
external build.gradle

ext {
    kotlinVersion = '1.0.6'

    compileSdkVersion = 25
    buildToolsVersion = '25.0.2'
    apolloVersion = '0.2.1-SNAPSHOT'

    dep = [
            androidPlugin: 'com.android.tools.build:gradle:2.3.0-beta3',
            apolloPlugin: "com.apollographql.android:gradle-plugin:$apolloVersion",
            apolloConverter: "com.apollographql.android:converter:$apolloVersion",
            supportAnnotations: 'com.android.support:support-annotations:25.1.0',
            compiletesting: 'com.google.testing.compile:compile-testing:0.10',
            javaPoet: 'com.squareup:javapoet:1.8.0',
            moshi: 'com.squareup.moshi:moshi:1.3.1',
            rxjava: 'io.reactivex:rxjava:1.2.3',
            rxjava2: 'io.reactivex.rxjava2:rxjava:2.0.5',
            rxandroid: 'io.reactivex.rxjava2:rxandroid:2.0.1',
            jsr305: 'com.google.code.findbugs:jsr305:3.0.1',
            jsr205: 'javax.annotation:jsr250-api:1.0',
            pluralizer: 'com.github.cesarferreira:kotlin-pluralizer:0.2.9',
            appcompat: 'com.android.support:appcompat-v7:25.0.1',
            retrofit: 'com.squareup.retrofit2:retrofit:2.1.0',
            retrofitMoshiConverter: 'com.squareup.retrofit2:converter-moshi:2.1.0',
            retrofitRx2Adapter: 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0',
            kotlinStdLib: "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion",
            kotlinReflect: "org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion",
            kotlinGradlePlugin: "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion",
            gradleNodePlugin: "com.moowork.gradle:gradle-node-plugin:1.0.0",
            junit: 'junit:junit:4.12',
            spock: 'org.spockframework:spock-core:0.7-groovy-2.0',
            truth: 'com.google.truth:truth:0.30',
            mockWebServer: 'com.squareup.okhttp3:mockwebserver:3.5.0',
            okhttpLoggingInterceptor: 'com.squareup.okhttp3:logging-interceptor:3.5.0'
    ]

    isCi = "true" == System.getenv('CI')
}

subprojects {
    buildscript {
        repositories {
            jcenter()
            maven { url "https://jitpack.io" }
            maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
        }

        dependencies {
            classpath dep.kotlinGradlePlugin
        }
    }
    repositories {
        jcenter()
        maven { url "https://jitpack.io" }
        maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
    }
}

app/build.gradle

buildscript {
    dependencies {
        classpath dep.androidPlugin
        classpath dep.apolloPlugin
    }
}

apply plugin: 'com.android.application'
apply plugin: 'com.apollographql.android'

dependencies {
  compile 'com.apollographql.android:api:0.1.0'
  compile 'com.squareup.retrofit2:retrofit:2.1.0'
  compile 'com.apollographql.android:converter-pojo:0.1.0'
  compile 'com.squareup.retrofit2:converter-moshi:2.1.0'
}

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"

      defaultConfig {
        minSdkVersion 15
            targetSdkVersion 25
        applicationId "com.commonsware.graphql.trips.simple.staticlist"

        jackOptions {
            enabled true
        }
     }

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

apollo {
  generateClasses = true
}

Please let me know if still having issue

@commonsguy and couple notes:

  1. you don't need .addConverterFactory(MoshiConverterFactory.create())
  2. for ApolloConverterFactory you need to register mapper:
.withResponseFieldMapper(GetAllTrips.Data.class, new GetAllTrips.Data.Mapper(GetAllTrips.Data.FACTORY))

cleaned it up a bit this is the actual required build.gradle files

top level build.gradle:

ext {
    kotlinVersion = '1.0.6'

    compileSdkVersion = 25
    buildToolsVersion = '25.0.2'
    apolloVersion = '0.2.1-SNAPSHOT'

    dep = [
            androidPlugin: 'com.android.tools.build:gradle:2.3.0-beta3',
            apolloPlugin: "com.apollographql.android:gradle-plugin:$apolloVersion",
            apolloConverter: "com.apollographql.android:converter:$apolloVersion",
            retrofit: 'com.squareup.retrofit2:retrofit:2.1.0',
            kotlinGradlePlugin: "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion",
    ]
}

subprojects {
    buildscript {
        repositories {
            jcenter()
            maven { url "https://jitpack.io" }
            maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
        }

        dependencies {
            classpath dep.kotlinGradlePlugin
        }
    }
    repositories {
        jcenter()
        maven { url "https://jitpack.io" }
        maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
    }
}

app/build.gradle

buildscript {
    dependencies {
        classpath dep.androidPlugin
        classpath dep.apolloPlugin
    }
}

apply plugin: 'com.android.application'
apply plugin: 'com.apollographql.android'

dependencies {
  compile 'com.apollographql.android:api:0.1.0'
  compile 'com.squareup.retrofit2:retrofit:2.1.0'
  compile 'com.apollographql.android:converter-pojo:0.1.0'
}

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"

      defaultConfig {
        minSdkVersion 15
            targetSdkVersion 25
        applicationId "com.commonsware.graphql.trips.simple.staticlist"

        jackOptions {
            enabled true
        }
     }

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

apollo {
  generateClasses = true
}

@digitalbuddha Your original suggested Gradle build files resulted in the same error as the one that I reported. Your revised suggested Gradle build files removed compile 'com.squareup.retrofit2:converter-moshi:2.1.0', which means my current code will not build.

@sav007 While your sample code shows the use of withResponseFieldMapper(), that method does not seem to exist on ApolloConverterFactory.Builder, at least in the 0.2.1-SNAPSHOT edition of your artifacts. Leastways, Android Studio claims that it does not exist. Hence, I am at a loss as to how to apply that suggestion.

@digitalbuddha Given the aforementioned withResponseFieldMapper() problem, I had to add back in the Moshi stuff, otherwise Retrofit craps out before I can even attempt to execute the request. With that and the MoshiConverterFactory back in, I am back to the same error as before. Attached is an updated project showing these changes.
StaticList.zip

Could you try my version. Sorry not sure what else I changed but currently the version included does work.. StaticList.zip

Your version crashes, because there is no converter:

Process: com.commonsware.graphql.trips.simple, PID: 14180
java.lang.IllegalArgumentException: Unable to create @Body converter for class com.commonsware.graphql.trips.api.GetAllTrips (parameter #1)
    for method ServerApi._allTrips
    at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:720)
    at retrofit2.ServiceMethod$Builder.parameterError(ServiceMethod.java:725)
    at retrofit2.ServiceMethod$Builder.parseParameterAnnotation(ServiceMethod.java:681)
    at retrofit2.ServiceMethod$Builder.parseParameter(ServiceMethod.java:333)
    at retrofit2.ServiceMethod$Builder.build(ServiceMethod.java:202)
    at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:166)
    at retrofit2.Retrofit$1.invoke(Retrofit.java:145)
    at java.lang.reflect.Proxy.invoke(Proxy.java:393)
    at com.commonsware.graphql.trips.simple.$Proxy0._allTrips(Unknown Source)
    at com.commonsware.graphql.trips.simple.MainActivity$1.run(MainActivity.java:45)
 Caused by: java.lang.IllegalArgumentException: Could not locate RequestBody converter for class com.commonsware.graphql.trips.api.GetAllTrips.
  Tried:
   * retrofit2.BuiltInConverters
   * com.apollographql.android.converter.pojo.ApolloConverterFactory
    at retrofit2.Retrofit.nextRequestBodyConverter(Retrofit.java:298)
    at retrofit2.Retrofit.requestBodyConverter(Retrofit.java:258)
    at retrofit2.ServiceMethod$Builder.parseParameterAnnotation(ServiceMethod.java:678)
      ... 7 more

wrong versions again blah. Working on it. Thanks for being first victim :-)

Thanks for reporting issue. I wasn't able to fix it today but will continue working on it tomorrow. It has to do with our artifact deployment. Thanks for being first to try and raise issue.

No worries. My job is to wield the machete, blazing a trail through the underbrush for all the Android developers that follow. Besides, machetes are cool. :-)

OK I got it, needed a bit of massaging. We will update docs but you are cooking with gas :-)

StaticList.zip

Agreed, this project works. I'll work on blending your changes back into my main projects and try getting those going. Many thanks!

Was this page helpful?
0 / 5 - 0 ratings