Search in sources :

Example 1 with SubscriptionExecutionStrategy

use of graphql.execution.SubscriptionExecutionStrategy in project graphql-java by graphql-java.

the class SubscriptionExamples method basicSubscriptionExample.

void basicSubscriptionExample() {
    GraphQL graphQL = GraphQL.newGraphQL(schema).subscriptionExecutionStrategy(new SubscriptionExecutionStrategy()).build();
    String query = "" + "    subscription StockCodeSubscription {\n" + "        stockQuotes(stockCode:\"IBM') {\n" + "            dateTime\n" + "            stockCode\n" + "            stockPrice\n" + "            stockPriceChange\n" + "        }\n" + "    }\n";
    ExecutionResult executionResult = graphQL.execute(query);
    Publisher<ExecutionResult> stockPriceStream = executionResult.getData();
    AtomicReference<Subscription> subscriptionRef = new AtomicReference<>();
    stockPriceStream.subscribe(new Subscriber<ExecutionResult>() {

        @Override
        public void onSubscribe(Subscription s) {
            subscriptionRef.set(s);
            s.request(1);
        }

        @Override
        public void onNext(ExecutionResult er) {
            // 
            // process the next stock price
            // 
            processStockPriceChange(er.getData());
            // 
            // ask the publisher for one more item please
            // 
            subscriptionRef.get().request(1);
        }

        @Override
        public void onError(Throwable t) {
        // 
        // The upstream publishing data source has encountered an error
        // and the subscription is now terminated.  Real production code needs
        // to decide on a error handling strategy.
        // 
        }

        @Override
        public void onComplete() {
        // 
        // the subscription has completed.  There is not more data
        // 
        }
    });
}
Also used : GraphQL(graphql.GraphQL) SubscriptionExecutionStrategy(graphql.execution.SubscriptionExecutionStrategy) ExecutionResult(graphql.ExecutionResult) AtomicReference(java.util.concurrent.atomic.AtomicReference) Subscription(org.reactivestreams.Subscription)

Aggregations

ExecutionResult (graphql.ExecutionResult)1 GraphQL (graphql.GraphQL)1 SubscriptionExecutionStrategy (graphql.execution.SubscriptionExecutionStrategy)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Subscription (org.reactivestreams.Subscription)1