Websocket 图形解谜器
在客户端上使用apollo链接,在服务器上使用apollo server express中的PubSub。在与GraphQL API对话的mocha测试中得到奇怪的结果: 摩卡咖啡:Websocket 图形解谜器,websocket,graphql,apollo,Websocket,Graphql,Apollo,在客户端上使用apollo链接,在服务器上使用apollo server express中的PubSub。在与GraphQL API对话的mocha测试中得到奇怪的结果: 摩卡咖啡: import { execute, makePromise } from 'apollo-link'; const uri = 'http://localhost:3001/graphql'; const link = new HttpLink({ uri, fetch }); const subscribe
import { execute, makePromise } from 'apollo-link';
const uri = 'http://localhost:3001/graphql';
const link = new HttpLink({ uri, fetch });
const subscribe = (query, handlers) => {
const operation = {
query: gql`${query}`,
};
return execute(link, operation).subscribe(handlers);
};
const handlers = {
next: (data) => {
console.log(`received data: ${Date.now()}, ${JSON.stringify(data, null, 2)}`);
},
error: error => console.log(`received error ${error}`),
complete: () => console.log('complete'),
};
it('subscribe', async () => {
const query = `subscription {
info
}`;
subscribe(query, handlers);
});
服务器:
try {
console.log('subscription =>| ', Date.now(), '|', line);
worker.pubsub.publish('infoTopic', { info: line });
} catch (e) {
console.error(e);
}
以下是我从测试中看到的:
收到的数据:1545013826838,{错误:[
{
消息:对于不可为null的字段订阅,无法返回null。信息
从服务器:
订阅=>| 1545013826887 |信息深度1选择深度1
用户正在826838接收,
但出版商正在发送826887
怎么回事?我最近遇到了这个错误。我找到了一个解决方案,我需要发布的对象的字段名与订阅的字段名匹配。请注意,模式中的字段名newPost与解析程序中的名称匹配,以及发布到频道和订阅的对象的字段名匹配订阅操作:
// schema
type Subscription {
newPost: Post!
}
// subscription resolver
newPost: {
subscribe(parent, args, { pubsub }, info)
return pubsub.asyncIterator('new post')
}
}
// in the event publisher
pubsub.publish('new post', { newPost: post })
// the subscription operation
subscription {
newPost {
id
title
body
published
}
}
谢谢你,杰克。我终于让它工作起来了,不过可能正是通过你上面所做的。我发布了一篇关于它的hackernoon文章。