Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 858
skippy130 Avatar asked Aug 01 '19 21:08

skippy130


People also ask

Which HTTP error code is used for auth errors in GraphQL?

A HTTP 403 status could be used to tell the user that he cannot access the gql api at all. We solve this issue in express/nestjs by having a middleware before hitting the graphql layer that enriches the request with the user (maybe undefined) or fails if the user cannot be authenticated.

Does GraphQL work over HTTP?

GraphQL is typically served over HTTP via a single endpoint which expresses the full set of capabilities of the service. This is in contrast to REST APIs which expose a suite of URLs each of which expose a single resource.

How do you handle errors in Apollo GraphQL?

By default, Apollo Client throws away partial data and populates the error. graphQLErrors array of your useQuery call (or whichever hook you're using). You can instead use these partial results by defining an error policy for your operation. If the response includes GraphQL errors, they are returned on error.

What HTTP method does GraphQL use?

You can also make GraphQL requests using the GET HTTP method. The rules for making GraphQL requests this way are almost the same as the POST version — there are just slight differences: Each property is provided as an HTTP query parameter (values separated by an ampersand — & )


1 Answers

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

like image 176
Gibolt Avatar answered Sep 30 '22 22:09

Gibolt