react-native - Apollo graphql : writeQuery after mutation does not trigger re-render of flatlist

标签 react-native graphql apollo apollo-client

我在平面列表中具有以下按钮,可触发graphql突变,在突变后,我执行writeQuery以更新本地缓存(存储)。在变异的更新函数中,我正在更新缓存中的两个字段。基本上,当用户触摸“赞”按钮时,我会将“赞”的 bool 值更改为true,并通过+1(类似于“推特”)来更新该帖子的“赞”计数。但是,平面列表中的组件不会更新。我什至打印出了阿波罗存储/缓存,并且看到值正在更新。为何在缓存写入后不重新呈现平面列表?

   render() {


 const { posts, isFetching, lastUpdated, location, navigation, data, likeMutation, username, distancePointLatitude, distancePointLongitude, searchPointLatitude, searchPointLongitude } = this.props


 <FlatList
  data={data.near}
  style={styles.scrollViewContent}
  extraData={this.props.store}
  //renderSeparator={(sectionId, rowId) => <View key={rowId} style={styles.separator} />}
  showsVerticalScrollIndicator={false}
  onRefresh={this._onRefresh.bind(this)}
  refreshing={this.state.refreshing}
  keyExtractor={this._keyExtractor}
  renderItem={({item, index}) => item.posts.length != 0 && <ListItem>

{item.posts[0].userInteraction.userLike ? <Icon name='md-heart' style={{ color: 'crimson',fontSize: 28}} /> 
          : <Icon name='heart' style={{ fontSize: 26}} 

          onPress={() => likeMutation({ variables: { elementId: item.posts[0].postId, userId: username  },

            update: (store, { data: { addLike } }) => {
              // Read the data from our cache for this query.

              var thisLocationRadius = {searchPointLongitude: searchPointLongitude,
                searchPointLatitude: searchPointLatitude,
                radius: fiftyMilesInMeters, distancePointLongitude: distancePointLongitude,
                 distancePointLatitude: distancePointLatitude };


              var data = store.readQuery({ query: getLocalPosts,
                variables: {
                locationRadius: thisLocationRadius,
                userId: username
              }, });


             data.near[index].posts[0].userInteraction.userLike = true

              data.near[index].posts[0].interactionStats.totalLikes + 1


              // Write our data back to the cache.
              store.writeQuery({ query: getLocalPosts, data });



            },
          }).catch((error) => {
          console.log('there was an error sending the query', error);
          })} />  }
}

  const HomeWithData = graphql(getLocalPosts, {
        options:  ({ searchPointLongitude, searchPointLatitude, distancePointLongitude, distancePointLatitude, username }) => ({ variables: { locationRadius: {searchPointLongitude: searchPointLongitude,
           searchPointLatitude: searchPointLatitude,
           radius: fiftyMilesInMeters, distancePointLongitude: distancePointLongitude,
            distancePointLatitude: distancePointLatitude }, userId: username } }),

        });


export default compose( connect(mapStateToProps),
HomeWithData,
graphql(like, { name: 'likeMutation' }))(Home);

getLocalPosts查询:
export const getLocalPosts = gql`query getLocalPosts($locationRadius: locationRadius!, , $userId: String!) {
    near(locationRadius: $locationRadius){
      name,
      address,
      phonenumber,
      email,
      website,
      about,
      location {
        longitude,
        latitude
      },
      distance(unit: MILE),
      businessId,
      hours {
        weekDay,
        startTime,
        endTime
      },
      posts(isActive: true) {
        postText,
        postId,
        userInteraction(userId: $userId){
          userLike
        },
        interactionStats{
          totalLikes
        }
      },
    }
    }`;

最佳答案

我认为Apollo在决定更改后应触发哪个查询监听器时会考虑一些事项,包括variables字段。

对于您而言,带有平面列表的组件不会重新呈现,因为不会向查询通知更改。它不会收到更改通知,因为Apollo认为这是与调用writeQuery时要在商店中更新的查询不同的查询。

因此,解决方案是在调用variables时添加writeQuery字段。并且它应该具有与调用查询时所使用的值相同的值。

假设它们具有正确的值,则您对store.writeQuery的调用应如下所示:

          store.writeQuery({ 
            query: getLocalPosts, 
            data, 
            variables: {
              locationRadius: thisLocationRadius,
              userId: username
            }
          });

关于react-native - Apollo graphql : writeQuery after mutation does not trigger re-render of flatlist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49102797/

相关文章:

reactjs - redux-form 值得在 apollo-react-redux 应用程序中使用吗?

Angular Apollo 在突变中使用来自重新获取查询的响应

reactjs - 如何为 graphql 生成 TypeScript 定义

reactjs - React Native - 是否可以使用 rn-fetch-blob 下载多个资源?

react-native - borderStyle 在 android 中不起作用 - react-native

ios - 完整图像 Assets 未转换为 base64 字符串

react-native - 无法解析模块@react-navigation/stack

Azure 环境配置搞砸了 AJAX 调用? (Prismic、React、GraphQL、GatsbyJS)

Golang graphql schema字段无法返回数组

node.js - AWS Lambda 上的 Apollo Gateway 无法读取未定义的属性 'content-type'