Sujoy Saha
Sujoy Saha

Reputation: 220

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

After mutation when I am updating the cache, changes are reflected in UI but getting the below error

Invariant Violation: Store error: the application attempted to write an object with no provided typename but the store already contains an object with typename of ItemCodeConnection for the object of id $ROOT_QUERY.itemCodes({"filter":{"number":10000001}}). The selectionSet that was trying to be written is: {"kind":"Field","name":{"kind":"Name","value":"itemCodes"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"filter"},"value":{"kind":"Variable","name":{"kind":"Name","value":"filter"}}}],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"itemCodes"},"arguments":[],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"itemCodeTile"},"directives":[]},{"kind":"Field","name":{"kind":"Name","value":"__typename"}}]}},{"kind":"Field","name":{"kind":"Name","value":"__typename"}}]}}

GraphQL query:

const CREATE_ITEM_CODE_SPEC = gql`
mutation createItemCodeSpec($input: createItemCodeSpecInput) {
    createItemCodeSpecification(input: $input){
        __typename
        id
        itemCode {
            number
        }
        product
        spec_class
        grade
    }
}
`

const GET_ITEM_CODE  = gql`
    query itemCode($filter: filterInput){
        itemCodes(filter: $filter){
            itemCodes {
                number
                type
                description
                group 
                item_code_spec {
                    id
                    itemCode {
                        number
                    }
                product
                spec_class
                grade
           }
             created_on
             created_by
             changed_on
             changed_by      
            }
        }
    }
`

Below is the mutation:

const [mutation, { data, loading, error}] = useMutation(
        CREATE_ITEM_CODE_SPEC,
        {
            update(cache, { data: { createItemCodeSpecification } }){
                const currentData  = cache.readQuery({
                    query: GET_ITEM_CODE,
                    variables: { filter : {number:itemCode} } 
                })
                cache.writeQuery({
                    query: GET_ITEM_CODE,
                    variables: { filter : {number:itemCode} },
                    data: {
                        ...currentData,
                        itemCodes: {
                            itemCodes: currentData.itemCodes.itemCodes.map((itemCode, index) => {
                                return {
                                    ...itemCode,
                                    item_code_spec: index === 0? [
                                        ...itemCode.item_code_spec,
                                        createItemCodeSpecification
                                     ] : itemCode.item_code_spec
                                }
                            })
                        }
                    }
                }) 
            }
        }
        );

Upvotes: 0

Views: 1119

Answers (2)

Elcin Guseynov
Elcin Guseynov

Reputation: 535

You simply need to add "id" for each subsection of your query. Adding "id" for "itemCodes" in your GET_ITEM_CODE query might solve your problem.

Upvotes: 2

Ric0
Ric0

Reputation: 640

You have fields missing in your response mutation.

Basically, you should make your mutation results have all of the data necessary to update the queries previously fetched.

That’s also why is a best practice to use fragments to share fields among all queries and mutations that are related.

To make it work both query and mutation should have exactly the same fields.

Have a look here to see more in depth how cache updates work: https://medium.com/free-code-camp/how-to-update-the-apollo-clients-cache-after-a-mutation-79a0df79b840

Upvotes: 0

Related Questions