首页 > 解决方案 > AWS Appsync - 无法解析订阅 - _version 的预期非空值

问题描述

我有一个 Todo AppSync Android 应用程序,这些应用程序作为依赖项

implementation 'com.amazonaws:aws-android-sdk-appsync:2.9.+'
implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.0'
implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1'

这是我的架构

type Task @model {
  id: ID!
  title: String!
  description: String
  status: String
}
type Note @model {
  id: ID!
  content: String!
}

到目前为止,我能够列出和创建任务。但是当我尝试订阅 OnCreateTask 时,我收到一个异常,说它无法解析对象。

这是我的代码


    private fun subscribeOnCreate() {
        val subscription: OnCreateTaskSubscription = OnCreateTaskSubscription.builder().build()
        onCreateSubscriptionWatcher = mAWSAppSyncClient.subscribe(subscription)
        onCreateSubscriptionWatcher.execute(onCreateSubscriptionCallback)
    }

    val onCreateSubscriptionCallback: AppSyncSubscriptionCall.Callback<OnCreateTaskSubscription.Data> =
        object : AppSyncSubscriptionCall.Callback<OnCreateTaskSubscription.Data> {
            override fun onResponse(response: Response<OnCreateTaskSubscription.Data?>) {
                Log.i("Subscription", response.data().toString())
                query()
            }

            override fun onFailure(@Nonnull e: ApolloException) {
                Log.e("Subscription", e.toString())
            }

            override fun onCompleted() {
                Log.i("Subscription", "Subscription completed")
            }
        }

这是我的堆栈跟踪:

2020-05-21 10:13:18.773 16259-16472/com.wwm.todo E/SubscriptionObject: Failed to parse: {"data":{"onCreateTask":{"id":"1cbcfdd9-c124-4fca-8544-6f983757bc13","title":"math","__typename":"Task"}}}
    java.lang.NullPointerException: corrupted response reader, expected non null value for _version
        at com.apollographql.apollo.internal.response.RealResponseReader.checkValue(RealResponseReader.java:264)
        at com.apollographql.apollo.internal.response.RealResponseReader.readInt(RealResponseReader.java:67)
        at com.amazonaws.amplify.generated.graphql.OnCreateTaskSubscription$OnCreateTask$Mapper.map(OnCreateTaskSubscription.java:340)
        at com.amazonaws.amplify.generated.graphql.OnCreateTaskSubscription$Data$Mapper$1.read(OnCreateTaskSubscription.java:167)

有谁知道为什么 AppSync 没有返回 _version 值?

谢谢

标签: androidaws-appsync

解决方案


我发现了问题,这是因为当我在 AWS 控制台上创建任务时,我没有将 _version 标识为返回属性。这是我以前的

mutation create {
  createTask(input: {
    title:"sync, plkease work"
    description:"pm-v"}) { 
    id
    title
  }
}

现在改为

mutation create {
  createTask(input: {
    title:"sync, plkease work"
    description:"pm-v"}) { 
    id
    title
    description
    _version
    _lastChangedAt
  }
}

推荐阅读