react-apollo - 迁移后如何更新存储
问题描述
我有一个评论列表,其中每个评论都是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,自动找出获取数据的变化?
解决方案
推荐阅读
- flutter - Flutter 中初始启动时的屏幕选择器
- javascript - 当我点击按钮时,它会隐藏表格内容,提升
- flutter - ListView - AnimatedContainers - 只有一个展开
- firebase - 如何在 Flutter 中从 Firestore 中检索当前用户信息
- android - 自定义 Firebase 重置电子邮件内容
- pytorch - pytorchalbumentations 增强 p 值?
- three.js - 为什么我的 CatmullRomCurve3 有时会产生不露面的 TubeGeometry?
- qt - QML 错误:无效的属性名称 minX (M16)
- curl - axios 相当于 curl -u 标志
- javascript - 在 console.log 中调用函数(返回仍然未定义)