Search in sources :

Example 1 with Subscription

use of com.apollographql.apollo.api.Subscription in project aws-mobile-appsync-sdk-android by awslabs.

the class ApolloCallTracker method registerCall.

/**
 * <p>Adds provided {@link GraphQLCall} that is currently in progress.</p>
 *
 * <p><b>Note</b>: This method needs to be called right before an apolloCall is executed.</p>
 */
void registerCall(@Nonnull GraphQLCall call) {
    checkNotNull(call, "call == null");
    Operation operation = call.operation();
    if (operation instanceof Query) {
        registerQueryCall((AppSyncQueryCall) call);
    } else if (operation instanceof Mutation) {
        registerMutationCall((AppSyncMutationCall) call);
    } else if (operation instanceof Subscription) {
    } else {
        throw new IllegalArgumentException("Unknown call type");
    }
}
Also used : Query(com.apollographql.apollo.api.Query) AppSyncMutationCall(com.amazonaws.mobileconnectors.appsync.AppSyncMutationCall) Operation(com.apollographql.apollo.api.Operation) Mutation(com.apollographql.apollo.api.Mutation) Subscription(com.apollographql.apollo.api.Subscription)

Example 2 with Subscription

use of com.apollographql.apollo.api.Subscription in project aws-mobile-appsync-sdk-android by awslabs.

the class ApolloCallTracker method unregisterCall.

/**
 * <p>Removes provided {@link GraphQLCall} that finished his execution, if it is found, else throws an
 * {@link AssertionError}.</p>
 *
 * If the removal operation is successful and no active running calls are found, then the registered
 * {@link ApolloCallTracker#idleResourceCallback} is invoked.
 *
 * <p><b>Note</b>: This method needs to be called right after an apolloCall is completed (whether successful or
 * failed).</p>
 */
void unregisterCall(@Nonnull GraphQLCall call) {
    checkNotNull(call, "call == null");
    Operation operation = call.operation();
    if (operation instanceof Query) {
        unregisterQueryCall((AppSyncQueryCall) call);
    } else if (operation instanceof Mutation) {
        unregisterMutationCall((AppSyncMutationCall) call);
    } else if (operation instanceof Subscription) {
    } else {
        throw new IllegalArgumentException("Unknown call type");
    }
}
Also used : Query(com.apollographql.apollo.api.Query) AppSyncMutationCall(com.amazonaws.mobileconnectors.appsync.AppSyncMutationCall) Operation(com.apollographql.apollo.api.Operation) Mutation(com.apollographql.apollo.api.Mutation) Subscription(com.apollographql.apollo.api.Subscription)

Example 3 with Subscription

use of com.apollographql.apollo.api.Subscription in project aws-mobile-appsync-sdk-android by awslabs.

the class AppSyncSubscriptionInterceptor method interceptAsync.

