Skip to content

Commit c49da73

Browse files
author
Valeriy Naida
authored
Merge pull request #3442 from magento-engcom/graphql-develop-prs
[EngCom] Public Pull Requests - GraphQL
2 parents 8cc960c + a5ccff7 commit c49da73

35 files changed

+879
-37
lines changed

app/code/Magento/CatalogGraphQl/etc/schema.graphqls

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ interface CustomizableOptionInterface @typeResolver(class: "Magento\\CatalogGrap
358358
title: String @doc(description: "The display name for this option")
359359
required: Boolean @doc(description: "Indicates whether the option is required")
360360
sort_order: Int @doc(description: "The order in which the option is displayed")
361+
option_id: Int @doc(description: "Option ID")
361362
}
362363

363364
interface CustomizableProductInterface @typeResolver(class: "Magento\\CatalogGraphQl\\Model\\ProductInterfaceTypeResolverComposite") @doc(description: "CustomizableProductInterface contains information about customizable product options.") {

app/code/Magento/CustomerGraphQl/Model/Customer/UpdateAccountInformation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function execute(int $customerId, array $data): void
7373

7474
if (isset($data['email']) && $customer->getEmail() !== $data['email']) {
7575
if (!isset($data['password']) || empty($data['password'])) {
76-
throw new GraphQlInputException(__('For changing "email" you should provide current "password".'));
76+
throw new GraphQlInputException(__('Provide the current "password" to change "email".'));
7777
}
7878

7979
$this->checkCustomerPassword->execute($data['password'], $customerId);

app/code/Magento/CustomerGraphQl/Model/Resolver/ChangePassword.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1818

1919
/**
20-
* @inheritdoc
20+
* Change customer password resolver
2121
*/
2222
class ChangePassword implements ResolverInterface
2323
{
@@ -70,11 +70,11 @@ public function resolve(
7070
array $args = null
7171
) {
7272
if (!isset($args['currentPassword'])) {
73-
throw new GraphQlInputException(__('"currentPassword" value should be specified'));
73+
throw new GraphQlInputException(__('Specify the "currentPassword" value.'));
7474
}
7575

7676
if (!isset($args['newPassword'])) {
77-
throw new GraphQlInputException(__('"newPassword" value should be specified'));
77+
throw new GraphQlInputException(__('Specify the "newPassword" value.'));
7878
}
7979

8080
$currentUserId = $context->getUserId();

app/code/Magento/CustomerGraphQl/Model/Resolver/GenerateCustomerToken.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ public function resolve(
4545
array $args = null
4646
) {
4747
if (!isset($args['email'])) {
48-
throw new GraphQlInputException(__('"email" value should be specified'));
48+
throw new GraphQlInputException(__('Specify the "email" value.'));
4949
}
5050

5151
if (!isset($args['password'])) {
52-
throw new GraphQlInputException(__('"password" value should be specified'));
52+
throw new GraphQlInputException(__('Specify the "password" value.'));
5353
}
5454

5555
try {

app/code/Magento/CustomerGraphQl/Model/Resolver/RevokeCustomerToken.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,6 @@ public function resolve(
5555

5656
$this->checkCustomerAccount->execute($currentUserId, $currentUserType);
5757

58-
return $this->customerTokenService->revokeCustomerAccessToken((int)$currentUserId);
58+
return ['result' => $this->customerTokenService->revokeCustomerAccessToken((int)$currentUserId)];
5959
}
6060
}

app/code/Magento/CustomerGraphQl/etc/schema.graphqls

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ type Query {
66
}
77

88
type Mutation {
9-
generateCustomerToken(email: String!, password: String!): CustomerToken @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\GenerateCustomerToken") @doc(description:"Retrieve Customer token")
10-
changeCustomerPassword(currentPassword: String!, newPassword: String!): Customer @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\ChangePassword") @doc(description:"Changes password for logged in customer")
11-
updateCustomer (input: UpdateCustomerInput): UpdateCustomerOutput @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\UpdateCustomer") @doc(description:"Update customer personal information")
12-
revokeCustomerToken: Boolean @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\RevokeCustomerToken") @doc(description:"Revoke Customer token")
9+
generateCustomerToken(email: String!, password: String!): CustomerToken @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\GenerateCustomerToken") @doc(description:"Retrieve the customer token")
10+
changeCustomerPassword(currentPassword: String!, newPassword: String!): Customer @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\ChangePassword") @doc(description:"Changes the password for the logged-in customer")
11+
updateCustomer (input: UpdateCustomerInput!): UpdateCustomerOutput @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\UpdateCustomer") @doc(description:"Update the customer's personal information")
12+
revokeCustomerToken: RevokeCustomerTokenOutput @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\RevokeCustomerToken") @doc(description:"Revoke the customer token")
1313
}
1414

1515
type CustomerToken {
@@ -28,6 +28,10 @@ type UpdateCustomerOutput {
2828
customer: Customer!
2929
}
3030

31+
type RevokeCustomerTokenOutput {
32+
result: Boolean!
33+
}
34+
3135
type Customer @doc(description: "Customer defines the customer name and address and other details") {
3236
created_at: String @doc(description: "Timestamp indicating when the account was created")
3337
group_id: Int @doc(description: "The group assigned to the user. Default values are 0 (Not logged in), 1 (General), 2 (Wholesale), and 3 (Retailer)")
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\QuoteGraphQl\Model\Cart\Address;
9+
10+
use Magento\Framework\Api\ExtensibleDataObjectConverter;
11+
use Magento\Quote\Api\Data\AddressInterface;
12+
use Magento\Quote\Api\Data\CartInterface;
13+
use Magento\Quote\Model\Quote\Address as QuoteAddress;
14+
15+
/**
16+
* Class AddressDataProvider
17+
*
18+
* Collect and return information about cart shipping and billing addresses
19+
*/
20+
class AddressDataProvider
21+
{
22+
/**
23+
* @var ExtensibleDataObjectConverter
24+
*/
25+
private $dataObjectConverter;
26+
27+
/**
28+
* AddressDataProvider constructor.
29+
*
30+
* @param ExtensibleDataObjectConverter $dataObjectConverter
31+
*/
32+
public function __construct(
33+
ExtensibleDataObjectConverter $dataObjectConverter
34+
) {
35+
$this->dataObjectConverter = $dataObjectConverter;
36+
}
37+
38+
/**
39+
* Collect and return information about shipping and billing addresses
40+
*
41+
* @param CartInterface $cart
42+
* @return array
43+
*/
44+
public function getCartAddresses(CartInterface $cart): array
45+
{
46+
$addressData = [];
47+
$shippingAddress = $cart->getShippingAddress();
48+
$billingAddress = $cart->getBillingAddress();
49+
50+
if ($shippingAddress) {
51+
$shippingData = $this->dataObjectConverter->toFlatArray($shippingAddress, [], AddressInterface::class);
52+
$shippingData['address_type'] = 'SHIPPING';
53+
$addressData[] = array_merge($shippingData, $this->extractAddressData($shippingAddress));
54+
}
55+
56+
if ($billingAddress) {
57+
$billingData = $this->dataObjectConverter->toFlatArray($billingAddress, [], AddressInterface::class);
58+
$billingData['address_type'] = 'BILLING';
59+
$addressData[] = array_merge($billingData, $this->extractAddressData($billingAddress));
60+
}
61+
62+
return $addressData;
63+
}
64+
65+
/**
66+
* Extract the necessary address fields from address model
67+
*
68+
* @param QuoteAddress $address
69+
* @return array
70+
*/
71+
private function extractAddressData(QuoteAddress $address): array
72+
{
73+
$addressData = [
74+
'country' => [
75+
'code' => $address->getCountryId(),
76+
'label' => $address->getCountry()
77+
],
78+
'region' => [
79+
'code' => $address->getRegionCode(),
80+
'label' => $address->getRegion()
81+
],
82+
'street' => $address->getStreet(),
83+
'selected_shipping_method' => [
84+
'code' => $address->getShippingMethod(),
85+
'label' => $address->getShippingDescription(),
86+
'free_shipping' => $address->getFreeShipping(),
87+
],
88+
'items_weight' => $address->getWeight(),
89+
'customer_notes' => $address->getCustomerNotes()
90+
];
91+
92+
return $addressData;
93+
}
94+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\QuoteGraphQl\Model\Cart;
9+
10+
use Magento\Framework\Exception\InputException;
11+
use Magento\Framework\Exception\NoSuchEntityException;
12+
use Magento\Framework\Exception\StateException;
13+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
14+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
15+
use Magento\Quote\Model\Quote;
16+
use Magento\Quote\Model\Quote\AddressFactory as QuoteAddressFactory;
17+
use Magento\Quote\Model\ResourceModel\Quote\Address as QuoteAddressResource;
18+
use Magento\Checkout\Model\ShippingInformationFactory;
19+
use Magento\Checkout\Api\ShippingInformationManagementInterface;
20+
use Magento\Checkout\Model\ShippingInformation;
21+
22+
/**
23+
* Class SetShippingMethodsOnCart
24+
*
25+
* Set shipping method for a specified shopping cart address
26+
*/
27+
class SetShippingMethodOnCart
28+
{
29+
/**
30+
* @var ShippingInformationFactory
31+
*/
32+
private $shippingInformationFactory;
33+
34+
/**
35+
* @var QuoteAddressFactory
36+
*/
37+
private $quoteAddressFactory;
38+
39+
/**
40+
* @var QuoteAddressResource
41+
*/
42+
private $quoteAddressResource;
43+
44+
/**
45+
* @var ShippingInformationManagementInterface
46+
*/
47+
private $shippingInformationManagement;
48+
49+
/**
50+
* @param ShippingInformationManagementInterface $shippingInformationManagement
51+
* @param QuoteAddressFactory $quoteAddressFactory
52+
* @param QuoteAddressResource $quoteAddressResource
53+
* @param ShippingInformationFactory $shippingInformationFactory
54+
*/
55+
public function __construct(
56+
ShippingInformationManagementInterface $shippingInformationManagement,
57+
QuoteAddressFactory $quoteAddressFactory,
58+
QuoteAddressResource $quoteAddressResource,
59+
ShippingInformationFactory $shippingInformationFactory
60+
) {
61+
$this->shippingInformationManagement = $shippingInformationManagement;
62+
$this->quoteAddressResource = $quoteAddressResource;
63+
$this->quoteAddressFactory = $quoteAddressFactory;
64+
$this->shippingInformationFactory = $shippingInformationFactory;
65+
}
66+
67+
/**
68+
* Sets shipping method for a specified shopping cart address
69+
*
70+
* @param Quote $cart
71+
* @param int $cartAddressId
72+
* @param string $carrierCode
73+
* @param string $methodCode
74+
* @throws GraphQlInputException
75+
* @throws GraphQlNoSuchEntityException
76+
*/
77+
public function execute(Quote $cart, int $cartAddressId, string $carrierCode, string $methodCode): void
78+
{
79+
$quoteAddress = $this->quoteAddressFactory->create();
80+
$this->quoteAddressResource->load($quoteAddress, $cartAddressId);
81+
82+
/** @var ShippingInformation $shippingInformation */
83+
$shippingInformation = $this->shippingInformationFactory->create();
84+
85+
/* If the address is not a shipping address (but billing) the system will find the proper shipping address for
86+
the selected cart and set the information there (actual for single shipping address) */
87+
$shippingInformation->setShippingAddress($quoteAddress);
88+
$shippingInformation->setShippingCarrierCode($carrierCode);
89+
$shippingInformation->setShippingMethodCode($methodCode);
90+
91+
try {
92+
$this->shippingInformationManagement->saveAddressInformation($cart->getId(), $shippingInformation);
93+
} catch (NoSuchEntityException $exception) {
94+
throw new GraphQlNoSuchEntityException(__($exception->getMessage()));
95+
} catch (StateException $exception) {
96+
throw new GraphQlInputException(__($exception->getMessage()));
97+
} catch (InputException $exception) {
98+
throw new GraphQlInputException(__($exception->getMessage()));
99+
}
100+
}
101+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\QuoteGraphQl\Model\Resolver;
9+
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Framework\GraphQl\Config\Element\Field;
12+
use Magento\Framework\GraphQl\Query\ResolverInterface;
13+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
14+
use Magento\QuoteGraphQl\Model\Cart\Address\AddressDataProvider;
15+
16+
/**
17+
* @inheritdoc
18+
*/
19+
class CartAddresses implements ResolverInterface
20+
{
21+
/**
22+
* @var AddressDataProvider
23+
*/
24+
private $addressDataProvider;
25+
26+
/**
27+
* @param AddressDataProvider $addressDataProvider
28+
*/
29+
public function __construct(
30+
AddressDataProvider $addressDataProvider
31+
) {
32+
$this->addressDataProvider = $addressDataProvider;
33+
}
34+
35+
/**
36+
* @inheritdoc
37+
*/
38+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
39+
{
40+
if (!isset($value['model'])) {
41+
throw new LocalizedException(__('"model" value should be specified'));
42+
}
43+
44+
$cart = $value['model'];
45+
46+
return $this->addressDataProvider->getCartAddresses($cart);
47+
}
48+
}

0 commit comments

Comments
 (0)