Apollo query and mutation tips
Apollo query and mutation tips
最近在使用Apollo为APP提供data,在此列举了一些在使用过程的常见需求。
使用mutation向服务器提交操作后更新视图:
//可以fetch任何query来更新catch,注意query名和参数要相同
refetchQueries: replyCommentResult => [
{
query: commentsQuery,
variables: {
articleid: comment.commentableid,
order: "LATEST_FIRST",
filter: "ALL"
}
}
]
//mutation数据返回后的操作,可以用来更新cache等等
update: (cache, { data: { result} }) => {
}
query通过variable来filter数据:
//把filter当做state来传递,通过change state来重新fetch data
<Query query={Query} variables={{ article_id: article.id, filter }}>
</Query>
query中强行fetch其它query:
//通过client对象来fetch query
import { Query, Mutation, withApollo } from "react-apollo";
this.props.client.query({
query: unreadsQuery,
//避免catch
fetchPolicy: "network-only"
});
//use withApollo() enhance screen
withApollo(CommentsScreen)
这个人暂时没有 freestyle