Graphql 在Apollo客户端中创建和删除关联时更新本地状态
我的模式中有Graphql 在Apollo客户端中创建和删除关联时更新本地状态,graphql,apollo,react-apollo,apollo-client,Graphql,Apollo,React Apollo,Apollo Client,我的模式中有student和team类型的对象,还有一个表student\u team,用于记录它们之间的关系。 要管理这些,我使用以下调用: // to add a student team relationship mutation CreateStudentTeam($studentId: UUID!, $teamId: UUID!) { createStudentTeam( input: { studentTeam: { studentId: $studentId, team
student
和team
类型的对象,还有一个表student\u team
,用于记录它们之间的关系。
要管理这些,我使用以下调用:
// to add a student team relationship
mutation CreateStudentTeam($studentId: UUID!, $teamId: UUID!) {
createStudentTeam(
input: { studentTeam: { studentId: $studentId, teamId: $teamId } }
) {
student {
id
}
team {
id
}
}
}
// to remove a student team relationship
mutation DeleteStudentTeam($studentId: UUID!, $teamId: UUID!) {
deleteStudentTeamByStudentIdAndTeamId(input: {studentId:$studentId, teamId:$teamId}) {
student {
id
}
team {
id
}
}
}
// to view teams with students
query Teams {
teams {
nodes {
id
name
students {
nodes {
id
fullName
}
}
}
}
}
我的应用程序将基于这些关系的数据显示为列表。
团队可能会收到一份学生名单。
打了这些电话后,我对如何更新本地状态有点困惑。
最好的做法是用Teams查询重新获取数据吗?
我很想知道如何最好地与阿波罗连接状态
谢谢 您需要使用
update
prop passed手动执行此操作:
graphql(YourMutation, {
options: {
update: (cache, result) => {
const query = TeamsQuery
const currentQueryData = cache.readQuery({query})
const updatedData = //change your data regarding of the mutation
cache.writeQuery({query, data: graphql updatedData })
}
}
}
还可以看一看这些文档