首页 > 解决方案 > 当 Android 应用通过 Apollo 客户端发送请求时,GraphQL 服务器返回 422 错误

问题描述

嗨,我一直在尝试设置一个简单的 Android 应用程序来向我通过 Springboot 在本地主机上设置的 GraphQL 服务器发送查询。如果我不使用该应用程序发送请求,无论是通过 GraphiQL 还是 Postman,一切都很好,我绝对没有问题。只有当我通过 Apollo 客户端在应用程序中发送请求时,才会收到 422 错误。

我已经在托管 GraphQL 服务器的 Springboot 中设置了日志语句来记录有效负载。

这是 Springboot 中定义的模式。

type Query {
  bookById(id: ID): Book 
}

type Book {
  id: ID
  name: String
  pageCount: Int
  author: Author
}

type Author {
  id: ID
  firstName: String
  lastName: String
}

这是 AndroidStudio 中为 Apollo Client 定义的查询。

query BookById($id : ID) {
    bookById(id : $id){
      name
      author{
        firstName
        lastName
      }
}}

这是 Android Studio 中的代码。

public class MainActivity extends AppCompatActivity {
    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();


        // To make requests to our GraphQL API we must use an instance 
        // of a query or mutation class generated by Apollo.
        Input<String> id = Input.fromNullable("book-1");
        BookByIdQuery bq = BookByIdQuery.builder()
                                        .id("book-1")
                                        .build();

        queryButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d(TAG, "REGISTERED CLICK");
                Log.d(TAG, "Book Query: " + bq.queryDocument());

                apolloClient.query(bq).enqueue(new 
                ApolloCall.Callback<BookByIdQuery.Data>() {

                    @Override
                    public void onResponse(@NotNull com.apollographql.apollo.api.Response<BookByIdQuery.Data> dataResponse){
                        Log.d(TAG, "Got Response: \n" + dataResponse.toString());
                        // changing UI must be on UI thread
                       textBox.setText(dataResponse.data().toString());
                    }

                    @Override
                    public void onFailure(@NotNull ApolloException a){
                        Log.d(TAG, "Failed, ApolloException " + a);                  
                    }
                });  // end apolloClient query
            }
        });
    }

如果通过 GraphiQL 或 Postman 发出请求,这是服务器端的日志。

DEBUG 13749 --- [nio-8080-exec-8] o.s.w.f.CommonsRequestLoggingFilter      : Before request [uri=/graphql]

这是应用程序中来自 Apollo 的请求时的日志。

DEBUG 13749 --- [io-8080-exec-10] o.s.w.f.CommonsRequestLoggingFilter      : Before request [uri=/graphql]

DEBUG 13749 --- [io-8080-exec-10] o.s.w.f.CommonsRequestLoggingFilter      : REQUEST DATA : uri=/graphql;payload={"operationName":"BookById","variables":{"id":"book-1"},"query":"query BookById($id: ID) {  bookById(id: $id) {    __typename    name    author {      __typename      firstName      lastName    }  }}"}]

这个问题是否与 Apollo 添加的 __typename 属性有关?或者可能是因为“id”被定义为服务器端架构中的一个简单参数,但它是查询的 Android Studio 定义中的一个变量?

我只是希望收到没有问题的回复,因为它似乎以其他方式工作。即使我在我的网络浏览器中手动输入查询,我也没有问题得到正确的响应,只有在 G̶r̶a̶p̶h̶Q̶L̶ Android Studio 中使用 Apollo 客户端时才会弹出此问题,我完全不知道为什么。我感谢人们可以提供的任何帮助。提前致谢。

更新:所以环顾四周,当通过 ApolloClient 发送查询时,它将查询作为对象而不是 JSON 字符串发送。现在我认为这可能是 422 错误的原因。我还读到允许我的 GraphQL 服务器接受除 JSON 字符串之外的对象是我必须启用的。不过,我不太确定该怎么做,那么有 Spring Boot 经验的人对我该如何做有什么建议吗?谢谢。

标签: androidspring-bootgraphqlapollographql-java

解决方案


推荐阅读