【问题标题】:How to read the response data of Apollo Client response/ GraphQL response in Kotlin Andorid如何在 Kotlin Android 中读取 Apollo Client 响应/GraphQL 响应的响应数据
【发布时间】:2020-01-18 19:51:05
【问题描述】:

我正在使用 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 字段。如何找回?

【问题讨论】:

  • 您使用的是 Android Studio 吗?一旦您在response 之后键入句点,您应该有代码完成功能,它将建议可用的属性和方法。因为data 始终可以为空,所以您可能必须使用安全调用运算符(即键入?. 而不仅仅是.)来查看填充的选项。您也可以自己查看生成的LoginMutation.Data 类。

标签: android kotlin graphql apollo-client


【解决方案1】:

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)
        }
    })

【讨论】:

    猜你喜欢
    • 2019-07-26
    • 2018-07-17
    • 2019-03-10
    • 2022-12-02
    • 2018-02-25
    • 2019-07-26
    • 2019-04-21
    • 2020-04-01
    • 2021-09-28
    相关资源
    最近更新 更多