Search in sources :

Example 1 with SimpleGraphQLRequest

use of com.amplifyframework.api.graphql.SimpleGraphQLRequest in project amplify-android by aws-amplify.

the class SubscriptionEndpointTest method subscribeToEventComments.

/**
 * Subscribe to comments on an event.
 * @param eventId ID of event for which comments are being made
 * @return Subscription ID received from subscription_ack message payload
 * @throws ApiException If outcome of subscription request is anything other than an ACK w/ new ID
 */
private String subscribeToEventComments(String eventId) throws ApiException {
    // Arrange a request to start a subscription.
    String document = Assets.readAsString("subscribe-event-comments.graphql");
    GsonVariablesSerializer serializer = new GsonVariablesSerializer();
    Map<String, Object> variables = Collections.singletonMap("eventId", eventId);
    GraphQLRequest<String> request = new SimpleGraphQLRequest<>(document, variables, String.class, serializer);
    return Await.<String, ApiException>result((onResult, onError) -> executor.execute(() -> subscriptionEndpoint.requestSubscription(request, onResult, item -> {
        final String message;
        if (item.hasErrors()) {
            message = "Subscription error: " + item.getErrors().toString();
        } else {
            message = "Unexpected subscription data: " + item.getData();
        }
        ApiException apiException = new ApiException(message, "Not expected.");
        onError.accept(apiException);
    }, error -> onError.accept(new ApiException("Subscription failed.", error, "Not expected.")), () -> onError.accept(new ApiException("Subscription completed too soon.", "Not expected.")))));
}
Also used : SimpleGraphQLRequest(com.amplifyframework.api.graphql.SimpleGraphQLRequest) JSONObject(org.json.JSONObject) RandomString(com.amplifyframework.testutils.random.RandomString) ApiException(com.amplifyframework.api.ApiException)

Aggregations

ApiException (com.amplifyframework.api.ApiException)1 SimpleGraphQLRequest (com.amplifyframework.api.graphql.SimpleGraphQLRequest)1 RandomString (com.amplifyframework.testutils.random.RandomString)1 JSONObject (org.json.JSONObject)1