Mike SOK
05/21/2020, 1:35 PMasync minusFromCart(parent, args, ctx, info) {
// 1. Make sure they are signed in
const { userId } = ctx.request;
if (!userId) {
throw new Error('You must be signed in soooon');
}
// 2. Query the users current cart
const [existingCartItem] = await ctx.db.query.cartItems({
where: {
user: { id: userId },
item: { id: args.id },
},
});
// 3. Check if that item is already in their cart and increment by 1 if it is
if (existingCartItem) {
console.log('This item is already in their cart');
return ctx.db.mutation.updateCartItem(
{
where: { id: existingCartItem.id },
data: { quantity: existingCartItem.quantity - 1 },
},
info
);
}
//1. Find the cart item
const cartItem = await ctx.db.query.cartItem(
{
where: {
id: args.id,
},
},
`{ id, user { id }}`
);
if (existingCartItem < 1) {
//3. Delete that cart Item
return ctx.db.mutation.deleteCartItem ({
where: { id: args.id },
}, info);
}
},
@Harshit