【问题标题】:How storage gets updated after migration迁移后如何更新存储
【发布时间】:2018-07-20 22:39:50
【问题描述】:

我有一个 cmets 列表,其中每个评论都是 Message 组件。

一个消息有一个“编辑”按钮,点击这个按钮,它的文本被替换为表单,它没有连接到 ApolloClient。

Message 组件也只是用来展示数据,它没有连接到 ApolloClient。

代码如下:

import React from 'react';
import gql from 'graphql-tag';
import { graphql, compose } from 'react-apollo';
import { number } from 'prop-types';
import withCurrentUser from '!/lib/hoc/withCurrentUser';
import Message from '!/components/messages/Message';


class CompanyComments extends React.Component {
  static propTypes = {
    companyId: number.isRequired
  };

  updateComment = (content, contentHTML, id) => {
    const {doEditCommentMutation, companyId} = this.props;

    return doEditCommentMutation({
      variables: {
        id: id,
        content: content
      }
    });
  }


  render(){
    const { data: {loading, comments}, companyId } = this.props;

    return(
      <div>
        <ul>
          {
            !loading &&
            comments &&
            comments.map((comment, i)=>(
              <Message
                key={i}
                index={i}
                id={comment.id}
                content={comment.content}
                user={comment.user}
                onReply={this.handleReply}
                handleUpdate={(content, contentHTML)=>(
                  this.updateComment(content, contentHTML, comment.id)
                )} />
            ))
          }
        </ul>
      </div>
    );
  }
}

const getCommentsQuery = gql`query
  getComments(
    $commentableId: Int,
    $commentable: String
  ) {
    comments(
      commentableId: $commentableId,
      commentable: $commentable,
      order: "createdAt ASC"
    ) {
      id
      content
      user {
        id
        nickname
      }
      createdAt
    }
  }
`;

const editCommentMutation = gql`mutation
  editCommentMutation(
    $id: Int!,
    $content: String!
  ) {
    editComment(
      id: $id,
      content: $content
    ) {
      id
      content
      createdAt
      user {
        id
        nickname
      }
    }
  }
`;

export default compose(
  graphql(getCommentsQuery, {
    options: ({companyId}) => ({
      variables: {
        commentableId: companyId,
        commentable: 'company'
      },
      pollInterval: 5000
    })
  }),
  graphql(editCommentMutation, {name: 'doEditCommentMutation'})
)(CompanyComments);

唯一连接到 ApolloClient 的组件是上面代码的这个组件。

有趣的行为开始了,当执行editComment 突变并且getComments 查询接收到的组件列表神奇地更新时。

怎么样?我没有使用乐观回应或refetch

是不是使用 ApolloClient 存储的新行为,而不是 Redux,自动找出获取数据的变化?

【问题讨论】:

    标签: react-apollo apollo-client


    【解决方案1】:

    我在

    中看到pollInterval: 5000
    graphql(
      getCommentsQuery, 
      { 
        options : { ... }  // << here
      }
    )
    

    pollInterval 强制重复获取查询。在您的示例中,每 5 秒。了解更多here

    由于 Apollo 缓存存储了规范化的数据,任何具有相同 __typenameid 的项目都将在存储中更新,并且它的 UI 表示会为正在侦听任何返回它们的查询的组件自动更新。更多关于automatic cache updates

    【讨论】:

    • 谢谢!自动缓存更新是我的例子,非常好的功能!
    • 是的,节省了很多时间! apollo 客户端的一大优势。我们还可以通过apollo-link-state 将其用于本地状态管理
    猜你喜欢
    • 2021-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    • 1970-01-01
    • 2016-05-08
    • 1970-01-01
    相关资源
    最近更新 更多