graphql - Looking for code for a server side GraphQL subscription listener
问题描述
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: 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()}`);
推荐阅读
- c# - System.NullReferenceException:对象引用未设置为对象的实例。在测试自动化执行期间
- gcc - 与 FPU 寄存器内联的 arm gcc 程序集
- mysql - 具有许多嵌套子句的查询速度慢
- android - 在 Kotlin 中使用来自 Android 注释的自动生成的类
- javascript - SetState 未定义
- nim-lang - nim中的reduce相当于什么?
- c# - 在asp.net c#中将HTML Div(带有标签和印地语Unicode)转换为PDF
- javascript - 从 JavaScript 中的动态表格输入文本框创建动态对象
- azure-data-factory - 从父管道创建的分桶表中读取特定的桶文件
- sql-server - 带有 case 语句的字符串拆分或其他类似的 SQL Server 函数