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
//
}
});
}
Aggregations