我认为我正确设置了后端订阅。我在客户端使用angular,当我尝试调用subscribe
时出现错误
passwordUpdatedSubscription = gql`
subscription passwordUpdated{passwordUpdated{name password}}
`;
// Apollo Subscription
var subscription = this.apollo.subscribe({
query: this.passwordUpdatedSubscription
});
subscription.subscribe(
{
next(data) {
console.log(data);
},
error(err) { console.error('err', err); },
}
);
然后这是控制台中出现的错误
{"type":"subscription_fail","id":0,"payload":{"errors":[{"message":"Cannot read property 'subscribe' of undefined"}]}}
也许我在后端错过了一些东西?我是否需要在setupFunctions
?
SubscriptionManager
这是我的SubscriptionManager
const sub = require('graphql-subscriptions');
const pubSub = new sub.PubSub();
const manager = new sub.SubscriptionManager({
schema,
pubSub
});
这是我在graphQL中的架构
const graphql = require('graphql');
var schema = graphql.buildSchema(`
type Subscription {
passwordUpdated: User
}
type Mutation {
setMessage(message: String): String,
updateUserPassword(userName: String, password: String): User!
}
type Query {
getMessage: String,
getUsers: [User],
findUsers(userName: String): [User]
}
type User {
name: String,
password: String
}
`);
答案 0 :(得分:1)
是的,你缺少设置功能。您可以查看此链接graphql subscription docu或example。
您的订阅管理器可能如下所示:
const manager = new sub.SubscriptionManager({
schema,
pubSub,
setupFunctions: {
passwordUpdated: (options, args) => ({ // name of your graphQL subscription
passwordUpdatedChannel: { // name of your pubsub publish-tag
filter: () => {
return true
},
},
}),
},
});

当您调用pubsub发布函数时,您必须像pubsub.publish("passwordUpdatedChannel")
一样编写它。
Sidenode:您可能希望将已更改密码的用户的ID添加到订阅中。如果您这样做,可以将其添加到过滤器选项,可能看起来像filter: (user) => {return user.id === args.userId}