我正在测试一个简单的案例,我可以添加猫并显示猫的详细信息。
但是我收到这个错误
bundle.esm.js:805 Invariant Violation: Can't find field color on object {
"id": "5cf74600ed38a91e472e0e20",
"name": "Rose",
"breed": "Italian",
"__typename": "Cat"
}
当我尝试在添加新猫之后更新缓存
this.apollo.mutate<CreateCatMutationResponse>({
mutation: CREATE_CAT_MUTATION,
variables: {
name: this.name,
breed: this.breed,
color: this.color
},
// awaitRefetchQueries: false,
update: (store, {data: {createCat}}) => {
// Read the data from our cache for this query.
const data: any = store.readQuery({query: cats, variables: {id: createCat.id}});
// Add our comment from the mutation to the end.
if (data) {
data.cats.push(createCat);
}
// Write our data back to the cache.
store.writeQuery({query: cats, data});
},
}`
因为它在页面中我试图添加一只新猫,所以只显示了2个属性,即名称和品种,而不是颜色:
this.allCat = this.apollo.watchQuery<Query>({
query: gql
查询猫{
猫 {
ID
名称
品种
}
}
,
fetchPolicy: 'cache-and-network'
})
.valueChanges
.pipe(
map(result => result.data.cats)
);
}
任何主意,如何在不使用refetch查询的情况下摆脱这个问题?