javascript - 尝试从更新的突变响应中写入 Apollo gql 中的缓存
问题描述
我在我的应用程序的某些部分有这个工作,但我试图在更大的查询上实现它但它不起作用。
我有以下突变,它确实运行了,但是在尝试写入缓存时它给了我一个错误,这样我就可以立即重新加载我的状态而不是刷新页面。
我回来的错误如下
错误:需要解析的 GraphQL 文档。也许您需要将查询字符串包装在“gql”标签中?
我正在使用 gql 标签,所以我认为不是这样。我认为在 UpdateDadHat Mutation 的更新缓存函数中写入 writeQuery 时,我的数据不匹配。
希望有敏锐眼光的人可以帮助我解决这个问题。
提前感谢
const [updateDadHat, { data: updateDadHatData, loading: updating }] =
useMutation(UPDATE_DAD_HAT, {
update(cache, { data: mutatedData }) {
const updateDadHatResponse = mutatedData?.updateDadHat;
if (updateDadHatResponse) {
cache.writeQuery({
query: GET_DAD_HATS_BY_USER_ID,
variables: { id: user.id },
data: {
findUserByID: {
hats: {
data: updateDadHatResponse,
},
},
},
});
}
},
});
updateDadHatResponse 如下所示
{
"markers":
[
{
"state": "select",
"containerTransformMatrix":
{
"e": 0,
"f": 0,
"a": 1,
"c": 0,
"b": 0,
"d": 1,
"__typename": "VtM"
},
"left": 37,
"height": 30,
"text": "your text here",
"color": "#EF4444",
"typeName": "TextMarker",
"fontFamily": "Helvetica, Arial, sans-serif",
"rotationAngle": 0,
"padding": 5,
"width": 100,
"top": 328,
"visualTransformMatrix":
{
"e": 0,
"f": 0,
"a": 1,
"c": 0,
"b": 0,
"d": 1,
"__typename": "VtM"
},
"__typename": "Marker"
}
],
"name": "The test hat",
"image": "http://res.cloudinary.com/image.png",
"_id": "304386127558606920",
"__typename": "DadHat"
}
以下架构
export const GET_DAD_HATS_BY_USER_ID = gql`
query FindUserByID($id: ID!) {
findUserByID(id: $id) {
_id
name
hats {
data {
markers {
left
height
text
color
typeName
rotationAngle
padding
width
top
fontFamily
state
containerTransformMatrix {
a
b
c
d
e
f
}
visualTransformMatrix {
a
b
c
d
e
f
}
}
name
image
_id
}
}
}
}
`;
export const UPDATE_DAD_HAT = gql`
mutation UpdateDadHat($id: ID!, $connect: ID!, $markers: [MarkerInput]) {
updateDadHat(
id: $id
data: { markers: $markers, owner: { connect: $connect } }
) {
markers {
state
containerTransformMatrix {
e
f
a
c
b
d
}
left
height
text
color
typeName
fontFamily
rotationAngle
padding
width
top
visualTransformMatrix {
e
f
a
c
b
d
}
}
name
image
_id
}
}
`;
解决方案
推荐阅读
- firebase - Firebase-tools 没有将新创建的函数上传到 Firebase
- python-3.x - 从持久 URL 获取 URL 重定向的有效方法
- sql - 为什么这不能捕获存储过程中的错误?
- unity3d - 无服务器推送通知
- php - 使用节点的字符串表示访问 stdClass 对象上的嵌套属性
- docker - 使用 mono repo 在 Docker 容器中包含 Go 依赖项
- angular - 为什么 ngx-bootstrap 未应用于我已分类的按钮
- r - keras中用于混合数据输入(图像,csv)的生成器函数 - R
- java - 如何在此 ArrayList 中搜索 null,然后将其替换为另一个 Object?
- c - 我需要正确读取行尾。空白区域问题