如何在Kotlin Andorid中读取Apollo Client响应/ GraphQL响应的响应数据

时间:2019-09-17 22:39:27

标签: android kotlin graphql apollo-client

我正在使用Kotlin开发一个Android应用程序。在我的应用程序中,我正在使用Apollo Client使用GraphQL API。我现在想做的是要检索响应的响应字段。

这是我的代码

protected fun _handleLoginButtonClick(view: View) {
        val apolloClient = ApolloClient.builder()
            .serverUrl("https://app.herokuapp.com/graphql")
            .okHttpClient(OkHttpClient())
            .build()
        val loginMutation = LoginMutation.builder()
            .identity(view.etf_email.text.toString())
            .password(view.etf_password.text.toString())
            .build()

        view.tv_login_error_message.text = "Started making request"
        apolloClient.mutate(loginMutation).enqueue(object: ApolloCall.Callback<LoginMutation.Data>() {
            override fun onFailure(e: ApolloException) {
                view.tv_login_error_message.text = e.message

            }

            override fun onResponse(response: Response<LoginMutation.Data>) {
                //here I dont know how to retrieve a field, accessToken
            }
        })
    }

正如您在onResponse回调中看到的注释一样,我无法弄清楚如何检索accessToken字段。我该如何找回它?

1 个答案:

答案 0 :(得分:0)

OnResponse 包含响应对象,它具有数据对象,您可以从中获取字段。

apolloClient.mutate(loginMutation).enqueue(object: ApolloCall.Callback() { 覆盖乐趣 onFailure(e: ApolloException) { view.tv_login_error_message.text = e.message

        }

        override fun onResponse(response: Response<LoginMutation.Data>) {
            //here you can use response to get your model data like accessToken
           response.data.(here you can get data from your model. eg accessToken)
        }
    })