-
Notifications
You must be signed in to change notification settings - Fork 263
/
Copy pathhandle-checkout-mutation.js
30 lines (24 loc) · 1.11 KB
/
handle-checkout-mutation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
export default function handleCheckoutMutation(mutationRootKey, client) {
return function({data = {}, errors, model = {}}) {
const rootData = data[mutationRootKey];
const rootModel = model[mutationRootKey];
if (rootData && rootData.checkout) {
return client.fetchAllPages(rootModel.checkout.lineItems, {pageSize: 250}).then((lineItems) => {
rootModel.checkout.attrs.lineItems = lineItems;
rootModel.checkout.errors = errors;
rootModel.checkout.userErrors = rootModel.userErrors;
return rootModel.checkout;
});
}
if (errors && errors.length) {
return Promise.reject(new Error(JSON.stringify(errors)));
}
if (rootData && rootData.checkoutUserErrors && rootData.checkoutUserErrors.length) {
return Promise.reject(new Error(JSON.stringify(rootData.checkoutUserErrors)));
}
if (rootData && rootData.userErrors && rootData.userErrors.length) {
return Promise.reject(new Error(JSON.stringify(rootData.userErrors)));
}
return Promise.reject(new Error(`The ${mutationRootKey} mutation failed due to an unknown error.`));
};
}