subscriptions graphql 的一项实时数据推送的功能,还是很方便的,自己在直接使用subscriptions-transport-ws npm 包
的时候运行一直有错误(主要是依赖的apollo版本),还好hasura graphql 默认提供了一个开发模版,还是比较方便的

模版clone

git clone https://github.com/hasura/nodejs-graphql-subscriptions-boilerplate.git

基本代码集成

  • 使用模版(graphql server是自己的)
const { execute } = require('apollo-link');
const { WebSocketLink } = require('apollo-link-ws');
const { SubscriptionClient } = require('subscriptions-transport-ws');
const ws = require('ws');
const getWsClient = function(wsurl) {
const client = new SubscriptionClient(
wsurl, {reconnect: true}, ws
);
return client;
}; // wsurl: GraphQL endpoint
// query: GraphQL query (use gql`` from the 'graphql-tag' library)
// variables: Query variables object
const createSubscriptionObservable = (wsurl, query, variables) => {
const link = new WebSocketLink(getWsClient(wsurl));
return execute(link, {query: query, variables: variables});
};
const gql = require('graphql-tag');
const SUBSCRIBE_QUERY = gql`
subscription {
apps {
dr
id
appname
}
}
`;
function main() {
const subscriptionClient = createSubscriptionObservable(
'http://myserver:port/v1alpha1/graphql', // GraphQL endpoint
SUBSCRIBE_QUERY, // Subscription query
);
var consumer = subscriptionClient.subscribe(eventData => {
console.log("Received event: ");
console.log(JSON.stringify(eventData, null, 2));
}, (err) => {
console.log('Err');
console.log(err);
});
}
main();
  • 效果

说明

主要是package.json

  • package.json
{
"name": "nodejs-graphql-subscriptions-boilerplate",
"version": "1.0.0",
"description": "",
"main": "src/index.js",
"scripts": {
"start":"node index"
},
"author": "Karthik V",
"license": "ISC",
"dependencies": {
"apollo-link": "^1.2.2",
"apollo-link-ws": "^1.0.8",
"graphql": "^0.13.2",
"graphql-tag": "^2.9.2",
"subscriptions-transport-ws": "^0.9.12",
"ws": "^5.2.2" // 这个比较重要,一般可能会忘记添加,会有提示websocket 没有实现,还好官方模版提供好了
}
}
  • 数据查询demo
subscription:
subscription appdemo($input:Int!) {
apps (where:{
id: {
_eq:$input
}}
)
{
dr
id
appname
}
}
查询参数:
const subscriptionClient = createSubscriptionObservable(
'http://server:port/v1alpha1/graphql', // GraphQL endpoint
SUBSCRIBE_QUERY, // Subscription query
{id:1}
);

参考资料

https://github.com/hasura/nodejs-graphql-subscriptions-boilerplate
https://www.apollographql.com/docs/react/advanced/subscriptions.html

 
 
 
 

