0

我想从帖子列表中删除帖子。删除的时候,我先通过读取对象readQuery,然后做深拷贝,再通过更改缓存writeQuery。如果我再次读取缓存,它确实会改变。但是,在页面上,订阅变量的值this.moments$并没有改变,所以 UI 也没有改变。有人能帮我吗?

  • “阿波罗角”:“^1.8.0”,
  • “阿波罗客户端”:“^2.6.8”,
  • “离子”:“^5.4.16”,
  • 角 8.2.14
-  this is the delete function
public async updatePost(updateInput: PostUpdateInput): Promise<Result<PostEntity>> {
    return await getResult<PostEntity>(
      this.apollo.mutate<Mutation>({
        mutation: UPDATE_POST,
        variables: {
          updateInput
        },
        update: (proxy, { data }) => {
          const { postUpd } = data
          if (postUpd) {
            if (updateInput.isDeleted) {
               try {
                const myListCache = proxy.readQuery<Query>({
                  query: SELF_LIST,
                  variables: { userId: postUpd.userId, watchId: postUpd.userId, offset: 0, num: 10 }
                });
                let myCache = JSON.parse(JSON.stringify(myListCache.user.post.postsOfSelf));
                myCache = myCache.filter(i => {
                  console.log(i.id, postUpd.id);
                  return i.id !== postUpd.id
                })
                proxy.writeQuery({
                  query: SELF_LIST,
                  variables: { userId: postUpd.userId, watchId: postUpd.userId, offset: 0, num: 10 },
                  data: {
                    user: {
                      __typename: dataCache.user.__typename,
                      post: {
                        __typename: dataCache.user.post.__typename,
                        postsOfSelf: [...myCache]
                      }
                    }
                  }
                });
               } catch (e) {
                 console.log('something wrong');
               }

            } else {
              this.changePost(postUpd.id, postUpd.userId, (post) => {
                copyAttrs(post, updateInput);
              })
            }

          }
        }
      })
        .pipe(
          map(res => res.data.postUpd)
        )
        .toPromise()
    )
  }
  • gql
export const SELF_LIST = gql`
query($userId: Int!, $offset: Int, $num: Int){
  user(userId: $userId){
    post{
      postsOfSelf(offset: $offset, num: $num){
        id
        userId
        content
        createdAt
        updatedAt
        editedAt
        userInfo{
          id
          nickName
          photo
        }
      }
    }
  }
}
`
  • 这个变量不会改变
    this.moments$ = this.momentsDataService.getSomeonePostList(this.fId, this.baseInfoService.userId, this.momentsPage, 'cache-first');

  • html
<div class="container" *ngFor="let moment of likeMoments$ | async; index as i;">
          <app-saying [moment]="moment" [index]="i"></app-saying>
</div>
4

0 回答 0