question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError: Cannot assign to read only property

See original GitHub issue

I’m keep getting this error

const { data, client } = useQuery(GET_GROUPS);
const onSkillSelect = id => {
  setSkill({ variables: { id, skills: props.skills } });
};

const toggleSwitch = () => {
  data.dataGroups[props.id].isSkillsDisabled = false;
  client.writeData({ data: data });
}

when i’m trying to mutate something from the cache.

Uncaught TypeError: Cannot assign to read only property 'isSkillsDisabled' of object '#<Object>

I’m using 3.0 beta client react

import { gql, useMutation, useQuery } from '@apollo/client';

any idea how to override the cache?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

18reactions
dylanwulfcommented, Feb 3, 2020

this should work:

const { data, client } = useQuery(GET_GROUPS);
const onSkillSelect = id => {
  setSkill({ variables: { id, skills: props.skills } });
};

const toggleSwitch = () => {
  const newDataGroups = [...data.dataGroups];
  newDataGroups[props.id] = { ...newDataGroups[props.id], isSkillsDisabled: false };
  const newData = { ...data, dataGroups: newDataGroups };
  client.writeData({ data: newData });
}
16reactions
benjamncommented, Feb 3, 2020

The Apollo Client 2.6 blog post explains the benefits of immutability, and why it’s becoming mandatory in Apollo Client 3.0: https://blog.apollographql.com/whats-new-in-apollo-client-2-6-b3acf28ecad1

any idea how to override the cache?

If your first instinct in response to an error like this is to “override” the library that’s giving you the error, you’re starting off on the wrong foot. Please try to understand what’s going on first, or at least realize that the errors may be for your own good, before you jump to conclusions about possible workarounds. This issue tracker is for tracking bugs and features in the Apollo Client library, not a help desk for quick solutions to common problems that could be solved by reading the documentation on your own time.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot assign to read only property of Object in JavaScript
The error "Cannot assign to read only property of object" occurs when we try to change a property of an object that has...
Read more >
Uncaught TypeError: Cannot assign to read only property
When you use Object.defineProperties , by default writable is set to false , so _year and edition are actually read only properties.
Read more >
TypeError: "x" is read-only - JavaScript - MDN Web Docs
The JavaScript strict mode-only exception "is read-only" occurs when a global variable or object property that was assigned to is a read-only property....
Read more >
[Cannot assign to read only property '<field_name>' of object ...
This type of error(Cannot assign to read-only property) generally occurs on update of public property @api decorator variable value.
Read more >
Cannot assign to read only property 'X' of object ... - Reddit
This is most likely the issue, wired properties are immutable I believe. Best way to handle this is by using the spread operator...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found