graphql-java使用手册:part4 订阅(Subscriptions)

原文:http://blog.mygraphql.com/wordpress/?p=106html

订阅(Subscriptions)

订阅查询(Subscription Queries)

Graphql 订阅(subscriptions)使你能够让你订阅响应式数据源(reactive
source) 。当有新数据时,会发送给订阅者。java

能够阅读 http://graphql.org/blog/subsc...
来了解订阅的背景知识。react

假设你有一个股票服务。能够用这个 graphql 语句来订阅它的数据:git

subscription StockCodeSubscription {
    stockQuotes(stockCode:"IBM') {
        dateTime
        stockCode
        stockPrice
        stockPriceChange
    }
}

股票价格变化时,graphql 订阅 能够把 ExecutionResult
对象以流的方式传送给订阅者。和其它 graphql 查询同样,只会发送指定的字段
github

不一样的是,一开始的查询结果是一个响应式流(reactive-streams)
Publisher(流发布者) 对象。经过对象能够获取将来的数据。web

你须要使用 SubscriptionExecutionStrategy 策略做为执行策略(execution
strategy)。由于它支持 reactive-streams APIs.websocket

GraphQL graphQL = GraphQL
        .newGraphQL(schema)
        .subscriptionExecutionStrategy(new SubscriptionExecutionStrategy())
        .build();

ExecutionResult executionResult = graphQL.execute(query);

Publisher<ExecutionResult> stockPriceStream = executionResult.getData();

这里的 Publisher<ExecutionResult> 就是流事件的发布者【译注:原文
publisher of a stream of events】。你须要编写你本身的流处理代码,如:网络

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

须要编写 reactive-streams 代码去消费一源源不断的
ExecutionResults。你能够在 http://www.reactive-streams.org/ 中看到更
reactive-streams 代码的编写细节。socket

>><<RxJava是这个流行的 reactive-streams 实现。在
http://reactivex.io/intro.html 中能够看到更多建立Publishers 数据 和
Subscriptions 数据的细节。ide

graphql-java 只是产出一个流对象。它不关心如何在网络上用 web sockets
或其它手段发送流数据 。虽然这很重要,但不是做为基础 graphql-java
库应该作的。

咱们编写了一个 websockets 的(基于 Jetty)
模拟股票报价的示例应用。它使用了 RxJava。

详见 https://github.com/graphql-ja...

关于订阅服务的 Data Fetchers

订阅字段的 DataFetcher 的职责是生成一个 Publisher。这个 Publisher
输出的每个对象,将会经过 graphql 查询来映射。而后做为执行结果返回。

你会像这样子去编写Data Fetcher:

DataFetcher<Publisher<StockInfo>> publisherDataFetcher = new DataFetcher<Publisher<StockInfo>>() {
    @Override
    public Publisher<StockInfo> get(DataFetchingEnvironment environment) {
        String stockCodeArg = environment.getArgument("stockCode");
        return buildPublisherForStockCode(stockCodeArg);
    }
};

如何获取流事件,就由你的 reactive code 来决定 了。graphql-java会帮助你从流对象中获取 graphql 字段(fields)。像通常的 graphql查询同样。

相关文章
相关标签/搜索