【问题标题】:Generated GraphQL Class implementing Query interface instead of Subscription interface生成的 GraphQL 类实现 Query 接口而不是 Subscription 接口
【发布时间】:2018-09-04 12:15:36
【问题描述】:

我正在为我的项目使用 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 接口

【问题讨论】:

    标签: java android graphql apollo aws-appsync


    【解决方案1】:

    您是否还在 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

    已编辑

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

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

    【讨论】:

    • 是的。检查此类型订阅 { subscribeToEventComments(eventId: String!): Comment @aws_subscribe(mutations: ["commentOnEvent"]) onEventCreated: Event @aws_subscribe(mutations: ["createEvent"]) }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-20
    • 1970-01-01
    • 2013-09-30
    • 1970-01-01
    • 1970-01-01
    • 2022-01-06
    • 2020-01-03
    相关资源
    最近更新 更多