'Update item in Apollo cache

I don't know how to update existing item in the list with apollo.

I have list like:

query TimePerProject($first: Int!, $offset: Int!) {
  timePerProjects(first: $first, offset: $offset) {
    edges {
      node {
        id
        startTime
        stopTime
        project {
          mainorder
        }
        person {
          id
          fullName
        }
      }
    }
    totalCount
  }
}

and mutation query with update as:

mutation UpdateTimePerProject($id: Int!, $projectmainorder: String, $startTime: Datetime, $stopTime: Datetime) {
  __typename
  updateTimePerProject(input: {patch: {projectId: $projectmainorder, startTime: $startTime, stopTime: $stopTime}, id: $id}) {
    clientMutationId
    timePerProjectEdge {
      node {
        id
        startTime
        stopTime
        project {
           mainorder
        }
        person {
          id
          fullName
        }
      }
    }
  }
}

I'm trying to use mutation as:

const [updateWorktime] = useMutation(
    UpdateWorkTimeMutation, {
      update(cache, { data:  { updateTimePerProject: { timePerProjectEdge } } }) {
        cache.modify({
          fields: {
            timePerProjects(existingTimePerProjects = [], { readField }) {
              const newCommentRef = cache.writeFragment({
                data: timePerProjectEdge ,
                fragment: gql`
                  fragment TimePerProject on TimePerProjectsEdge {
                    node {
                      id
                      startTime
                      stopTime
                      project {
                        mainorder
                      }
                    }
                  }
                `
              });
              return [...existingTimePerProjects];
            }
          }
        });
      }}
  );

Unfortunately it seems it is wrong way as I'm stuck with error:

Error: Could not identify object {"node":{"id":76552,"startTime":"2020-09-23T14:19:44+02:00","stopTime":"2020-09-23T15:53:54+02:00","projectId":"XXXX","person":{"fullName":"person name","__typename":"Person"},"__typename":"TimePerProject"},"__typename":"TimePerProjectsEdge"}

Any advices what is proper way to update item in cache list?



Solution 1:[1]

I was missing returning cursor from both original query and mutation. With both of them, cache automatically recognizes updated record without custom update function

Example (my API returns nodeId as cursor):

const cache = new InMemoryCache({
  typePolicies: {
    TimePerProject: { keyFields: ['nodeId']},

and query would look like:

query TimePerProject($first: Int!, $offset: Int!) {
  timePerProjects(first: $first, offset: $offset) {
    edges {
      node {
        id
        nodeId
        startTime
        stopTime
        project {
          mainorder
        }
        person {
          id
          fullName
        }
      }
    }
    totalCount
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1