@Override
public void interceptAsync(@Nonnull final InterceptorRequest request, @Nonnull ApolloInterceptorChain chain, @Nonnull final Executor dispatcher, @Nonnull final CallBack callBack) {
    final boolean isSubscription = request.operation instanceof Subscription;
    if (!isSubscription) {
        chain.proceedAsync(request, dispatcher, callBack);
        return;
    }
    chain.proceedAsync(request, dispatcher, new CallBack() {

        @Override
        public void onResponse(@Nonnull final InterceptorResponse response) {
            dispatcher.execute(new Runnable() {

                @Override
                public void run() {
                    // Declared here to allow error message to have scope
                    Map<String, Object> responseMap = null;
                    try {
                        ResponseJsonStreamReader responseStreamReader = ApolloJsonReader.responseJsonStreamReader(new BufferedSourceJsonReader(response.httpResponse.get().body().source()));
                        responseMap = responseStreamReader.toMap();
                        /* The response is of the form
                               extensions
                                    subscription
                                        mqttConnections
                                            [ url, topics [], clientID ]
                                        newSubscriptions
                                            individualSub
                                                topic, expireTime
                               data
                                    individualSubscriptions
                             */
                        // Get Subscription information by looking through the response
                        Map<String, LinkedHashMap> extensions = (Map) responseMap.get("extensions");
                        Map<String, Object> subscriptions = (Map) extensions.get("subscription");
                        List<Map<String, Object>> mqttConnections = (List) subscriptions.get("mqttConnections");
                        // Collect all the topics listed under newSubscriptions in the newTopics var.
                        List<String> newTopics = new ArrayList<>();
                        Collection<Map> newSubscriptions = ((Map) subscriptions.get("newSubscriptions")).values();
                        for (Map subscriptionInstance : newSubscriptions) {
                            if (subscriptionInstance.containsKey("topic")) {
                                newTopics.add((String) subscriptionInstance.get("topic"));
                            }
                        }
                        SubscriptionResponse subscriptionResponse = new SubscriptionResponse();
                        for (Map<String, Object> mqttConnection : mqttConnections) {
                            final String clientId = (String) mqttConnection.get("client");
                            final String wssURI = (String) mqttConnection.get("url");
                            final String[] preExistingTopics = ((List<String>) mqttConnection.get("topics")).toArray(new String[0]);
                            subscriptionResponse.add(new SubscriptionResponse.MqttInfo(clientId, wssURI, preExistingTopics));
                        }
                        AppSyncSubscriptionInterceptor.this.mSubscriptionManager.subscribe((Subscription) request.operation, newTopics, subscriptionResponse, mapResponseNormalizer);
                        Response parsedResponse = parseSubscription(request.operation, response);
                        callBack.onResponse(new InterceptorResponse(response.httpResponse.get(), parsedResponse, null));
                    } catch (Exception e) {
                        try {
                            callBack.onFailure(new ApolloException("Failed to parse subscription response: " + responseMap, e));
                        } catch (Exception e1) {
                            callBack.onFailure(new ApolloException("Failed to parse subscription response, failed to get body string", e));
                        }
                    } finally {
                        callBack.onCompleted();
                    }
                }
            });
        }

        @Override
        public void onFetch(FetchSourceType sourceType) {
            callBack.onFetch(sourceType);
        }

        @Override
        public void onFailure(@Nonnull ApolloException e) {
            callBack.onFailure(e);
        }

        @Override
        public void onCompleted() {
        /* call onCompleted in onResponse in case of error */
        }
    });
}
Also used : ArrayList(java.util.ArrayList) ResponseJsonStreamReader(com.apollographql.apollo.internal.json.ResponseJsonStreamReader) LinkedHashMap(java.util.LinkedHashMap) BufferedSourceJsonReader(com.apollographql.apollo.internal.json.BufferedSourceJsonReader) ArrayList(java.util.ArrayList) List(java.util.List) SubscriptionResponse(com.amazonaws.mobileconnectors.appsync.subscription.SubscriptionResponse) Subscription(com.apollographql.apollo.api.Subscription) ApolloException(com.apollographql.apollo.exception.ApolloException) SubscriptionResponse(com.amazonaws.mobileconnectors.appsync.subscription.SubscriptionResponse) Response(com.apollographql.apollo.api.Response) ApolloException(com.apollographql.apollo.exception.ApolloException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

Subscription (com.apollographql.apollo.api.Subscription)3 AppSyncMutationCall (com.amazonaws.mobileconnectors.appsync.AppSyncMutationCall)2 Mutation (com.apollographql.apollo.api.Mutation)2 Operation (com.apollographql.apollo.api.Operation)2 Query (com.apollographql.apollo.api.Query)2 SubscriptionResponse (com.amazonaws.mobileconnectors.appsync.subscription.SubscriptionResponse)1 Response (com.apollographql.apollo.api.Response)1 ApolloException (com.apollographql.apollo.exception.ApolloException)1 BufferedSourceJsonReader (com.apollographql.apollo.internal.json.BufferedSourceJsonReader)1 ResponseJsonStreamReader (com.apollographql.apollo.internal.json.ResponseJsonStreamReader)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1