4

我正在为我的项目使用 AWS AppSync。当使用突变将数据推送到服务器时,它工作正常。但我有订阅问题。

OnEventCreated onEventCreated = OnEventCreated.builder().build();
        subscriptionWatcher =  ClientFactory.getInstance(this).subscribe(onEventCreated); // giving error

subscribe函数接受实现订阅的输入。但是当我构建我的项目时,生成的代码实现了查询。

生成的类

@Generated("Apollo GraphQL")
public final class OnEventCreated implements Query<OnEventCreated.Data, OnEventCreated.Data, Operation.Variables> {
  public static final String OPERATION_DEFINITION = "subscription OnEventCreated {\n"
      + "  onEventCreated {\n"
      + "    __typename\n"
      + "    id\n"
      + "    description\n"
      + "    name\n"
      + "    when\n"
      + "    where\n"
      + "  }\n"
      + "}";

  public static final String QUERY_DOCUMENT = OPERATION_DEFINITION;
}...

GraphQL 文件中订阅的具体代码是..

subscription OnEventCreated {
    onEventCreated {
      id
      description
      name
      when
      where
    }
} ...

Schema.json 文件

type Subscription {
    subscribeToEventComments(eventId: String!): Comment
        @aws_subscribe(mutations: ["commentOnEvent"])
    onEventCreated: Event
        @aws_subscribe(mutations: ["createEvent"])
}...

构建文件包含...

compile 'com.amazonaws:aws-android-sdk-appsync:2.6.16'
    compile 'com.amazonaws:aws-android-sdk-appsync-compiler:2.6.16'
    compile 'com.amazonaws:aws-android-sdk-cognitoidentityprovider:2.6.16'
    compile 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.0'
    compile 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1'

我能做些什么呢。当我构建时,生成的 OnEventCreated 类实现了 Subscription 接口而不是 Query 接口

4

1 回答 1

0

您是否还在@aws_subscribeGraphQL 架构上设置了指令以在订阅时触发突变onEventCreated?例如,它可能看起来像这样:

type Subscription {
    onEventCreated: Event
    @aws_subscribe(mutations: ["createEvent"])
}

然后,当createEvent成功调用突变时,它将触发订阅。更多数据可以在这里找到:https ://docs.aws.amazon.com/appsync/latest/devguide/real-time-data.html

已编辑

再次阅读上述信息后(由于自动换行而遗漏了一些信息),我看到您遇到的错误是:

OnEventCreated onEventCreated = OnEventCreated.builder().build();
subscriptionWatcher = ClientFactory.getInstance(this).subscribe(onEventCreated);

你需要通过你onEventCreated喜欢.createInstance()这样:

subscriptionWatcher = ClientFactory.createInstance(this).subscribe(subscription);

此时,您可以在收到如下响应时对数据采取行动:

    subscriptionWatcher.execute(new AppSyncSubscriptionCall.Callback() {
        @Override
        public void onResponse(@Nonnull Response response) {
            Log.d("RESPONSE", response.data().toString());
        }

        @Override
        public void onFailure(@Nonnull ApolloException e) {
            Log.d("ERROR", e.toString());
        }

        @Override
        public void onCompleted() {
            Log.d("COMPLETE", "COMPLETED SUBSCRIPTION");
        }
    });
于 2018-03-26T23:34:19.623 回答