我正在使用GraphQL做一个React Native应用程序。在该应用程序中,存在一个用户加星标存储库的列表和一个用于查看器用户为存储库加注星标的按钮,但是问题是,当用户单击星标按钮时,UI显然不会重新获取userInfo查询。
功能:
userInfo = async (query, repoNumber) => {
const result = await this.props.client.query({
query: USER_INFO_QUERY,
variables: { query, repoNumber },
});
const user = result.data.user;
this.setState({ user });
}
addStar = (repoId) => {
this.props.client.mutate({
mutation: ADD_STAR_MUTATION,
variables: { repoId },
refetchQueries: [{
query: USER_INFO_QUERY,
variables: { query: this.state.user.login, repoNumber: 10 },
}]
});
}
removeStar = (repoId) => {
this.props.client.mutate({
mutation: REMOVE_STAR_MUTATION,
variables: { repoId },
refetchQueries: [{
query: USER_INFO_QUERY,
variables: { query: this.state.user.login, repoNumber: 10 },
}]
});
}
查询:
export const USER_INFO_QUERY = gql`
query UserInfoQuery($query: String!, $repoNumber: Int!) {
user (login: $query) {
id
login
avatarUrl
email
url
name
starredRepositories(first: $repoNumber) {
totalCount
nodes {
name
description
viewerHasStarred
id
}
}
}
}`;
突变:
export const ADD_STAR_MUTATION = gql`
mutation AddStar($repoId: ID!) {
addStar (input: { starrableId: $repoId }) {
clientMutationId
starrable {
viewerHasStarred
}
}
}
`;
export const REMOVE_STAR_MUTATION = gql`
mutation RemoveStar($repoId: ID!) {
removeStar (input: { starrableId: $repoId }) {
clientMutationId
starrable {
viewerHasStarred
}
}
}
`;