Skip to content

Commit 0654c32

Browse files
author
Vitaliy Boyko
committed
graphQl-352: is email available
1 parent f6876dd commit 0654c32

File tree

4 files changed

+152
-0
lines changed

4 files changed

+152
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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\Customer;
9+
10+
use Magento\Customer\Api\AccountManagementInterface;
11+
12+
/**
13+
* Is Customer Email Available checker
14+
*/
15+
class IsEmailAvailableDataProvider
16+
{
17+
/**
18+
* @var AccountManagementInterface
19+
*/
20+
private $accountManagement;
21+
22+
/**
23+
* @param AccountManagementInterface $accountManagement
24+
*/
25+
public function __construct(AccountManagementInterface $accountManagement)
26+
{
27+
$this->accountManagement = $accountManagement;
28+
}
29+
30+
/**
31+
* Check is Email available
32+
*
33+
* @param string $email
34+
* @return bool
35+
*/
36+
public function execute(string $email): bool
37+
{
38+
return $this->accountManagement->isEmailAvailable($email);
39+
}
40+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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\IsEmailAvailableDataProvider;
11+
use Magento\Framework\GraphQl\Config\Element\Field;
12+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
13+
use Magento\Framework\GraphQl\Query\ResolverInterface;
14+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
15+
16+
/**
17+
* Is Customer Email Available
18+
*/
19+
class IsEmailAvailable implements ResolverInterface
20+
{
21+
/**
22+
* @var IsEmailAvailableDataProvider
23+
*/
24+
private $isEmailAvailableDataProvider;
25+
26+
/**
27+
* @param IsEmailAvailableDataProvider $isEmailAvailableDataProvider
28+
*/
29+
public function __construct(
30+
IsEmailAvailableDataProvider $isEmailAvailableDataProvider
31+
) {
32+
$this->isEmailAvailableDataProvider = $isEmailAvailableDataProvider;
33+
}
34+
35+
/**
36+
* @inheritdoc
37+
*/
38+
public function resolve(
39+
Field $field,
40+
$context,
41+
ResolveInfo $info,
42+
array $value = null,
43+
array $args = null
44+
) {
45+
$email = $args['email'] ?? null;
46+
if (!$email) {
47+
throw new GraphQlInputException(__('"Email should be specified'));
48+
}
49+
$isEmailAvailable = $this->isEmailAvailableDataProvider->execute($email);
50+
51+
return [
52+
'is_email_available' => $isEmailAvailable
53+
];
54+
}
55+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
type Query {
55
customer: Customer @resolver(class: "Magento\\CustomerGraphQl\\Model\\Resolver\\Customer") @doc(description: "The customer query returns information about a customer account")
6+
isEmailAvailable (
7+
email: String! @doc(description: "The new customer email")
8+
): IsEmailAvailableOutput @resolver(class: "Magento\\CustomerGraphQl\\Model\\Resolver\\IsEmailAvailable")
69
}
710

811
type Mutation {
@@ -126,6 +129,10 @@ type CustomerAddressAttribute {
126129
value: String @doc(description: "Attribute value")
127130
}
128131

132+
type IsEmailAvailableOutput {
133+
is_email_available: Boolean @doc(description: "Is email availabel value")
134+
}
135+
129136
enum CountryCodeEnum @doc(description: "The list of countries codes") {
130137
AF @doc(description: "Afghanistan")
131138
AX @doc(description: "Åland Islands")
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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\GraphQl\Customer;
9+
10+
use Magento\TestFramework\TestCase\GraphQlAbstract;
11+
12+
class IsEmailAvailableTest extends GraphQlAbstract
13+
{
14+
/**
15+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
16+
*/
17+
public function testEmailNotAvailable()
18+
{
19+
$query =
20+
<<<QUERY
21+
{
22+
isEmailAvailable(email: "[email protected]") {
23+
is_email_available
24+
}
25+
}
26+
QUERY;
27+
$response = $this->graphQlQuery($query);
28+
29+
self::assertArrayHasKey('isEmailAvailable', $response);
30+
self::assertArrayHasKey('is_email_available', $response['isEmailAvailable']);
31+
self::assertFalse($response['isEmailAvailable']['is_email_available']);
32+
}
33+
34+
public function testEmailAvailable()
35+
{
36+
$query =
37+
<<<QUERY
38+
{
39+
isEmailAvailable(email: "[email protected]") {
40+
is_email_available
41+
}
42+
}
43+
QUERY;
44+
$response = $this->graphQlQuery($query);
45+
46+
self::assertArrayHasKey('isEmailAvailable', $response);
47+
self::assertArrayHasKey('is_email_available', $response['isEmailAvailable']);
48+
self::assertTrue($response['isEmailAvailable']['is_email_available']);
49+
}
50+
}

0 commit comments

Comments
 (0)