【发布时间】:2020-09-05 05:15:29
【问题描述】:
当我更新缓存时发生突变后,更改会反映在 UI 中,但出现以下错误
Invariant Violation: Store error: 应用程序试图写入一个没有提供类型名的对象,但存储已经包含一个类型名为 ItemCodeConnection 的对象,用于 id $ROOT_QUERY.itemCodes({"filter":{"number" :10000001}})。试图写入的 selectionSet 是: {"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": "名称","值":"__typename"}}]}},{"kind":"Field","name":{"kind":"Name","value":"__typename"}}]} }
GraphQL 查询:
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
}
}
}
`
下面是突变:
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
}
})
}
}
})
}
}
);
【问题讨论】:
标签: graphql apollo apollo-client