Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GraphQl] Single mutation for adding different types of products to the shopping cart #27914

Merged

Conversation

rogyar
Copy link
Contributor

@rogyar rogyar commented Apr 20, 2020

Description (*)

This PR introduces a new approach that allows adding different types of products using the same mutation.

More details will be provided soon ...

Solution Architecture

Related Pull Requests

https://github.com/magento/partners-magento2ee/pull/260
https://github.com/magento/partners-magento2-infrastructure/pull/17

Fixed Issues (if relevant)

  1. Single mutation for Add to Cart that supports all product types

Manual testing scenarios (*)

TBP

Questions or comments

The data providers for different product types were decoupled as separate modules according to the suggestion from @mslabko.

  • This PR does not include resolvers for ID_V2 of different options. This functionality will be provided in a separate PR
  • This PR does not include a possibility to use Parent SKU + SKU pair for adding items to cart (only via Parent SKU + product options in the same manner as the standard storefront works). The possibility to use Parent SKU + SKU may be provided in a separate PR (still in discussion)

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds are green)

@m2-assistant
Copy link

m2-assistant bot commented Apr 20, 2020

Hi @rogyar. Thank you for your contribution
Here is some useful tips how you can test your changes using Magento test environment.
Add the comment under your pull request to deploy test or vanilla Magento instance:

  • @magento give me test instance - deploy test instance based on PR changes
  • @magento give me 2.4-develop instance - deploy vanilla Magento instance

For more details, please, review the Magento Contributor Guide documentation.

@ghost ghost assigned paliarush Apr 21, 2020
Copy link
Contributor

@paliarush paliarush left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lenaorobei
Copy link
Contributor

@paliarush please confirm interfaces comply with requirements and standards.

}

if (is_string($result)) {
$errors = array_unique(explode("\n", $result));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For complex errors we should be able utilize composite GraphQlInputException. Please see example \Magento\QuoteGraphQl\Model\Cart\AddSimpleProductToCart::execute.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @lenaorobei. Thank you for your suggestion. In case of GraphQl modules, this approach will work. However, in this particular case, we work with the Quote module that has no dependency to GraphQl modules.
We use a similar approach in the Reorder functionality

if (is_string($addProductResult)) {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lenaorobei this ugly approach comes from \Magento\Quote\Model\Quote::addProduct and due to we need to provide error message(s) we need to "parse" it in that way, as was "designed" in Quote::addProduct

       /**
         * Error message
         */
        if (is_string($cartCandidates) || $cartCandidates instanceof \Magento\Framework\Phrase) {
            return (string)$cartCandidates;
        }

@ghost ghost assigned lenaorobei May 1, 2020
/** @var AddProductsToCartOutput $addProductsToCartOutput */
$addProductsToCartOutput = $this->addProductsToCart->execute($maskedCartId, $cartItems);

return [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With mentioned exception we should be able to return both - cart data and errors.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rogyar :
Below is a list of few of the use cases that are failing. Please take a look.
Case 1: add product to cart with a qty> the stock available
Actual result:

"errors": {
      "debugMessage": "Expected a value of type \"CheckoutUserInputErrorCodes\" but received: (empty string)","message": "Internal server error",

Case 2 : Add product with qty=0 and a valid sku,

Expected: cart is empty and error message to enter a quantity >0
Actual: The product is still added to the cart with qty =1

Case 3: Add disabled product to cart

Expected: Cart is empty and message that "product is not available"
Actual result :

exception: {
  "errors": [
    {
      "debugMessage": "Expected a value of type \"CheckoutUserInputErrorCodes\" but received: (empty string)",
      "message": "Internal server error",
Now, if we omit "code" from userInputError{} and run the same query, below exception is returned
exception: ""errors": [
    {
      "debugMessage": "User Error: expected iterable, but did not find one for field CheckoutUserInputError.path.","

Case 4 : Same result as above when you try to add a product with "Out of Stock" status.

@ghost ghost assigned rogyar May 6, 2020
@rogyar rogyar force-pushed the 257-single-mutation-add-to-cart branch from 015fcf3 to b9e28fd Compare May 7, 2020 15:18
@nrkapoor nrkapoor requested review from cpartica, mslabko and dthampy and removed request for nrkapoor May 8, 2020 16:16
@rogyar rogyar changed the title [WIP][GraphQl] Single mutation for adding different types of products to the shopping cart [GraphQl] Single mutation for adding different types of products to the shopping cart May 11, 2020
@rogyar rogyar marked this pull request as ready for review May 11, 2020 17:31
@prabhuram93
Copy link
Contributor

@magento run all tests

@prabhuram93
Copy link
Contributor

@magento run all tests

@ghost ghost moved this from Changes Requested to Ready for Testing in Pull Requests Dashboard Aug 5, 2020
@dthampy dthampy moved this from Ready for Testing to Testing in Progress in Pull Requests Dashboard Aug 5, 2020
@prabhuram93
Copy link
Contributor

@magento run all tests

@prabhuram93
Copy link
Contributor

@magento run all tests

@dthampy dthampy moved this from Testing in Progress to Merge in Progress in Pull Requests Dashboard Aug 6, 2020
@magento-engcom-team magento-engcom-team merged commit 8880671 into magento:2.4-develop Aug 11, 2020
@m2-assistant
Copy link

m2-assistant bot commented Aug 11, 2020

Hi @rogyar, thank you for your contribution!
Please, complete Contribution Survey, it will take less than a minute.
Your feedback will help us to improve contribution process.

@ghost ghost moved this from Merge in Progress to Recently Merged in Pull Requests Dashboard Aug 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Pull Requests Dashboard
  
Recently Merged
Development

Successfully merging this pull request may close these issues.

Single mutation for Add to Cart that supports all product types