【问题标题】:Custom field not getting published in subscription apollo server自定义字段未在订阅阿波罗服务器中发布
【发布时间】:2018-01-27 21:17:28
【问题描述】:

我正在尝试发布新添加的帖子,但未发布自定义字段并引用另一种类型的字段 authorvoteCount,因此我在这些字段上未定义。

我的架构:

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 字段未定义:

在使用查询或突变时,它可以正常发布,我该怎么办?谢谢。

【问题讨论】:

    标签: graph react-apollo apollo-server


    【解决方案1】:

    您看到的错误并不一定意味着voteCount 为空——而是意味着您试图解构未定义的值而不是对象。该路径告诉您在尝试解析 voteCount 时发生此错误。您在 resolve 函数中的两个地方使用解构 - 一次使用根对象,另一次使用上下文。应该有一个根对象可以使用,所以我想问题出在上下文。

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

    为了解决这个问题,我认为您需要将相同的上下文注入到您的订阅中——您可以看到an example of how to do that here

    【讨论】:

    • 天啊!你救了我的一天!非常感谢!
    猜你喜欢
    • 2021-02-13
    • 1970-01-01
    • 2017-12-11
    • 2021-09-09
    • 2019-02-08
    • 2020-10-18
    • 2020-09-26
    • 1970-01-01
    • 2019-10-08
    相关资源
    最近更新 更多