javascript - 乐观地更新 UI 时缺少字段 '_id' 、 'owner' 、 'meta' 和 'comment'

标签 javascript reactjs graphql react-apollo apollo-client

我正在尝试乐观地更新 UI,并为此使用 props 和 update 函数。但是我收到一条警告,例如缺少字段“_id”、“所有者”、“元”和“评论”。我还附上了警告屏幕截图。如何解决以下问题?

enter image description here

这是代码

    const withMutations = graphql(CommentMutation, {
  options: () => ({
    ...getDefaultOptions('collaboration')
  }),
  props: ({ ownProps, mutate }) => {
    return {
    submitComment: ({ value }) =>
      mutate({
        variables: {
          commentData: {
            comment: value
          }
        },
        optimisticResponse: {
          __typename: 'Mutation',
          addComment: {
            __typename: 'CommonResponse',
            _id       : -1,
            id        : null,
            comment   : value,
            meta      : ownProps.data[0].meta,
            owner     : ownProps.data[0].owner,
            status    : 'success',
            message   : 'Comment successfully added.',
            action    : 'AddComment',
          },
        },
//         optimisticResponse: {
//           __typename: 'Mutation',
//           addComment: {
//             __typename: 'CommonResponse',
//             id        : -1,
//             status    : 'success',
//             message   : 'Comment successfully added.',
//             action    : 'AddComment',
//           },
//         },
        update: (store, { data }) => {
          const data2 = store.readQuery({ query: CommentsQuery });
          data2.comments.unshift(data.addComment);
          store.writeQuery({ query: CommentsQuery, data: data2 });
        },
      }),
    };
  },
});

我这里有完整的代码

https://gist.github.com/SanskarSans/907063ed9a27d3f38f7de25ca4072faf

最佳答案

好吧,你必须传递一些东西。例如,只需使用 ShortId 生成临时 ID。当服务器的数据到达时,乐观数据就会回滚,所以这不是问题。如果您还不知道注释并且已正确地将 prop 声明为可选,则只需在乐观响应中将其声明为 null 即可。

关于javascript - 乐观地更新 UI 时缺少字段 '_id' 、 'owner' 、 'meta' 和 'comment',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48316417/

相关文章:

javascript - ReactJS 中的组件数组

graphql - 如何使用 where 子句执行 WpGraphQL 查询?

java - 提供 Web 浏览器和 Java 应用程序之间的接口(interface)

javascript - Object.getPrototypeOf 和 example.isPrototypeOf(obj) 给出了令人困惑的结果

javascript - 在仅满足某些条件的 id 之间进行选择

javascript - react-native:组件不在 .map 中呈现

javascript - 如何保护全局变量(窗口)不被覆盖

javascript - 使用 react 实现页脚不停留在底部

graphql - 有没有办法在 GraphQL 中执行 GROUP BY 操作?

javascript - 尝试更新密码 (GraphQL) "data must be a string and salt must either be a salt string"