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

5

7

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?

Patriarchate answered 1/8, 2019 at 21:31 Comment(0)
E
2

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

Ensepulcher answered 16/10, 2019 at 18:14 Comment(1)
Deleting and reinstalling the app worked for me, Thank you so much Gibolt.Chlorate
P
0

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 to the same network. The only way to solve this was to disconnect my computer from the network, even having to go as far as to forget the network, and then reconnect. Doing this has worked for me so far.

Now I'm stuck with a 422 error which is a whole other issue now that I'm trying to solve.

Patriarchate answered 9/8, 2019 at 16:25 Comment(0)
R
0

use commands of aws from terminal

amplify env pull --restore

and then

amplify push // this command plays the role to remove Network exception
Rossman answered 23/6, 2020 at 22:18 Comment(0)
C
0

First, start your localhost server through terminal window by using command below. -> node index.js

Then run your app.

Cantor answered 15/9, 2020 at 20:32 Comment(0)
F
0

since you used http call, you have to add android:usesCleartextTraffic="true" to your manifest and check Internet permission

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

Filature answered 25/8, 2021 at 8:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.