Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { encodeCartOpaqueId } from "@reactioncommerce/reaction-graphql-xforms/ca
import { resolveAccountFromAccountId, resolveShopFromShopId } from "@reactioncommerce/reaction-graphql-utils";
import checkout from "./checkout";
import items from "./items";
import totalItemQuantity from "./totalItemQuantity";

export default {
_id: (node) => encodeCartOpaqueId(node._id),
account: resolveAccountFromAccountId,
checkout,
items,
shop: resolveShopFromShopId
shop: resolveShopFromShopId,
totalItemQuantity
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { xformTotalItemQuantity } from "@reactioncommerce/reaction-graphql-xforms/cart";

/**
* @name "Cart.totalItemQuantity"
* @method
* @memberof Cart/GraphQL
* @summary Calculates the total quantity of items in the cart and returns a number
* @param {Object} cart - Result of the parent resolver, which is a Cart object in GraphQL schema format
* @param {Object} connectionArgs - Connection args. (not used for this resolver)
* @param {Object} context - An object containing the per-request state
* @return {Promise<Number>} A promise that resolves to the number of the total item quantity
*/
export default async function totalItemQuantity(cart, connectionArgs, context) {
if (!Array.isArray(cart.items)) return 0;

return xformTotalItemQuantity(context.collections, cart.items);
}
Original file line number Diff line number Diff line change
Expand Up @@ -323,3 +323,13 @@ export async function xformCartCheckout(collections, cart) {
}
};
}

/**
* @param {Object} collections Map of Mongo collections
* @param {Object} items Cart items
* @returns {Number} Total quantity of all items in the cart
*/
export async function xformTotalItemQuantity(collections, items) {
// Total item quantity comes from the sum of the quantities of each item
return (items || []).reduce((sum, item) => (sum + item.quantity), 0);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ type Cart implements Node {
"The shop that owns the cart."
shop: Shop!

"Total quantity of all items in the cart"
totalItemQuantity: Int!

"The date and time at which this cart was last updated."
updatedAt: DateTime!

Expand Down