2

我在 Apollo 中订阅 GraphQL 时遇到问题。我想订阅关于主题的添加“观点”(基本上是在帖子上添加评论),我很确定我的服务器设置正确。客户给我带来了麻烦。(如果这个问题看起来很眼熟,我之前问过它并认为我得到了答案,但没有去)。这是我的订阅模式:

type Subscription {
  perspectiveAdded: Perspective
}

schema {
  query: RootQuery
  mutation: Mutation
  subscription: Subscription
}

我的订阅解析器:

Subscription: {
    perspectiveAdded(perspective) {
      return perspective;
    }
  }

我的订阅管理器:

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),但它显示“Resolve function for \"Subscription.perspectiveAdded\”返回未定义”。

对于我的组件,我尝试连接“subscribeToMore”,但在浏览器控制台中,我收到一个错误对象,上面写着“onSubscribe 返回的参数无效!返回值必须是一个对象!” 我不确定它指的是哪个对象。

这是我的订阅查询,称为透视订阅:

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

From here, I get a message in my terminal saying the client has been subscribed, but still I get the error object mentioned above. I've been pulling my hair out about this for days - do you guys see what I am missing here? Specifically, any ideas on the client side? Thanks everyone!

4

1 回答 1

0

It seems like the server side is not correct, because the subscription is added and graphiql also does not deliver a correct result.

One thing that i suggest is that you check the channel definition:

const pubsub = new PubSub();
const subscriptionManager = new SubscriptionManager({
  schema,
  pubsub,
  setupFunctions: {
    perspectiveAdded: (options, args) => {
      perspectiveAdded: {
        filter: (perspective) => {
        console.log(perspective); // check if object is correct
          return true; // return true and not the object as long as you do not want to filter
        }
      }
    },
  }
});

export { subscriptionManager, pubsub };

And also check if the perspective object is saved and defined before the pubsub call. And i think you also want to add a comment id for which the subscription should be working. On my side it looks more or less like in this post

于 2017-04-22T11:17:35.427 回答