I try to update the cache
of my list of needs that are inside a project after a mutation
on 1 of the needs. But I get a Invariant Violation
.
First is important to know how I load my project. These is like the following:
projects(where: { id: { _eq: $id } }) {
id
title
user {
id
}
needs {
id
motivation
pending
user_id
}
}
I want to update a specific need. I do that with a mutation. After I do the mutation, I want to update the pending state in the UI, so I try to update my cache like the following.
updateNeed({
variables: {
id: selectedNeed.id,
motivation: motivation,
user_id: user.id,
pending: true,
},
optimisticResponse: true,
update: (cache) => {
const projects = cache.readQuery({
query: GET_PROJECT_BY_ID,
variables: { id: projectId },
});
const updatedProject = projects.project[0].needs.map((n) => {
if (n.id === need.id) {
return { ...n, pending: !n.pending };
} else {
return n;
}
});
cache.writeQuery({
query: GET_PROJECT_BY_ID,
variables: { id: projectId },
data: { projects: updatedProject },
});
},
});
However I run into the following error, happening at cache.readQuery
:
Invariant Violation: Can't find field projects({"where":{"id":{"_eq":122}}}) on object {
"users({\"where\":{\"id\":{\"_eq\":\"20\"}}})": [
{
"type": "id",
"generated": false,
"id": "users:20",
"typename": "users"
}
],
"projects({\"where\":{\"id\":{\"_eq\":\"122\"}}})": [
{
"type": "id",
"generated": true,
"id": "ROOT_QUERY.projects({\"where\":{\"id\":{\"_eq\":\"122\"}}}).0",
"typename": "projects"
}
]
}.
This is a sreenshot from the entries: