简体   繁体   English

Apollo 客户端无法连接到 GraphQL 服务器(http 调用无法执行)

[英]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.我正在尝试设置一个简单的 Android 应用程序,该应用程序通过使用 Java GraphQL 的 Springboot 应用程序连接到我本地主机上设置的 GraphQL 服务器。 Using GraphiQL I can queries just fine.使用 GraphiQL 我可以很好地查询。 The issue is when I try to make the same queries in my app using the Apollo Client I get the following error问题是当我尝试使用 Apollo Client 在我的应用程序中进行相同的查询时,我收到以下错误

"com.apollographql.apollo.exception.ApolloNetworkException: Failed to execute http call" “com.apollographql.apollo.exception.ApolloNetworkException:无法执行http调用”

I used this tutorial to set up the GraphQL server via Springboot and define the GraphQL schema.我使用本教程通过 Springboot 设置 GraphQL 服务器并定义 GraphQL 模式。 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" “出现意外错误(类型=错误请求,状态=400)。所需的字符串参数‘查询’不存在”

So I tried Postman and sent the same query that worked in GraphiQL to the localhost and got an error back.所以我尝试了 Postman 并将在 GraphiQL 中工作的相同查询发送到本地主机并得到一个错误返回。 This was done using a GET request so I tried a POST instead and it worked just fine like in GraphiQL.这是使用 GET 请求完成的,所以我尝试了 POST 并且它像在 GraphiQL 中一样工作得很好。 So I think for some reason the GraphQL server I've set up via Springboot doesn't work with GET requests.所以我认为由于某种原因,我通过 Springboot 设置的 GraphQL 服务器不能处理 GET 请求。 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.我不知道这是否是因为我在设置时搞砸了一些东西,或者这就是 GraphQL 处理查询的方式,但如果有人能就此启发我,我将不胜感激。

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.无论如何,我设置了一个断点并逐步执行应用程序,发现 Apollo 也通过 GET 请求发送查询,我假设这就是这个问题的根源。 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.我犯了一个错误并误读了代码,我了解到它绝对不是发送 GET 请求,但问题仍然存在,我不知道为什么。

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.这是 graphQL 查询文件。

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?有没有办法使用 POST 而不是 GET 请求通过 Apollo Client 发送我的查询? Or is it better, and possible, to have the GraphQL server accept a GET request?或者让 GraphQL 服务器接受 GET 请求是否更好,并且可能?

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.我只是很困惑为什么 ApolloClient 总是无法进行 http 调用,不幸的是该错误不是很具有描述性,所以我有点困惑。

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.老实说,我认为 GET 请求会工作得很好,甚至是首选,因为我只是请求返回数据而不是尝试添加任何内容。 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? e:讨论一下,问题可能是我尝试使用 http 而不是 https 连接吗?

First, make sure you add INTERNET permission to AndroidManifext.xml , above the <application block首先,确保您将INTERNET权限添加到AndroidManifext.xml ,在<application块上方

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

If that doesn't fix it, try Build -> Clean Project .如果这不能解决它,请尝试Build -> Clean Project

If that still doesn't work, uninstall app on device and then reinstall如果仍然不起作用,请卸载设备上的应用程序,然后重新安装

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.这对其他人来说可能很明显,但显然您需要确保运行应用程序的设备连接到托管 GraphQL 服务器的同一网络。 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.如果它们不在同一个网络上,那么 GraphQL 服务器甚至永远不会收到查询,并且应用程序会发送上述描述性错误。

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.现在我遇到了 422 错误,这是我正在尝试解决的另一个问题。

use commands of aws from terminal从终端使用 aws 命令

amplify env pull --restore

and then进而

amplify push // this command plays the role to remove Network exception

First, start your localhost server through terminal window by using command below.首先,使用以下命令通过终端窗口启动本地主机服务器。 -> node index.js -> 节点 index.js

Then run your app.然后运行您的应用程序。

由于您使用了 http 调用,因此您必须在清单中添加android:usesCleartextTraffic="true"并检查 Internet 权限

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM