0

I have an Android app that will be using data from GraphQL API via AWS Appsync. I know most resources point to Apollo as the client library to use, but due to our CICD, generating the models is not entirely possible, or I haven't found good examples/documentation on CICD/Apollo/Android. The work around was to create a regular POST request with Retrofit. No matter what I try I can't seem to get anything other than a 400 returned. I have successfully called the API with curl and NodeJS. I am fairly new to Android so any guidance is appreciated.

curl approach (success):

curl -XPOST -H "Content-Type:application/graphql" -H "x-api-key:[SOME_API_KEY]" -d '{ "query": "query { listData { dataName } }" }' https://amazon.endpoint.com/graphql

Node approach (success):

async function testApi() {
    const headers = {
        'x-api-key': 'SOME_API_KEY',
        'Content-Type': 'application/graphql'
    }

    const options = {
        headers
      };

    try {
        const b = await axios.post('https://amazon.endpoint.com/graphql',
        { "query": "query { listData { dataName } }" },options)
        console.log(b.data)
    } catch (error) {
        console.log(error)
    }
}

testApi();

Android approach (failure):

// class to create
public interface GraphqlService {
    @Headers({
            "Content-Type: application/graphql",
            "x-api-key: SOME_API_KEY"
    })
    @POST("/graphql")
    Call<String> getLockers(@Body String body);
}
// in main code
retrofit = new Retrofit.Builder()
        .baseUrl("https://amazon.endpoint.com")
        .addConverterFactory(GsonConverterFactory.create())
        .build();
GraphqlService service = retrofit.create(GraphqlService.class);

try {
    JSONObject paramObject = new JSONObject();
    String query = "query { listData { dataName } }";
    paramObject.put("query", query);
    service.getLockers(paramObject.toString())
            .enqueue(new Callback<String>() {

                @Override
                public void onResponse(Call<String> call, Response<String> response) {
                    System.out.println(response);
                }

                @Override
                public void onFailure(Call<String> call, Throwable t) {
                    System.out.println(t);
                }
            });
} catch (JSONException e) {
    e.printStackTrace();
}

I am thinking there is something funny at work with the way Gson converts the object? I have verified that the query is right via curl and Node. Once again I am new to Android development and would appreciate any thoughts on this.

Trayson Keli'i
  • 313
  • 2
  • 8

1 Answers1

0

I actually found the issue. It was with the use of the Gson objects. Instead of using String I should have been using JsonObject from (Gson). Since I was using the GsonConverterFactory there were weird values showing up when using the JSONObject creator. Hopefully this helps someone out. Also working with Gson, the default class that should be used when serializing objects is JsonObject.

Trayson Keli'i
  • 313
  • 2
  • 8