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.

watchQuery causes TypeError: Cannot assign to read only property 'data' of object

See original GitHub issue

I don’t know if I’m encountering a bug or if I’m doing something wrong. Every first time (after page load) my GQL query is executed, I get:

TypeError: Cannot assign to read only property ‘data’ of object

Subsequent queries (until the page is reloaded) are working correctly! My code doesn’t try to alter the received data, the error occurs while fetching the data, after a succesful reply of the server.

Here is the stack trace:

TypeError: Cannot assign to read only property ‘data’ of object at QueryInfo.js:210 at perform (inMemoryCache.js:197) at InMemoryCache.push.kifG.InMemoryCache.performTransaction (inMemoryCache.js:214) at QueryInfo.push.FmZO.QueryInfo.markResult (QueryInfo.js:180) at QueryManager.js:520 at asyncMap.js:11 at new ZoneAwarePromise (zone-evergreen.js:960) at Object.next (asyncMap.js:11) at notifySubscription (Observable.js:135) at onNotify (Observable.js:179)

This is my code:

    const query = gql`
      query identify($number: String!) {
        identify(number: $number) {
          number
          lastName
        }
      }
    `;
    subscription = this.apollo
      .watchQuery<IdentifyResult>({
        query,
        variables: {
          number
        }
      })
      .valueChanges.subscribe(
        queryResult => {
          console.log('queryResult', queryResult);
        },
        error => {
          console.error(error);
        }
      );

In the AppModule I don’t do anything special, I just add this provider:

    {
      provide: APOLLO_OPTIONS,
      useFactory: (httpLink: HttpLink) => {
        return {
          cache: new InMemoryCache(),
          link: httpLink.create({
            uri: 'http://localhost:60798/graphql/'
          })
        };
      },
      deps: [HttpLink]
    }

The error does not occur when I disable caching.

          .watchQuery<Citizen>({
            query: this.myQuery,
            variables: {
              nationalNumber: action.nationalNumber
            },
            fetchPolicy: 'no-cache'
          })

I am using these libraries:

@apollo/client”: “^3.3.6”, “apollo-angular”: “^2.1.0”, “graphql”: “^15.4.0”,

Your help is much appreciated!

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
JohanHeyvaertcommented, May 11, 2021

I don’t know which update/version I have to be thankful for, but in the current version (3.3.16) the error on longer occurs. It wasn’t working in 3.3.9 but it’s been a while since we updated our packages. So good news!

1reaction
intellixcommented, Jan 6, 2021

Related from the past: https://github.com/apollographql/apollo-client/issues/1909 This is stinging me again badly. Migrated to ApolloClient 3 and now getting alsorts of these errors throughout at runtime. Life is pain.

Can we not just opt into disabling freezing entirely? I’d prefer to have bugs due to mutating state than a readonly error being thrown at runtime in production personally. Still finding them in odd places.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeError: Cannot assign to read only property of object
I am fetching data using Apollo Angular library to populate the fields within the form (which uses ngModel for binding) for my application...
Read more >
Queries – Angular - GraphQL Code Generator
An Observable returned by watchQuery().valueChanges holds the actual result under the data field, so you can not directly access one of the ...
Read more >
class ApolloClient - Apollo GraphQL Docs
Defaults to "ROOT_QUERY", which is the ID of the root query object. This property makes writeQuery capable of writing data to any object...
Read more >
Vue/Apollo/graphql client cannot read 'watchQuery' error-Vue.js
... ERROR TypeError: Cannot read property 'minVersion' of undefined · Vue import causing cannot assign to read only property 'exports' of object error ......
Read more >
Cannot assign to read only property 'oppUrl' of object '#<Object>'
Next time please post full error include stacktrace. Stacktrace will help identify line causing the issue so community can you faster. – ...
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