GraphQL 進階: 使用RabbitMQ做爲PubSub後端實現GraphQL訂閱

這篇短文源於解決版本兼容性問題. graphql-rabbitmq-subscriptions 這項目已經半年沒有Commit了, 和最新的 graphql-subscriptions 有兼容性問題. 最新的 graphql-subscriptions 的GraphQL訂閱使用了 AsyncIterator, PubSubEngine增長了一個新的接口asyncIterator, 以下:git

export interface PubSubEngine {
  publish(triggerName: string, payload: any): boolean;
  subscribe(triggerName: string, onMessage: Function, options: Object): Promise<number>;
  unsubscribe(subId: number): any;
  asyncIterator<T>(triggers: string | string[]): AsyncIterator<T>;
}

如何運行

clipboard.png

左邊開兩個GraphiQL的窗口, 右側登陸 rabbitmqadmin 圖形管理界面. http://localhost:15672, 找到下面這個Exchange:github

clipboard.png

點擊進入 http://localhost:15672/#/exch...async

而後就能夠這樣發消息了spa

clipboard.png

Demo

順帶發一個能跑的DEMO: https://github.com/developerw...code

說明

這裏是修改的版本: https://github.com/developerw...graphql-rabbitmq-subscriptions 作了修改, 使其支持以下接口:server

asyncIterator<T>(triggers: string | string[]): AsyncIterator<T>;

RabbitMQ 默認安裝, 不作任何修改. Ubuntu 下的安裝命令爲 apt-get install rabbitmq-server接口

Github Issue:
https://github.com/cdmbase/gr...rabbitmq

變動:

2017-09-09:
1.https://github.com/developerw...
使用 rabbitmq-pub-subRabbitMqSingletonConnectionFactory替換RabbitMqConnectionFactory,不然不會重用鏈接, 而且建立生大量的Queue
clipboard.pngip

2017-09-10:
如今graphql-rabbitmq-subscriptions包的兼容性已經修復, 直接yarn add graphql-rabbitmq-subscriptions便可.get

Pull Request: https://github.com/cdmbase/gr...

相關文章
相關標籤/搜索