hasura graphql subscriptions 使用的更多相关文章

  1. Hasura GraphQL schema 生成是如何工作的

    不像大部分的graphql 引擎,使用标准的graphql 规范的处理模型,Hasura graphql 不存在resolver 的概念(实际上是有的,只是转换为了sql语法) 以下是Hasura g ...

  2. 通过torodb && hasura graphql 让mongodb 快速支持graphql api

    torodb 可以方便的将mongo 数据实时同步到pg,hasura graphql 可以方便的将pg 数据暴露为graphql api,集成在一起真的很方便 环境准备 docker-compose ...

  3. hasura graphql server 集成gatsby

    hasura graphql server 社区基于gatsby-source-graphql 开发了gatsby-postgres-graphql 插件, 可以快速的开发丰富的网站 基本使用 安装h ...

  4. hasura graphql server event trigger 试用

    hasura graphql server 是一个很不错的graphql 引擎,当前版本已经支持event triiger 了 使用此功能我们可以方便的集成webhook功能,实现灵活,稳定,快捷的消 ...

  5. hasura graphql auth-webhook api 说明

    hasura graphql 生产的使用是推荐使用webhook 进行角色访问控制的,官方同时提供了一个nodejs 的简单demo 代码 git clone https://github.com/h ...

  6. hasura graphql pg 自定义函数的使用

      hasura graphql 的安装可以参考相关项目 创建函数 数据表创建 CREATE TABLE sql_function_table ( id SERIAL PRIMARY KEY, inp ...

  7. Hasura GraphQL 内部表结构

    Hasura 使用pg 数据库存储引擎的元数据信息,在hdb_catalog schema 下面,是在初始化的时候生成的 对于表的管理.权限的信息存储都在这个schema下 hdb_table 这个表 ...

  8. hasura graphql server 集成gitlab

    默认官方是提供了gitlab 集成的demo的,但是因为gitlab 一些版本的问题, 跑起来总有问题,所以查找相关资料测试了一个可以运行的版本 项目使用docker-compose 运行 参考 ht ...

  9. hasura graphql 集成pipelinedb测试

    实际上因为pipelinedb 是原生支持pg的,所以应该不存在太大的问题,以下为测试 使用doker-compose 运行 配置 docker-compose 文件 version: '3.6' s ...

随机推荐

  1. 19重定向管道与popen模型

    重定向 dup2 int dup(int fd) 重定向文件描述符  int newFd = dup(STDOUT_FILENO) newFd 指向 stdout int dup2(int fd1, ...

  2. 大喜python版opencv3发布,demo脚本抢鲜版发布

    大喜,python版opencv3发布 zwPython3的升级也可以启动了,一直在等这个,zwPython会直接升级到版本3:zwPython3 zwPython3采用64位python3,支持op ...

  3. java第四天

    p32~p36: 学习javadoc 1.第一步,打开一个一定规模的java项目 2.第二步,搭建测试环境 IntelliJ IDEA ——> Tools ——> Generate Jav ...

  4. RESTful源码笔记之RESTful Framework的Mixins小结

    0x00 引言 本篇对drf中的mixins进行简要的分析总结.Mixins在drf中主要配合viewset共同使用,实现http方法与mixins的相关类与方法进行关联. from rest_fra ...

  5. selenium+java破解滑动验证码

    2019-04-16更新 修复极验页面改版,这次采用极验官方的demo地址:https://www.geetest.com/demo/slide-bind.html 截止2019-04-16,极验和腾 ...

  6. STRIDE 和 DREAD

    目录 STRIDE 和 DREAD 背景 STRIDE DREAD 注释 STRIDE 和 DREAD 背景 STRIDE 和 DREAD 是最常用也是最好用的安全模型 STRIDE 主要负责对安全风 ...

  7. HDU 4725 The Shortest Path in Nya Graph(最短路建边)题解

    题意:给你n个点,m条无向边,每个点都属于一个层,相邻层的任意点都能花费C到另一层任意点,问你1到n最小路径 思路:没理解题意,以为每一层一个点,题目给的是第i个点的层数编号.这道题的难点在于建边,如 ...

  8. POJ 2486 Apple Tree(树形dp)

    http://poj.org/problem?id=2486 题意: 有n个点,每个点有一个权值,从1出发,走k步,最多能获得多少权值.(每个点只能获得一次) 思路: 从1点开始,往下dfs,对于每个 ...

  9. UVa 10655 n次方之和(矩阵快速幂)

    https://vjudge.net/problem/UVA-10655 题意: 输入非负整数p,q,n,求a^n+b^n的值,其中a和b满足a+b=p,ab=q. 思路: 递推式转化成矩阵的规律: ...

  10. HTML深入探究(一)HTML入门

          HTML:超文本标记语言HyperText Markup Language,是一种用于创建网页的标准标记语言.HTML 不是一种编程语言,而是一种标记语言,标记语言是一套标记标签 (mar ...