When running an @client mutation using apollo-link-state, then response from the resolver is merely:
{ data: {}, errors: undefined }
From the resolver i'm returning
{data: { ...product }}
.
Where the product is an object with multiple values, among these a
__typename
.
I have tried setting the return value, to all sorts of values, such as
return product
,
return { data: product }
,
return { data: { data: {
product }}}
etc. etc. i think you get the idea.
Resolver:
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 }}
}
Mutation:
const UPDATE_ITEM = gql
mutation updateInventoryItem($product: InventoryItem!) {
updateInventoryItem(product: $product) @client {
__typename
id
...and so on
}
}
;
The function that calls the mutation:
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))
}
Firstly: I'm sorry for the terribly long block of code to plow through.
The expected result of the code above, would be for the resolver to return the product just added, like
{ data: {...product}, errors: undefined }
, but instead it returns
{ data: {}, errors: undefined }
as stated in the beginning.
Please let me know if you need more information, or if i missed something.
This function does of course need to return a value, but that's not the whole story here.
GraphQL (Apollo-Client in this case), expects an object formed like the response, you tell it you want.
In this case:
This asks to get an object with a __typename, and id, and whatever else you put in there.
So all in all, a stupid mistake on my part, but since no answers, if anyone falls into the same pit as i did, here's something to try.