Skip to content

Commit cb4e788

Browse files
ENGCOM-7839: createCustomer validation requirements #28888
- Merge Pull Request #28888 from michalderlatka/magento2:28570_createcustomer_graphql_schema - Merged commits: 1. 1943018 2. 887f191 3. b5208cd 4. 0f7d805 5. 592f5de 6. 3a4cfa1 7. 2e77e22
2 parents 1d8a6d7 + 2e77e22 commit cb4e788

File tree

6 files changed

+963
-3
lines changed

6 files changed

+963
-3
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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\CustomerGraphQl\Model\Resolver;
9+
10+
use Magento\CustomerGraphQl\Model\Customer\ExtractCustomerData;
11+
use Magento\CustomerGraphQl\Model\Customer\GetCustomer;
12+
use Magento\CustomerGraphQl\Model\Customer\UpdateCustomerAccount;
13+
use Magento\Framework\GraphQl\Config\Element\Field;
14+
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
15+
use Magento\Framework\GraphQl\Query\Resolver\Value;
16+
use Magento\Framework\GraphQl\Query\ResolverInterface;
17+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
18+
use Magento\GraphQl\Model\Query\ContextInterface;
19+
20+
/**
21+
* Customer email update, used for GraphQL request processing
22+
*/
23+
class UpdateCustomerEmail implements ResolverInterface
24+
{
25+
/**
26+
* @var GetCustomer
27+
*/
28+
private $getCustomer;
29+
30+
/**
31+
* @var UpdateCustomerAccount
32+
*/
33+
private $updateCustomerAccount;
34+
35+
/**
36+
* @var ExtractCustomerData
37+
*/
38+
private $extractCustomerData;
39+
40+
/**
41+
* @param GetCustomer $getCustomer
42+
* @param UpdateCustomerAccount $updateCustomerAccount
43+
* @param ExtractCustomerData $extractCustomerData
44+
*/
45+
public function __construct(
46+
GetCustomer $getCustomer,
47+
UpdateCustomerAccount $updateCustomerAccount,
48+
ExtractCustomerData $extractCustomerData
49+
) {
50+
$this->getCustomer = $getCustomer;
51+
$this->updateCustomerAccount = $updateCustomerAccount;
52+
$this->extractCustomerData = $extractCustomerData;
53+
}
54+
55+
/**
56+
* Resolve customer email update mutation
57+
*
58+
* @param \Magento\Framework\GraphQl\Config\Element\Field $field
59+
* @param \Magento\Framework\GraphQl\Query\Resolver\ContextInterface $context
60+
* @param ResolveInfo $info
61+
* @param array|null $value
62+
* @param array|null $args
63+
* @return array|Value
64+
* @throws \Exception
65+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
66+
*/
67+
public function resolve(
68+
Field $field,
69+
$context,
70+
ResolveInfo $info,
71+
array $value = null,
72+
array $args = null
73+
) {
74+
/** @var ContextInterface $context */
75+
if (false === $context->getExtensionAttributes()->getIsCustomer()) {
76+
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
77+
}
78+
79+
$customer = $this->getCustomer->execute($context);
80+
$this->updateCustomerAccount->execute(
81+
$customer,
82+
[
83+
'email' => $args['email'] ?? null,
84+
'password' => $args['password'] ?? null
85+
],
86+
$context->getExtensionAttributes()->getStore()
87+
);
88+
89+
$data = $this->extractCustomerData->execute($customer);
90+
91+
return ['customer' => $data];
92+
}
93+
}

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@ type Mutation {
1818
generateCustomerToken(email: String!, password: String!): CustomerToken @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\GenerateCustomerToken") @doc(description:"Retrieve the customer token")
1919
changeCustomerPassword(currentPassword: String!, newPassword: String!): Customer @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\ChangePassword") @doc(description:"Changes the password for the logged-in customer")
2020
createCustomer (input: CustomerInput!): CustomerOutput @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\CreateCustomer") @doc(description:"Create customer account")
21-
updateCustomer (input: CustomerInput!): CustomerOutput @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\UpdateCustomer") @doc(description:"Update the customer's personal information")
21+
createCustomerV2 (input: CustomerCreateInput!): CustomerOutput @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\CreateCustomer") @doc(description:"Create customer account")
22+
updateCustomer (input: CustomerInput!): CustomerOutput @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\UpdateCustomer") @doc(description:"Deprecated. Use UpdateCustomerV2 instead.")
23+
updateCustomerV2 (input: CustomerUpdateInput!): CustomerOutput @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\UpdateCustomer") @doc(description:"Update the customer's personal information")
2224
revokeCustomerToken: RevokeCustomerTokenOutput @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\RevokeCustomerToken") @doc(description:"Revoke the customer token")
2325
createCustomerAddress(input: CustomerAddressInput!): CustomerAddress @resolver(class: "Magento\\CustomerGraphQl\\Model\\Resolver\\CreateCustomerAddress") @doc(description: "Create customer address")
2426
updateCustomerAddress(id: Int!, input: CustomerAddressInput): CustomerAddress @resolver(class: "Magento\\CustomerGraphQl\\Model\\Resolver\\UpdateCustomerAddress") @doc(description: "Update customer address")
2527
deleteCustomerAddress(id: Int!): Boolean @resolver(class: "Magento\\CustomerGraphQl\\Model\\Resolver\\DeleteCustomerAddress") @doc(description: "Delete customer address")
2628
requestPasswordResetEmail(email: String!): Boolean @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\RequestPasswordResetEmail") @doc(description: "Request an email with a reset password token for the registered customer identified by the specified email.")
2729
resetPassword(email: String!, resetPasswordToken: String!, newPassword: String!): Boolean @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\ResetPassword") @doc(description: "Reset a customer's password using the reset password token that the customer received in an email after requesting it using requestPasswordResetEmail.")
30+
updateCustomerEmail(email: String!, password: String!): CustomerOutput @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\UpdateCustomerEmail") @doc(description: "")
2831
}
2932

