0

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:

enter image description here Does someone know what is going wrong here?

Sven Z
  • 183
  • 1
  • 10
  • different read `GET_PROJECT_BY_ID` and write `GET_NEEDS_BY_PROJECT` ... if intended then no earlier `GET_NEEDS_BY_PROJECT` query result in cache – xadm Jan 29 '21 at 12:36
  • @xadm I edited it, but the error already happens at cache.readQuery, but I have no idea why. I use the same query to load all my content and it works perfectly – Sven Z Jan 29 '21 at 13:06
  • sth messed ... error is about query with hardcoded ids, not passed as variable – xadm Jan 29 '21 at 13:24
  • @xadm I pass the variable from the params. I dont understand why it says: on object `users`. As you can see in my query above, I don't have an object with users. When I load the page, I do a query to load the currentUser. Could that be the problem? – Sven Z Jan 29 '21 at 13:34
  • not ... how exactly query is defined and used ... not like, exact ... with variable type/defs – xadm Jan 29 '21 at 13:43
  • @xadm I don't understand what you mean? – Sven Z Jan 29 '21 at 14:14
  • ... line before `projects(where: { id: { _eq: $id } }) {` ... with operation name and variable definition? – xadm Jan 29 '21 at 14:17
  • @xadm `query getProjectById($id: Int!) {` – Sven Z Jan 29 '21 at 14:19
  • insert breakpoint or `debugger` and explore cache store entries ... how it (query entry) looks like for this query? – xadm Jan 29 '21 at 14:21
  • @xadm I added a picture with the entries – Sven Z Jan 29 '21 at 14:31
  • deeper, what is under ROOT_QUERY/projects – xadm Jan 29 '21 at 14:58
  • @xadm updated the picture – Sven Z Jan 29 '21 at 15:10
  • I see code/question updates ... strange entries, no `__typename` for `0:` , no `__ref`s ... entries different to query - `projects:xx` contains not queried `description` and `image` ... what apollo version? ... queries specs/type defs (return type) (from BE/API)? – xadm Jan 29 '21 at 16:23

0 Answers0