在 Apollo Client 中创建和删除关联时更新本地状态
Update Local State when Creating and Deleting Associations in Apollo Client
我的模式中有 student
和 team
类型的对象,还有一个 table student_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 查询重新获取数据吗?
我很想知道如何最好地使用 Apollo Link State。
谢谢!
您需要使用已通过的 update
道具手动完成:
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 })
}
}
}
还可以查看 this part 文档
我的模式中有 student
和 team
类型的对象,还有一个 table student_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 查询重新获取数据吗? 我很想知道如何最好地使用 Apollo Link State。
谢谢!
您需要使用已通过的 update
道具手动完成:
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 })
}
}
}
还可以查看 this part 文档