graphql - 存储错误:应用程序试图写入一个没有提供类型名的对象,但存储已经包含一个对象
问题描述
在我更新缓存时发生突变后,更改会反映在 UI 中,但出现以下错误
不变违规:存储错误:应用程序尝试写入一个没有提供类型名称的对象,但存储已经包含一个类型名称为 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","
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
}
})
}
}
})
}
}
);
解决方案
您只需为查询的每个子部分添加“id”。在 GET_ITEM_CODE 查询中为“itemCodes”添加“id”可能会解决您的问题。
推荐阅读
- javascript - 在这里阻止每个 setState 的 componentWillReceivePropsrender 吗?
- r - 评估具有不同长度的多个二进制测试答案的正确统计测试/ R 函数是什么?
- database - 有没有办法在不同的环境中管理和部署更改?
- scala - 使用 IntelliJ 在 Maven 项目中进行 Scala 单元测试
- excel - 使用逻辑应用从在线共享点读取 Excel,然后插入 Azure SQL
- java - SoapUI - 在请求方法中使用自定义标头自动生成客户端
- html - 如何使按钮 onclick 动作在新窗口中打开?
- blazor - 如何从我的代码部分引用另一个组件
- php - 为什么 WordPress 图片 src=(unknown)?
- reactjs - 在 createGlobalStyles 中导入引导 css?