我在Apollo的GraphQL订阅中遇到问题。我想订阅添加“观点”的主题(基本上增加了对帖子的评论),并且我很确定我的服务器设置正确。客户是什么给我麻烦。 (如果这个问题看起来很熟悉,我之前问过它,并认为我有答案,但没有去)。这里是我的订阅模式:Apollo GraphQL订阅
type Subscription {
perspectiveAdded: Perspective
}
schema {
query: RootQuery
mutation: Mutation
subscription: Subscription
}
我的订阅解析:
Subscription: {
perspectiveAdded(perspective) {
return perspective;
}
}
我subscriptionManager:
const pubsub = new PubSub();
const subscriptionManager = new SubscriptionManager({
schema,
pubsub,
setupFunctions: {
perspectiveAdded: (options, args) => {
perspectiveAdded: {
filter: (topic) => {
return topic
}
}
},
}
});
export { subscriptionManager, pubsub };
我addPerspective突变的最后一部分是(事件触发的订阅):
//...
return perspective.save((error, perspective) => {
if(error){
console.log(error);
}
//Publish it to Subscription channel
pubsub.publish('perspectiveAdded', perspective);
});
个
然后我有线了实际的服务器支持订阅:
const PORT = process.env.PORT || 4000;
const server = createServer(app);
server.listen(PORT,()=>{
new SubscriptionServer(
{
subscriptionManager: subscriptionManager,
onConnect: (connectionParams, webSocket) => {
console.log('Websocket connection established Lord Commander');
},
onSubscribe: (message, params, webSocket) => {
console.log("The client has been subscribed, Lord Commander", message, params);
},
onUnsubsribe: (webSocket) => {
console.log("Now unsubscribed, Lord Commander");
},
onDisconnect: (webSocket) => {
console.log('Now disconnected, Lord Commander');
}
},
{
server: server,
path: '/subscriptions',
});
console.log('Server is hot my Lord Commander!');
});
我已经连接好客户端正确地为好,因为在我的终端我看到“WebSocket连接建立”消息。我难以理解的部分是如何实际调用订阅。根据Apollo博客,我应该能够在GraphiQL中测试订阅(因为我使用apollo服务器,现在是graphql-server-express),但它说“解析函数为”Subscription.perspectiveAdded \“返回undefined ”。对于我的组件,我试图连接'subscribeToMore',但在浏览器控制台中,我收到一个错误对象,上面写着“从onSubscribe返回的无效params!返回值必须是对象!”我不确定它指的是哪个对象。
这里是我叫perspectiveSubscription订阅查询:
export default gql`
subscription {
perspectiveAdded {
id
content
}
}
`;
和有线起来组成:
constructor(props){
super(props);
this.state = {};
this.subscription = null;
}
componentWillReceiveProps(nextProps) {
if (!this.subscription && !nextProps.data.loading) {
let { subscribeToMore } = this.props.data
this.subscription = subscribeToMore(
{
document: perspectiveSubscription,
updateQuery: (previousResult, { subscriptionData }) => {
if(!subscriptionData.data){
console.log('no new subscription data');
return previousResult;
}
const newPerspective = subscriptionData.data.perspectiveAdded;
console.log(newPerspective);
return Object.assign({}, previousResult, newPerspective);
}
}
)
}
从这里,我在终端得到一个消息,说客户端已被认购,但仍我得到上面提到的错误对象。我一直在为这件事拖着头发 - 你们看到我在这里失踪了吗?具体来说,客户端的任何想法?感谢大家!
我已经玩过这个频道定义而无济于事。但你说得对,这可能是我的服务器的问题。我知道突变被正确调用,并且pubsub.publish()方法也被执行(我已将控制台记录下来)。看起来问题在于我的订阅解析器: '订阅:{ perspectiveAdded(perspective){0}console.log('did this work?'); return perspective; } }' console.log没有执行,我得到的错误说订阅解析函数返回undefined –
好奇怪的,你可能想检查你的可执行模式。看看订阅是否在里面。 –
'常量executableSchema = makeExecutableSchema({ 类型定义:类型, 解析器:解析器, 记录仪, allowUndefinedInResolve:假 });' 我定义的订阅类型和种类多达顶部的模式类型,但你说我还需要在这里单独订阅一些订阅吗? –