如何使用嵌入式片段更新缓存

时间:2020-02-28 04:49:40

标签: javascript apollo react-apollo apollo-client graphql-js

我正在尝试从小组中删除用户。用户已删除,但用户界面没有刷新就不会更新。

这是初始查询:

const GET_TEAM = gql`
  query Team($id: ID!) {
    team(id: $id) {
      name
      users {
        results {
          id
          name
        }
      }
    }
  }
`;

这是我的删除突变:

const [deleteTeamUser] = useMutation(DELETE_TEAM_USER, {
    update(store) {
      const data = store.readQuery({
        query: GET_TEAM,
        variables: {
          id: teamId,
        },
      });

      const index = data.team.users.results.findIndex(user => user.id === id);
      const newData = { ...data };
      if (index > -1) {
        newData.team.users.results.splice(index, 1);
      }

      store.writeFragment({
        id: `TeamDetail:${teamId}`,
        fragment: gql`
          fragment myTeam on TeamDetail {
            name
            users {
              results {
                id
                name
              }
            }
          }
        `,
        data: newData,
      });
    },
  });

控制台中的错误是:

Invariant Violation: Cannot match fragment because __typename property is missing: {"team":{"name":"team1","description":"","users":{"results":[],"__typename":"UserListResultSet"},"__typename":"TeamDetail"}}

有什么想法不更新用户界面吗?

0 个答案:

没有答案