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

doyleyoung/apollo-graphql-subscriptions-example: Apollo GraphQL Subscriptions is ...

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

开源软件名称(OpenSource Name):

doyleyoung/apollo-graphql-subscriptions-example

开源软件地址(OpenSource Url):

https://github.com/doyleyoung/apollo-graphql-subscriptions-example

开源编程语言(OpenSource Language):

JavaScript 87.8%

开源软件介绍(OpenSource Introduction):

Apollo GraphQL Subscriptions Example

Simple application used to demonstrate minimalistic setup for an Apollo GraphQL Subscriptions architecture.

System Architecture

  .---------.                            .--------.
  | Client  |-.        GET /             | Web    |
  | Browser | | -----------------------> | Server |
  '---------' |                          '--------'
    '---------'
         |
         |                               .---------.
         |          GET /graphiql        | GraphQL |
         '-----------------------------> | Server  |
                    Websocket            '---------'
  • Browser starts by connecting to Web App and fetch available messages
  • Web page opens websocket tunnel to GraphQL server and subscribes to new messages
  • GraphQL mutations can then be submitted to the GraphQL server and new messages submitted to websocket clients for browser update

The master branch implementation uses filtering to decide if a message is to be sent to a given subscriber. The client app generates a random number to be used as the auth token. All requests placed to the GraphQL server will include the auth token in it. At the server, the auth token will be stored in the GraphQL context and eventually used by the filter function that validates if a message is intended to be sent to any given subscriber. In a real application, the GraphQL server would have to first validate the auth token against a token provider before proceding with any request.

Start Subscription Client and Server apps

In a terminal do:

cd server-app
yarn start

In another terminal

cd client-app
yarn start

Test it

  1. Open a browser window with the client page
  2. Open another browser window with GraphiQL Subscriptions and press ►
  3. Open another browser window with GraphiQL Mutations and press ►

Your client page as well as the GraphiQL subscription page should now be displaying the new message.

Using CURL to exercise GraphQL Mutation:

curl -k -H "Content-Type: application/json" -X POST -d '{ "operationName": null, "query": "mutation AddMessage { addMessage(message: \"My CURL message\", broadcast: false) }", "variables": "{}" }' http://localhost:5060/graphql

Using the subscription Observable

Check the observable branch for the simplest subscription implementation.

Using the withApollo decorator

For an example using the withApollo decorator see the withApollo branch.

Using Apollo's subscribeToMore

Checkout subscribeToMore branch for an example implementation using Apollo's subscribeToMore subscription callback function.




鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
react-native-training/apollo-graphql-mongodb-react-native: Apollo Server Example ...发布时间:2022-06-13
下一篇:
igorkosandyak/spring-boot-graphql发布时间:2022-06-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

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

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

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