apollo-android对express-graphql的请求始终导致http 400

问题描述

当通过apollo-graphql提交到express-graphql Webapp时,我在使graphQL的最基本功能发挥作用时遇到了问题。通过graphiql的Web请求,一切正常。尝试使用apollo-android客户端后,无论尝试如何,都会出现400个错误

我的android依赖项是:

  • com.apollographql.apollo:apollo运行时: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错误,也不知道为什么这么难。我已经通过仿真器以及实际设备尝试了此请求。由于服务器抛出错误,因此结果不会明显改变。希望这是一个简单的错误

解决方法

因此,经过数小时的挖掘,尝试让Wireshk读取完整的请求/响应等。我终于使它工作了。原来,我的.graphql定义文件名为“ Hello.graphql”。如上所述,取自其他示例的内容是:

query hello() {
     hello()
 }

我尝试删除以删除多余的括号,但无济于事,并且我还尝试将大写字母更改为无济于事。我错过的最后一件事就是解决办法,就是删除括号并更改大小写...最终的工作定义文件内容:

query Hello {
     hello
 }