Skip to content
This repository was archived by the owner on Dec 19, 2019. It is now read-only.

graphQl-352: is email available #356

Merged
merged 3 commits into from
Feb 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CustomerGraphQl\Model\Resolver;

use Magento\Customer\Api\AccountManagementInterface;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;

/**
* Is Customer Email Available
*/
class IsEmailAvailable implements ResolverInterface
{
/**
* @var AccountManagementInterface
*/
private $accountManagement;

/**
* @param AccountManagementInterface $accountManagement
*/
public function __construct(
AccountManagementInterface $accountManagement
) {
$this->accountManagement = $accountManagement;
}

/**
* @inheritdoc
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) {
$email = $args['email'] ?? null;
if (!$email) {
throw new GraphQlInputException(__('"Email should be specified'));
}
$isEmailAvailable = $this->accountManagement->isEmailAvailable($email);

return [
'is_email_available' => $isEmailAvailable
];
}
}
7 changes: 7 additions & 0 deletions app/code/Magento/CustomerGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

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

type Mutation {
Expand Down Expand Up @@ -126,6 +129,10 @@ type CustomerAddressAttribute {
value: String @doc(description: "Attribute value")
}

type IsEmailAvailableOutput {
is_email_available: Boolean @doc(description: "Is email availabel value")
}

enum CountryCodeEnum @doc(description: "The list of countries codes") {
AF @doc(description: "Afghanistan")
AX @doc(description: "Åland Islands")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GraphQl\Customer;

use Magento\TestFramework\TestCase\GraphQlAbstract;

class IsEmailAvailableTest extends GraphQlAbstract
{
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*/
public function testEmailNotAvailable()
{
$query =
<<<QUERY
{
isEmailAvailable(email: "[email protected]") {
is_email_available
}
}
QUERY;
$response = $this->graphQlQuery($query);

self::assertArrayHasKey('isEmailAvailable', $response);
self::assertArrayHasKey('is_email_available', $response['isEmailAvailable']);
self::assertFalse($response['isEmailAvailable']['is_email_available']);
}

public function testEmailAvailable()
{
$query =
<<<QUERY
{
isEmailAvailable(email: "[email protected]") {
is_email_available
}
}
QUERY;
$response = $this->graphQlQuery($query);

self::assertArrayHasKey('isEmailAvailable', $response);
self::assertArrayHasKey('is_email_available', $response['isEmailAvailable']);
self::assertTrue($response['isEmailAvailable']['is_email_available']);
}
}