4
votes

I am using AWS AppSync for my project. it is working fine when data is being pushed to the server using mutation. but i am having problem with subscription.

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

subscribe function takes input which implements Subscription. but when i build my project, the generated code implements Query.

Generated Class

@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;
}...

specific code for subscription in GraphQL file is ..

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

Schema.json file

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

Build files contains...

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'

what can i do for it. when i build, the generated OnEventCreated class implements Subscription interface instead of Query interface

1

1 Answers

0
votes

Have you also set the @aws_subscribe directive on your GraphQL schema to fire off a mutation when you subscribe to onEventCreated? For example it might look something like this:

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

Then when the createEvent mutation is called successfully it will fire off the subscription. More data can be found here: https://docs.aws.amazon.com/appsync/latest/devguide/real-time-data.html

Edited

After reading your information above again (some was missed due to wordwrap) I see the error you're getting is on:

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

You need to pass your onEventCreated to .createInstance() like so:

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

At that point you can act on the data when you get a response like this:

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