寻找服务器端GraphQL订阅侦听器的代码 [英] Looking for code for a server side GraphQL subscription listener

查看:100
本文介绍了寻找服务器端GraphQL订阅侦听器的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have been looking high and low for some code that will allow me to register to a GraphQL subscription on the server side and read messages, coming from the external subscription server on the server side. I can get my server-side subscription client to connect to the external subscription server, but I after get an initial null message upon connection like so

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

此后没有捕获任何消息.请帮忙?这是我的代码,

no messages get captured there after. Help, please? Here is my code,

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);
    }
});

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

If it turns out I've done something really dumb, please excuse me. Any help will be really appreciated.

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

PS: The subscription server is working fine when I subscribe and get messages using GraphQL Playground.

推荐答案

弄清楚了:

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()}`);

这篇关于寻找服务器端GraphQL订阅侦听器的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