Apollo Client Can't Connect To GraphQL Server (http call fails to execute)

I’m trying to set up a simple Android app that connects to a GraphQL server set on my local host via a Springboot application using Java GraphQL. Using GraphiQL I can queries just fine. The issue is when I try to make the same queries in my app using the Apollo Client I get the following error

 

“com.apollographql.apollo.exception.ApolloNetworkException: Failed to execute http call”

 

I used this tutorial to set up the GraphQL server via Springboot and define the GraphQL schema. And I’ve relied on this tutorial to help set up the app.

Earlier I’ve tried to access the server through my web browser but I would get an error saying

 

“There was an unexpected error (type=Bad Request, status=400).
Required String parameter ‘query’ is not present”

 

So I tried Postman and sent the same query that worked in GraphiQL to the localhost and got an error back. This was done using a GET request so I tried a POST instead and it worked just fine like in GraphiQL. So I think for some reason the GraphQL server I’ve set up via Springboot doesn’t work with GET requests. I don’t know if this is because I messed something up when setting it up or if that’s just how GraphQL works with queries, but if anyone can enlighten me about this I’d appreciate it.

 

Regardless, I set up a breakpoint and stepped through the app and found out that Apollo sends the query through a GET request as well and I’m assuming that’s the source of this issue. I made a mistake and misread the code and I’ve learned it’s definitely not sending a GET request, but the problem still exists and I’m not sure why.

Here’s the main activity

private static final String BASE_URL = 
    "http://xxx.xxx.xxx.xxx:8080/graphql";
    private static final String TAG = "MainActivity";
    private TextView textBox;
    private Button queryButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textBox = (TextView) findViewById(R.id.plainbox);
        queryButton = (Button) findViewById(R.id.queryButton);


        OkHttpClient okHttpClient = new OkHttpClient.Builder().build();

        ApolloClient apolloClient = ApolloClient.builder()
                .serverUrl(BASE_URL)
                .okHttpClient(okHttpClient)
                .build();

        BookByIdQuery bookQuery = BookByIdQuery.builder()
                                                .id("book-1")
                                                .build();

 queryButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                apolloClient.query(bookQuery).enqueue(new ApolloCall.Callback<BookByIdQuery.Data>() {

                    @Override
                    public void onResponse(@NotNull Response<BookByIdQuery.Data> dataResponse){
                        Log.d(TAG, "Got Response: n" + dataResponse.toString());
                        textBox.setText(dataResponse.data().toString());
                    }

                    @Override
                    public void onFailure(@NotNull ApolloException a){
                        Log.d(TAG, "Failed, ApolloException " + a);
                    }
                });  // end apolloClient query

 

Here’s the graphQL query file.

query BookById($id : ID) {
    Book: bookById(id : $id){
      name
      author{
        firstName
        lastName
      }
}}

 

So at this point I’m at a bit of a loss. Is there a way to send my queries through the Apollo Client using a POST instead of a GET request? Or is it better, and possible, to have the GraphQL server accept a GET request?

I’m just confused as to why ApolloClient keeps failing to make the http call, and unfortunately the error isn’t very descriptive so I’m a bit stumped.

I honestly thought a GET request would work just fine and would even be preferred since I’m only requesting data back and not trying to add anything. I appreciate any help you guys can offer, and thanks in advance!

e: Discussing it some, could the issue be the fact that I’m trying to connect to with http instead of https?

2
Leave a Reply

avatar
2 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Jason
Guest

So a few days ago I made some progress and I’ve kept looking around and trying different things to make sure this wasn’t a fluke or anything. This might be obvious to others but apparently you need to make sure the device you’re running the app on is connected to the same network that’s hosting the GraphQL server. If they aren’t on the same network then the GraphQL server never even receives the query and the app sends the descriptive error described above. The odd thing is I would sometimes get that error again even when the device was connected… Read more »

Jason
Guest

First, make sure you add INTERNET permission to AndroidManifext.xml, above the <application block

<uses-permission android:name="android.permission.INTERNET"/>

If that doesn’t fix it, try Build -> Clean Project.

If that still doesn’t work, uninstall app on device and then reinstall