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. Linux学习笔记之Linux通过yum安装桌面

    Centos系统最小化安装以后,进入默认是命令行模式,所以需要进一步安装桌面. 1,本文使用的是CentOS 7 Minimal版本. 2,启动linux操作系统,进入后没有图形界面,但是有时候还是希 ...

  2. CSS3 页面中展示邮箱列表点击弹出发送邮件界面

    CSS3 页面中展示邮箱列表点击弹出发送邮件界面 代码: <!DOCTYPE html> <html> <head> <meta charset=" ...

  3. JavaScript里Math对象的ceil()、floor()、round()方法的区别

    ceil(x) 官方含义:对一个数进行上舍入.理解:ceiling为天花板的意思,意译为向上取整.即取得大于于等于x的最大整数. floor(x) 官方含义:对一个数进行下舍入.理解:floor为地板 ...

  4. 安装基础版的kinetic

    没有gui工具 sudo apt-get install ros-kinetic-ros-base

  5. C# 使用Dictionary、linq实现根据集合里面的字符串进行分组

    //对下面集合里面的字符串按照“_”进行分组. List<string> list = new List<string>() { "1_32", " ...

  6. Mybatis四种分页方式

    数组分页 查询出全部数据,然后再list中截取需要的部分. mybatis接口 List<Student> queryStudentsByArray(); xml配置文件 <sele ...

  7. RGB2YCbCr RGB2Gray

    Y = 0.2990R+0.5870G+0.1140B;                        Cb=-0.1687R-0.3313G+0.5000B+128;                 ...

  8. bzoj2843&&1180

    题解: lct 和上一题差不多 这一题还要判断是否有链接 其实直接并查集判断就可以了 代码: #pragma GCC optimize(2) #include<bits/stdc++.h> ...

  9. activity+fragment+listview+adapter+bean在同一个类中的套路

    1.xml activity_main.xml <?xml version="1.0" encoding="utf-8"?><FrameLay ...

  10. 【解决】Android 2.x 不支持overflow、position:fixed解决方案【转】

    Android 2.x和IOS5以下都不支持overflow:auto属性(position:fixed也不支持). 移动端浏览器兼容性和PC端相比,有过之而无不及.操作系统版本及各式浏览器和各式的屏 ...