Ko7
Ko7

Reputation: 127

How to properly push object to data from Apollo Client

I tried to push some data received from Apollo Client,

    data.push({ key: `blank-${numberOfElementsLastRow}`, empty: true 
  return data; )}

But this gives me an error, Attempted to assign to readonly property. My question is How I can properly use data.push to the data getting from Apollo Client?

Upvotes: 0

Views: 599

Answers (1)

jack.benson
jack.benson

Reputation: 2363

Data from Apollo client is immutable (may not be altered). You would need to create a clone of the data, either by creating a deep clone (using something like lodash) or by spreading the properties of your object across a new object:

const { data , loading, error, fetchMore } = useQuery(GET_USERS,{
  variables: { cursor:  "" },
})

const mutableUsers = data.users.map(_ => {..._}); // Not sure this will work, can't remember how deep the immutability runs on these objects

As an aside, you are not supposed to modify data returned from an Apollo query, as this will mess up the local cache (hence the objects being immutable to begin with). For reference: https://www.apollographql.com/blog/previewing-the-apollo-client-3-cache-565fadd6a01e

InMemoryCache results are frozen/immutable

Upvotes: 2

Related Questions