我正在为我的项目使用AWS AppSync。使用突变将数据推送到服务器时它工作正常。但我对订阅有疑问。
OnEventCreated onEventCreated = OnEventCreated.builder().build();
subscriptionWatcher = ClientFactory.getInstance(this).subscribe(onEventCreated); // giving error
subscribe
函数接受实现Subscription的输入。
但是当我构建我的项目时,生成的代码实现了Query。
生成的类
@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接口
答案 0 :(得分:0)
您是否还在GraphQL架构上设置了@aws_subscribe
指令,以便在订阅onEventCreated
时触发突变?例如,它可能看起来像这样:
type Subscription {
onEventCreated: Event
@aws_subscribe(mutations: ["createEvent"])
}
然后,当成功调用createEvent
突变时,它将触发订阅。可以在此处找到更多数据:https://docs.aws.amazon.com/appsync/latest/devguide/real-time-data.html
<强>被修改强>
再次阅读上面的信息后(由于wordwrap错过了一些信息),我看到你得到的错误是:
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");
}
});