3033
input CustomerAddressInput {
@@ -78,6 +81,34 @@ input CustomerInput {
7881
is_subscribed: Boolean @doc(description: "Indicates whether the customer is subscribed to the company's newsletter")
7982
}
8083

84+
input CustomerCreateInput {
85+
prefix: String @doc(description: "An honorific, such as Dr., Mr., or Mrs.")
86+
firstname: String! @doc(description: "The customer's first name")
87+
middlename: String @doc(description: "The customer's middle name")
88+
lastname: String! @doc(description: "The customer's family name")
89+
suffix: String @doc(description: "A value such as Sr., Jr., or III")
90+
email: String! @doc(description: "The customer's email address. Required for customer creation")
91+
dob: String @doc(description: "Deprecated: Use `date_of_birth` instead")
92+
date_of_birth: String @doc(description: "The customer's date of birth")
93+
taxvat: String @doc(description: "The customer's Tax/VAT number (for corporate customers)")
94+
gender: Int @doc(description: "The customer's gender (Male - 1, Female - 2)")
95+
password: String @doc(description: "The customer's password")
96+
is_subscribed: Boolean @doc(description: "Indicates whether the customer is subscribed to the company's newsletter")
97+
}
98+
99+
input CustomerUpdateInput {
100+
date_of_birth: String @doc(description: "The customer's date of birth")
101+
dob: String @doc(description: "Deprecated: Use `date_of_birth` instead")
102+
firstname: String @doc(description: "The customer's first name")
103+
gender: Int @doc(description: "The customer's gender (Male - 1, Female - 2)")
104+
is_subscribed: Boolean @doc(description: "Indicates whether the customer is subscribed to the company's newsletter")
105+
lastname: String @doc(description: "The customer's family name")
106+
middlename: String @doc(description: "The customer's middle name")
107+
prefix: String @doc(description: "An honorific, such as Dr., Mr., or Mrs.")
108+
suffix: String @doc(description: "A value such as Sr., Jr., or III")
109+
taxvat: String @doc(description: "The customer's Tax/VAT number (for corporate customers)")
110+
}
111+
81112
type CustomerOutput {
82113
customer: Customer!
83114
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/CreateCustomerTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public function testCreateCustomerIfInputDataIsEmpty()
123123
mutation {
124124
createCustomer(
125125
input: {
126-
126+
127127
}
128128
) {
129129
customer {
@@ -339,7 +339,9 @@ public function testCreateCustomerSubscribed()
339339
public function testCreateCustomerIfCustomerWithProvidedEmailAlreadyExists()
340340
{
341341
$this->expectException(\Exception::class);
342-
$this->expectExceptionMessage('A customer with the same email address already exists in an associated website.');
342+
$this->expectExceptionMessage(
343+
'A customer with the same email address already exists in an associated website.'
344+
);
343345

344346
$existedEmail = '[email protected]';
345347
$password = 'test123#';

0 commit comments

Comments
 (0)