• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

graphql-java/graphql-java-subscription-example: An example of graphql-java and s ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

graphql-java/graphql-java-subscription-example

开源软件地址(OpenSource Url):

https://github.com/graphql-java/graphql-java-subscription-example

开源编程语言(OpenSource Language):

Java 75.6%

开源软件介绍(OpenSource Introduction):

graphql-java Subscriptions over WebSockets example

An example of using graphql subscriptions via websockets, graphql-java, reactive-streams and RxJava.

To build the example code in this repository type:

./gradlew build

To run the example code type:

./gradlew run

To access the example application, point your browser at:

http://localhost:3000/  

Code Explanation

This example shows how you can use graphql-java subscription support to "subscribe" to a publisher of events. Then as events occur, graphql-java will map the original graphql query over those same event objects and send out a stream of ExecutionResult objects.

In this example application we have a stock update type system defined as:

type Subscription {
    stockQuotes(stockCodes:[String]) : StockPriceUpdate!
}

type StockPriceUpdate {
    dateTime : String
    stockCode : String
    stockPrice : Float
    stockPriceChange : Float
}

The JavaScript client sends a subscription graphql query over websockets to the server:

var query = 'subscription StockCodeSubscription { \n' +
    '    stockQuotes {' +
    '       dateTime\n' +
    '       stockCode\n' +
    '       stockPrice\n' +
    '       stockPriceChange\n' +
    '     }' +
    '}';
var graphqlMsg = {
    query: query,
    variables: {}
};
exampleSocket.send(JSON.stringify(graphqlMsg));

The server executes this with the graphql-java engine:

    GraphQL graphQL = GraphQL
            .newGraphQL(graphqlPublisher.getGraphQLSchema())
            .build();

    ExecutionResult executionResult = graphQL.execute(executionInput);

The result of that initial subscription query is a http://www.reactive-streams.org/ Publisher

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

Under the covers a RxJava 2.x implementation is used to provide a stream of synthesized stock events.

Rxjava Flows are an implementation of the reactive streams Publisher interface. You can use ANY reactive streams implementation as a source. graphql-java uses the reactive streams interfaces as a common interface.

See https://github.com/ReactiveX/RxJava for more information on RxJava.

The server side code then subscribes to this publisher of events and sends the results back over the websocket to the waiting browser client:

    stockPriceStream.subscribe(new Subscriber<ExecutionResult>() {

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

        @Override
        public void onNext(ExecutionResult er) {
            log.debug("Sending stick price update");
            try {
                Object stockPriceUpdate = er.getData();
                getRemote().sendString(JsonKit.toJsonString(stockPriceUpdate));
            } catch (IOException e) {
                e.printStackTrace();
            }
            request(1);
        }

        @Override
        public void onError(Throwable t) {
            log.error("Subscription threw an exception", t);
            getSession().close();
        }

        @Override
        public void onComplete() {
            log.info("Subscription complete");
            getSession().close();
        }
    });

The selection set of fields named in the original query will be applied to each underlying stock update object.

The selection set in this example application is selected as follows:

    stockQuotes {
        dateTime
        stockCode
        stockPrice
        stockPriceChange
    }

The underling stock update object is mapped to this selection of fields, just like any normal graphql query. The format of the results on the browser is JSON, again like any other normal graphql query.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap