【发布时间】: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