1

我认为我的后端订阅设置正确。我在客户端使用角度,当我尝试调用时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"}]}}

也许我在后端遗漏了一些东西?我需要在 中定义setupFunctionsSubscriptionManager

这是我的订阅管理器

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

1 回答 1

1

是的,您缺少设置功能。您可以查看此链接graphql 订阅文档示例

您的订阅管理器可能如下所示:

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}

于 2017-03-18T08:14:15.623 回答