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

GraphQL-129: Retrieve Customer token #180

Merged
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,68 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CustomerGraphQl\Model\Resolver\Customer\Account;

use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\Exception\AuthenticationException;
use Magento\Framework\GraphQl\Query\Resolver\Value;
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;

/**
* Customers Token resolver, used for GraphQL request processing.
*/
class GenerateCustomerToken implements ResolverInterface
{
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;

/**
* @var ValueFactory
*/
private $valueFactory;

/**
* @param CustomerTokenServiceInterface $customerTokenService
* @param ValueFactory $valueFactory
*/
public function __construct(
CustomerTokenServiceInterface $customerTokenService,
ValueFactory $valueFactory
) {
$this->customerTokenService = $customerTokenService;
$this->valueFactory = $valueFactory;
}

/**
* @inheritdoc
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
): Value {
Copy link
Contributor

Choose a reason for hiding this comment

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

Pls, merge mainline
Magento\Framework\GraphQl\Query\ResolverInterface was changed
In this case we need to return string value

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If I change returned value to string or array the test Magento\GraphQl\IntrospectionQueryTest::testIntrospectionQueryWithFieldArgs fails

try {
$token = $this->customerTokenService->createCustomerAccessToken($args['email'], $args['password']);
$result = function () use ($token) {
return !empty($token) ? ['token' => $token] : '';
};
return $this->valueFactory->create($result);
} catch (AuthenticationException $e) {
throw new GraphQlAuthorizationException(
__($e->getMessage())
);
}
}
}
1 change: 1 addition & 0 deletions app/code/Magento/CustomerGraphQl/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"php": "~7.1.3||~7.2.0",
"magento/module-customer": "*",
"magento/module-authorization": "*",
"magento/module-integration": "*",
"magento/framework": "*"
},
"suggest": {
Expand Down
8 changes: 8 additions & 0 deletions app/code/Magento/CustomerGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ type Query {
customer: Customer @resolver(class: "Magento\\CustomerGraphQl\\Model\\Resolver\\Customer") @doc(description: "The customer query returns information about a customer account")
}

type Mutation {
generateCustomerToken(email: String!, password: String!): CustomerToken @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\Customer\\Account\\GenerateCustomerToken") @doc(description:"Retrieve Customer token")
}

type CustomerToken {
token: String @doc(description: "The new customer token")
}

type Customer @doc(description: "Customer defines the customer name and address and other details") {
created_at: String @doc(description: "Timestamp indicating when the account was created")
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)")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?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;
use PHPUnit\Framework\TestResult;

/**
* Class GenerateCustomerTokenTest
* @package Magento\GraphQl\Customer
*/
class GenerateCustomerTokenTest extends GraphQlAbstract
{
/**
* Verify customer token with valid credentials
*
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function testGenerateCustomerValidToken()
{
$userName = 'customer@example.com';
$password = 'password';

$mutation
= <<<MUTATION
mutation {
generateCustomerToken(
email: "{$userName}"
password: "{$password}"
) {
token
}
}
MUTATION;

$response = $this->graphQlQuery($mutation);
$this->assertArrayHasKey('generateCustomerToken', $response);
$this->assertInternalType('array', $response['generateCustomerToken']);
}

/**
* Verify customer with invalid credentials
*
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function testGenerateCustomerTokenWithInvalidCredentials()
{
$userName = 'customer@example.com';
$password = 'bad-password';

$mutation
= <<<MUTATION
mutation {
generateCustomerToken(
email: "{$userName}"
password: "{$password}"
) {
token
}
}
MUTATION;

$this->expectException(\Exception::class);
$this->expectExceptionMessage('GraphQL response contains errors: The account sign-in' . ' ' .
'was incorrect or your account is disabled temporarily. Please wait and try again later.');
$this->graphQlQuery($mutation);
}
}