anzie001
anzie001

Reputation: 241

Getting `Store error: the application attempted to write an object with no provided typename but the store already contains an object with typename`

I am getting Store Error when updating and am new to apollo and graphQL so unable to understand what am i doing wrong. I am trying to update the first name and last name of a person. This object is nested and i am passing typeName as that of the wrapped object. If anyone understands this and can suggest please let me know

This is how it looks like in apollo:

validationType:age@empId

persondetails: personDetailsType

first: "abc"
empId: 10002
age: "21"
last: "def"

Values are being passed from here:

 <Button content="Save"
              onClick={(_, {value}) => {
                this.props.mutation({
                  variables: {
                    persondetails: {
                      first: this.state.first,
                      last: this.state.last,
                      empId: empId,
                      age: age
                    },
                      age,
                      empId,
                  }
                });
              }}
            />

My Update query looks something like this:

updatePersonDetails: (
      _,
      {persondetails, age, empId},
      {cache}
    ) => {
      cache.writeFragment({
        id: `validationType:${age}!${empId}`,
        fragment: gql`
          fragment updatePersonDetails on validationType {
            persondetails {
              first
              last
              age
              empId
              __typename

            }
            __typename
          }
        `,
        data: {
          persondetails: persondetails,
          age,
          empId,
          __typename: 'validationType'
        }
      });

I get below error when updating:

[Network error]: Error: Error writing result to store for query:
 {
  ...updatePersonDetails
}

fragment persondetails on validationType {
  persondetails {
   first
   last
   age
   empId
    __typename
  }
  __typename
}

Store error: the application attempted to write an object with no provided typename but the store already contains an object with typename of personDetailsType for the object of id $validationType!21.1002. The selectionSet that was trying to be written is:
persondetails {
 first
 last
 age
 empId
  __typename
}

Upvotes: 0

Views: 1702

Answers (1)

anzie001
anzie001

Reputation: 241

I was having my object declared in wrong way in resolvers when passing in update. And also i was missing typeName within that object. The correct syntax was:

updatePersonDetails: (
      _,
      {persondetails, age, empId},
      {cache}
    ) => {
      cache.writeFragment({
        id: `validationType:${age}!${empId}`,
        fragment: gql`
          fragment updatePersonDetails on validationType {
            persondetails {
              first
              last
              age
              empId
              __typename

            }
            __typename
          }
        `,
        data: {
          persondetails: {
           ...persondetails,
           __typename: 'persondetailsType'
}
          __typename: 'validationType'
        }
      });

Upvotes: 1

Related Questions