当使用 apollo-link-state 运行 @client 突变时,解析器的响应仅仅是:
{ data: {}, errors: undefined }
从我要返回的解析器
{data: { ...product }}
。其中产品是具有多个值的对象,其中一个
__typename
.
我尝试将返回值设置为各种值,例如
return product
,
return { data: product }
等
return { data: { data: {
product }}}
。我想你明白了。
解析器:
export const updateInventoryItem = async (product, cache) => {
const query = gql
query {
inventory @client {
...omitted, but all the values
}
};
const prevState = await cache.readQuery({ query });
const prevInventory = prevState.inventory;
const productIndex = prevInventory.findIndex(x => x.id ===
product.id);
prevInventory.splice(productIndex, 1);
const data = {
inventory: [
product,
...prevInventory
]
}
await cache.writeQuery({ query, data });
return {data: { ...product }}
}
突变:
const UPDATE_ITEM = gql
mutation updateInventoryItem($product: InventoryItem!) {
updateInventoryItem(product: $product) @client {
__typename
id
...and so on
}
}
;
调用突变的函数:
updateItem = () => {
this.props.updateItem({
variables: {
product: {
__typename: this.state.item.__typename,
id: this.state.item.id,
addedToCart: this.state.item.addedToCart,
...and so on
}
}
}).then((e) => {
console.log(e);
this.setState({ savedRecently: true })
},
err => console.log(err))
}
首先:我很抱歉要通过非常长的代码块。
上面代码的预期结果是,解析器会返回刚刚添加的产品,例如
{ data: {...product}, errors: undefined }
,但它会返回
{ data: {}, errors: undefined }
如开头所述的那样。
如果您需要更多信息,或者我错过了什么,请告诉我。