0

当通过 apollo-graphql 提交到 express-graphql webapp 时,我在让最基本的 graphQL 工作时遇到问题。通过 graphiql 的网络请求,一切正常。一旦我尝试使用 apollo-android 客户端,无论我尝试什么,我都会收到 400 个错误。

我的android依赖是:

  • com.apollographql.apollo:apollo-runtime:2.3.1

我的节点依赖项:

  • 快递:4.17.1
  • express-graphql:^0.11.0
  • graphql:^15.3.0

我的基本服务器是:

const express = require('express')
const { graphqlHTTP } = require('express-graphql')

const {
    GraphQLSchema,
    GraphQLObjectType,
    GraphQLString,
    GraphQLList,
    GraphQLInt,
    GraphQLNonNull

} = require ('graphql')

const RootQueryType = new GraphQLObjectType({
    name: 'Query',
    description: 'Root query',
    fields: () => ({
        hello: {
            type: GraphQLString,
            resolve() {
              return 'world'
            }
        },
    })
})

const schema = new GraphQLSchema({
    query: RootQueryType
})

const app = express()

app.use('/graphql', function (req, res, next) {
    console.log("We got a request!: " + req.ip)
    next();
},
graphqlHTTP ({
    schema: schema,
    graphiql: true
}))
app.listen(5000, () => console.log('server running'))

通过 graphiQL 提交请求,我只是请求“{hello}”并获得成功的响应:

{
  "data": {
    "hello": "world"
  }
}

我使用 downloadApolloSchema 任务生成了我的 android 模式文件,我的 graphiql 内容很简单:

query hello() {
     hello()
 }

我的主要活动是 onCreate():

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ApolloClient client = ApolloClient.builder().serverUrl("http://10.0.2.2:5000/graphql").build();

        client.query(
                HelloQuery.builder().build()
        ).enqueue(
                new ApolloCall.Callback<HelloQuery.Data>() {
                    @Override
                    public void onResponse(@NotNull Response<HelloQuery.Data> response) {
                        Log.d("TEST", "Hit success: " + response.toString());

                    }

                    @Override
                    public void onFailure(@NotNull ApolloException e) {
                        Log.e("TEST", "Hit on failure", e);

                    }

                    @Override
                    public void onHttpError(@NotNull ApolloHttpException e) {
                        super.onHttpError(e);
                        Log.e("TEST", "Hit on failure: " + e.message() + e.rawResponse());

                    }
                }
        );
    }

无论我尝试什么,无论我如何尝试从服务器或 Android 应用程序获取更多详细信息,我能获得的信息量最大的堆栈跟踪是:

2020-09-20 12:50:00.428 16056-16101/com.rubyengineering.graphqltutorial E/TEST: Hit on failure
    com.apollographql.apollo.exception.ApolloHttpException: HTTP 400 Bad Request
        at com.apollographql.apollo.internal.interceptor.ApolloParseInterceptor.parse(ApolloParseInterceptor.java:108)
        at com.apollographql.apollo.internal.interceptor.ApolloParseInterceptor$1.onResponse(ApolloParseInterceptor.java:53)
        at com.apollographql.apollo.internal.interceptor.ApolloServerInterceptor$2.onResponse(ApolloServerInterceptor.java:146)
        at okhttp3.RealCall$AsyncCall.execute(RealCall.java:203)
        at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
        at java.lang.Thread.run(Thread.java:764)
2020-09-20 12:50:00.429 16056-16101/com.rubyengineering.graphqltutorial E/TEST: Hit on failure: Bad RequestResponse{protocol=http/1.1, code=400, message=Bad Request, url=http://10.0.2.2:5000/graphql}

这让我发疯,因为我似乎无法更深入地挖掘 400 错误,也不知道为什么这这么难。我已经通过模拟器以及实际设备尝试过这个请求。结果并没有明显改变,因为它是服务器抛出错误。希望这是一个简单的错误!

4

1 回答 1

0

所以经过数小时的挖掘,试图让wireshark阅读完整的请求/响应等。我终于让它工作了。原来我的 .graphql 定义文件被命名为“Hello.graphql”。如前所述,取自其他示例的内容为:

query hello() {
     hello()
 }

我尝试删除以删除多余的括号无济于事,我还尝试更改大写字母无济于事。我错过了最终修复的一件事是删除括号并更改大写...最终的工作定义文件内容:

query Hello {
     hello
 }
于 2020-09-21T06:01:50.387 回答