问题描述
我在我的React Native应用程序中具有更高阶的组件,可检索配置文件.当我称为"添加追随者"突变时,我希望它更新配置文件以反映其关注者集合中的新关注者.如何手动触发更新到商店.我可以拆除整个配置文件对象,但宁愿只做插入客户端,而无需进行网络进行补充.当前,当我触发突变时,轮廓并不能反映屏幕中的更改.
看来我应该使用update选项,但似乎对我的命名突变对我来说不起作用. http://dev.apollodata.com/react/api-mutations.html#graphql-munt-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 }); }, });
推荐答案
编辑:刚刚意识到您需要将更新添加到突变的GraphQL定义中.
编辑2:@monkeybonkey发现您必须在读取查询函数中添加变量
@graphql(getUserQuery) @graphql(followUserMutation, { name: 'follow', options: { update: (store, { data: { followersUser } }) => { console.log('this never triggers here'); const newData = store.readQuery({query:getUserQuery, variables}); newData.followers.push(followersUser); store.writeQuery({ getUserQuery, newData }); } }, }); @graphql(unfollowUserMutation, { name: 'unfollow' }) export default class MyProfileScreen extends Component Profile ... this.props.follow({ variables: { .... }, );
我建议您使用updateQueries功能 link .
参见例如,请参见此发布
您可以使用compose将突变添加到组件中.在突变中,您无需致电client.mutate.您只需在关注用户点击时调用突变.
也有可能让阿波罗为您处理更新.如果将突变响应更改为一点点,则将以下用户添加到后面的用户并添加dataIdFromObject功能. link
问题描述
I have a higher order component in my react native application that retrieves a Profile. When I call an "add follower" mutation, I want it to update the Profile to reflect the new follower in it's followers collection. How do I trigger the update to the store manually. I could refetch the entire profile object but would prefer to just do the insertion client-side without a network refetch. Currently, when I trigger the mutation, the Profile doesn't reflect the change in the screen.
It looks like I should be using the update option but it doesn't seem to work for me with my named mutations. 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 }); }, });
推荐答案
EDIT: Just realised that you need to add the update to the graphql definition of the mutation.
EDIT 2: @MonkeyBonkey found out that you have to add the variables in the read query function
@graphql(getUserQuery) @graphql(followUserMutation, { name: 'follow', options: { update: (store, { data: { followersUser } }) => { console.log('this never triggers here'); const newData = store.readQuery({query:getUserQuery, variables}); newData.followers.push(followersUser); store.writeQuery({ getUserQuery, newData }); } }, }); @graphql(unfollowUserMutation, { name: 'unfollow' }) export default class MyProfileScreen extends Component Profile ... this.props.follow({ variables: { .... }, );
I suggest you update the store using the updateQueries functionality link.
See for example this post
You could use compose to add the mutation to the component. Inside the mutation you do not need to call client.mutate. You just call the mutation on the follow user click.
It might also be possible to let apollo handle the update for you. If you change the mutation response a little bit that you add the following users to the followed user and add the dataIdFromObject functionality. link