我的反应本机应用程序中有一个更高阶的组件来检索配置文件。当我调用“添加追随者”突变时,我希望它更新个人资料以反映其追随者集合中的新追随者。如何手动触发对商店的更新。我可以重新获取整个配置文件对象,但更愿意只在没有网络重新获取的情况下进行插入客户端。目前,当我触发突变时,配置文件不会反映屏幕上的变化。
看起来我应该使用该update
选项,但它似乎不适用于我命名的突变。 http://dev.apollodata.com/react/api-mutations.html#graphql-mutation-options-update
const getUserQuery = gql`
query getUserQuery($userId:ID!) {
User(id:$userId) {
id
username
headline
photo
followers {
id
username
thumbnail
}
}
}
`;
...
const followUserMutation = gql`
mutation followUser($followingUserId: ID!, $followersUserId: ID!) {
addToUserFollowing(followingUserId: $followingUserId, followersUserId: $followersUserId) {
followersUser {
id
username
thumbnail
}
}
}`;
...
@graphql(getUserQuery)
@graphql(followUserMutation, { name: 'follow' })
@graphql(unfollowUserMutation, { name: 'unfollow' })
export default class MyProfileScreen extends Component Profile
...
this.props.follow({
variables,
update: (store, { data: { followersUser } }) => {
//this update never seems to get called
console.log('this never triggers here');
const newData = store.readQuery({ getUserQuery });
newData.followers.push(followersUser);
store.writeQuery({ getUserQuery, newData });
},
});