寻找服务器端 GraphQL 订阅监听器的代码

Posted

技术标签:

【中文标题】寻找服务器端 GraphQL 订阅监听器的代码【英文标题】:Looking for code for a server side GraphQL subscription listener 【发布时间】:2019-08-09 12:24:48 【问题描述】:

我一直在寻找一些代码,这些代码允许我在服务器端注册到 GraphQL 订阅并读取来自服务器端外部订阅服务器的消息。我可以让我的服务器端订阅客户端连接到外部订阅服务器,但是我在连接后收到一条初始空消息,就像这样

 message: 'From Default Listener',
  data:  data:  eventAdded: null   

之后没有消息被捕获。请帮忙?这是我的代码,

const ws = require('ws');
const  ApolloClient = require('apollo-client');
const  SubscriptionClient  = require('subscriptions-transport-ws');
const  createHttpLink = require( 'apollo-link-http');
const  InMemoryCache  = require('apollo-cache-inmemory');
const fetch = require('node-fetch');
const gql = require('graphql-tag');

const serverConfig = 
    serverUrl:'http://localhost:4000/', 
    subscriptionUrl:'ws://localhost:4000/graphql'
   ;
const PORT = process.env.PORT || 4001;

let apollo;
let networkInterface;

const link = createHttpLink( uri: serverConfig.serverUrl, fetch: fetch );

networkInterface = new SubscriptionClient(
    serverConfig.subscriptionUrl,  reconnect: true , ws);
apollo = new ApolloClient(
    networkInterface ,
    link: link,
    cache: new InMemoryCache()
);

const client = () => apollo;
const subClient = client();
subClient.subscribe(
    query: gql`
        subscription eventAdded
            eventAdded
                id
                name
                payload
                createdAt
                storedAt
            
        
    `,
    variables: 
).subscribe(
    next: (data) => 
        console.log(message: 'From Default Listener', data);
    ,
    error: (err)=>
        console.log(err);
        done(err);
    
);

如果事实证明我做了一些非常愚蠢的事情,请原谅。任何帮助将不胜感激。

PS:当我使用 GraphQL Playground 订阅和获取消息时,订阅服务器工作正常。

【问题讨论】:

【参考方案1】:

想通了:

const ws = require('ws');
const  WebSocketLink  = require("apollo-link-ws");
const  execute = require("apollo-link");
const  SubscriptionClient  = require('subscriptions-transport-ws');
const gql = require('graphql-tag');

const serverConfig = serverUrl:'http://localhost:4000/', subscriptionUrl:'ws://localhost:4000/graphql';

const client = new SubscriptionClient(serverConfig.subscriptionUrl, 
    reconnect: true
, ws);

const link = new WebSocketLink(client);

const operation = 
    query: gql`
        subscription eventAdded
            eventAdded
                id
                name
                payload
                createdAt
                storedAt
            
        `
;

// execute returns an Observable so it can be subscribed to
execute(link, operation).subscribe(
    next: data => console.log(`received data: $JSON.stringify(data, null, 2)`),
    error: error => console.log(`received error $error`),
    complete: () => console.log(`complete`),
);

console.log(`Listener running at $new Date().toString()`);

【讨论】:

【参考方案2】:

与上面类似的实现,但在 es2015 中:

var gql_ws = require('ws');  // needed because no native web socket implementation is present
var apollo_link = require("apollo-link");
var apollo_link_ws = require("apollo-link-ws");
var sub_trans_ws = require('subscriptions-transport-ws');

var gql = require('graphql-tag');

var gql_serverConfig = 
  serverUrl: 'https://endpoint',
  subscriptionUrl: 'ws://endpoint/graphql'
;

var gql_client = new sub_trans_ws.SubscriptionClient(gql_serverConfig.subscriptionUrl, reconnect: true , gql_ws);
var gql_link = new apollo_link_ws.WebSocketLink(gql_client);

var query_object = `subscription 
  songUpdate     
    current 
      time
      metadata
        id
        artist
        title
      
    
  
`;

var gql_operation =  query: gql(query_object) ;

// execute returns an Observable so it can be subscribed to
apollo_link.execute(gql_link, gql_operation).subscribe(
  next: function next(data) 
    return console.log("received data: " + JSON.stringify(data, null, 2));
  ,
  error: function error(_error) 
    return console.log("received error " + _error);
  ,
  complete: function complete() 
    return console.log("complete");
  
);

【讨论】:

以上是关于寻找服务器端 GraphQL 订阅监听器的代码的主要内容,如果未能解决你的问题,请参考以下文章

Apollo 订阅未在客户端监听新数据

无需 GUI 即可收听 graphql 订阅的独立客户端

Appsync 客户端对 GraphQL 突变的 Angular 订阅

Apollo 订阅 PubSub 未触发侦听器

如何在 Apollo 中关闭 GraphQL 订阅的套接字连接

GraphQL 通过公共网关服务订阅内部服务