Graph 订阅apollo服务器中未发布自定义字段

Graph 订阅apollo服务器中未发布自定义字段,graph,react-apollo,apollo-server,Graph,React Apollo,Apollo Server,我正试图发布新添加的帖子,但是自定义字段和引用其他类型的字段author和voteCount没有发布,因此这些字段没有定义 我的模式: type Post { id: ID! title: String! content: String voteCount: Int! author: User! votes: [Vote!]! createdAt: Date! updatedAt: Date! } type Subscripti

我正试图发布新添加的帖子,但是自定义字段和引用其他类型的字段
author
voteCount
没有发布,因此这些字段没有定义

我的模式:

type Post {
    id: ID!
    title: String!
    content: String
    voteCount: Int!
    author: User!
    votes: [Vote!]!
    createdAt: Date!
    updatedAt: Date!
  }
type Subscription {
    Post(filter: PostSubscriptionFilter): PostSubscriptionPayload
  }
  input PostSubscriptionFilter {
    mutation_in: [_ModelMutationType!]
  }
  type PostSubscriptionPayload {
    mutation: _ModelMutationType!
    node: Post
  }
  enum _ModelMutationType {
    CREATED
    UPDATED
    DELETED
  }
分解器

Mutation: {
    addPost: async (
      root,
      { title, content },
      { ValidationError, models: { Post }, user },
    ) => {
      if (!user) {
        throw new ValidationError('unauthorized');
      }
      const post = new Post({
        title,
        content,
        author: user.id,
      });
      await post.save();
      pubsub.publish('Post', { Post: { mutation: 'CREATED', node: post } });
      return post;
    },
},
Subscription: {
    Post: {
      subscribe: () => pubsub.asyncIterator('Post'),
    },
  },
Post: {
    // eslint-disable-next-line no-underscore-dangle
    id: root => root.id || root._id,
    author: async ({ author }, data, { dataLoaders: { userLoader } }) => {
      const postAuthor = await userLoader.load(author);
      return postAuthor;
    },
    voteCount: async ({ _id }, data, { models: { Vote } }) => {
      const voteCount = await Vote.find({ post: _id }).count();
      return voteCount || 0;
    },
    votes: async ({ _id }, data, { models: { Vote } }) => {
      const postVotes = await Vote.find({ post: _id });
      return postVotes || [];
    },
  },
以及React客户端中的订阅:

componentWillMount() {
    this.subscribeToNewPosts();
  }
subscribeToNewPosts() {
    this.props.allPostsQuery.subscribeToMore({
      document: gql`
        subscription {
          Post(filter: { mutation_in: [CREATED] }) {
            node {
              id
              title
              content
              updatedAt
              voteCount
            }
          }
        }
      `,
      updateQuery: (previous, { subscriptionData }) => {
        // const result = Object.assign({}, previous, {
        //   allPosts: [subscriptionData.data.Post.node, ...previous.allPosts],
        // });
        // return result;
        console.log(subscriptionData);
        return previous;
      },
    });
  }
字段
voteCount
未定义:


当使用查询或突变时,它会正常发布,我应该怎么做?谢谢。

您看到的错误不一定意味着
voteCount
为空,而是意味着您正在尝试对未定义的值而不是对象进行解构。路径告诉您此错误是在尝试解析
voteCount
时发生的。在resolve函数中,可以在两个位置使用分解功能——一个是根对象,另一个是上下文。应该有一个根对象与您一起工作,所以我认为问题在于上下文

当您为典型的GraphQL服务器设置上下文时,您可以通过使用中间件(如
graphqlExpress
)将其本质上注入到您正在发出的请求中。当您使用订阅时,一切都是通过WebSocket完成的,因此中间件永远不会被命中,因此您的上下文为空


为了解决这个问题,我认为您需要在订阅中注入相同的上下文——您可以看到。

OMG!你救了我一天!非常感谢你!