Skip to content

Commit

Permalink
GraphQL-129: Change resolve to return array, update schema and test
Browse files Browse the repository at this point in the history
  • Loading branch information
pfantini committed Sep 14, 2018
1 parent c0d1b7b commit e97301d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
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
*/
Expand Down Expand Up @@ -54,7 +56,7 @@ public function resolve(
try {
$token = $this->customerTokenService->createCustomerAccessToken($args['email'], $args['password']);
$result = function () use ($token) {
return !empty($token) ? $token : '';
return !empty($token) ? ['token' => $token] : '';
};
return $this->valueFactory->create($result);
} catch (AuthenticationException $e) {
Expand Down
6 changes: 5 additions & 1 deletion app/code/Magento/CustomerGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ type Query {
}

type Mutation {
generateCustomerToken(email: String!, password: String!): String! @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\Customer\\Account\\GenerateCustomerToken") @doc(description:"Retrieve Customer token")
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") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
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
*
Expand All @@ -29,13 +33,15 @@ public function testGenerateCustomerValidToken()
generateCustomerToken(
email: "{$userName}"
password: "{$password}"
)
) {
token
}
}
MUTATION;

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

/**
Expand All @@ -54,7 +60,9 @@ public function testGenerateCustomerTokenWithInvalidCredentials()
generateCustomerToken(
email: "{$userName}"
password: "{$password}"
)
) {
token
}
}
MUTATION;

Expand Down

0 comments on commit e97301d

Please sign in to comment.