Skip to content

Commit

Permalink
GraphQl-129: Retrieve Customer token
Browse files Browse the repository at this point in the history
  • Loading branch information
chrom committed Aug 28, 2018
1 parent 232eec2 commit 527e4fe
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?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\Authorization\Model\UserContextInterface;
use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\Customer\Model\Customer;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
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;

class GenerateCustomerToken implements ResolverInterface
{
/**
* @var UserContextInterface
*/
private $userContext;

/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;

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

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

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

$token = $this->customerTokenService->createCustomerAccessToken($args['email'], $args['password']);
//TODO: exception
$result = function () use ($token) {
return !empty($token) ? $token : '';
};
return $this->valueFactory->create($result);
}
}
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!): GenerateCustomerTokenOutput! @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\Customer\\Account\\GenerateCustomerToken") @doc(description:"Retrieve Customer token")
}

type GenerateCustomerTokenOutput {
token: String! @doc(description: "The 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

0 comments on commit 527e4fe

Please sign in to comment.