嗨,我正在从 apollo 服务器查询一些数据,结构如下:
hero {
image {
id
name
mimeType
url
}
title
description
}
welcome {
image {
id
name
mimeType
url
}
title
description
}
我在“响应”(在浏览器开发工具中)中收到的数据如下:
hero {
image {
"id": ''
"name": ''
"mimeType": ''
"url": ''
"__typename":"Asset"
}
title: 'some title'
description: 'some description'
"__typename":"Hero"
}
welcome {
image {
"id": ''
"name": ''
"mimeType": ''
"url": 'intro/welcome.png'
"__typename":"Asset"
}
title: 'another title'
description: 'another description'
"__typename":"Welcome"
}
但是当我
console.log
返回的数据进入代码时,我会合并“图像”对象,如下所示:
hero {
image {
"id": '',
"name": '',
"mimeType": '',
"url": 'intro/welcome.png', <-- welcome.image.url merged here as well.
}
title: 'some title',
description: 'some description',
}
welcome {
image {
"id": '',
"name": '',
"mimeType": '',
"url": 'intro/welcome.png',
}
title: 'another title',
description: 'another description',
}
问题:
因此,根据上述内容,我想了解发生这种情况背后的原因是什么。这是我必须启用/禁用的规则吗?这是一个原则,“当没有 id 值时,我合并对象”?
我在文档中读到,Apollo 客户端基于
id + typename
制作唯一密钥,所以在这种情况下,它是否感到困惑? (我的结论是“id”必须始终有价值。)
另一方面,如果“id”具有相同的值会发生什么?
我附上我查阅的来源,以供参考:https://www.apollographql.com/docs/react/caching/cache-field-behavior/#the-merge-function
谢谢。
''
仍然是一个字符串,因此也是一个有效的标识符,例如 '5'
或 'foo'
。
Apollo 客户端假设如果两个元素具有相同的
__typename
和 id
,则它们是同一个对象,应该合并在一起。
“No id”意味着您不请求
id
字段,或者它会返回为 null
- 在这种情况下,它们不会被合并。
dataIdFromObject
函数来覆盖此行为 - 但如果您可以控制后端,那么在这些情况下最好让它返回 null
。