Skip to content

Commit

Permalink
ENGCOM-5288: GraphQL-741: Add extension point to set custom parameter…
Browse files Browse the repository at this point in the history
…s to Query Context object #742

 - Merge Pull Request magento/graphql-ce#742 from magento/graphql-ce:magento/graphql-ce#741
 - Merged commits:
   1. 3096057
   2. e514ce0
   3. 5d12f8e
   4. ed35766
   5. ac05279
   6. 8831221
   7. 13ea1f8
   8. bb19773
   9. 7b3024a
   10. 892c131
   11. 4485e23
   12. 785ab2b
   13. 4788d35
   14. 87296c1
   15. 0d505fe
   16. 28aaefa
  • Loading branch information
magento-engcom-team committed Jun 19, 2019
2 parents 883a4b1 + 28aaefa commit 6cdc2b2
Show file tree
Hide file tree
Showing 23 changed files with 499 additions and 134 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CustomerGraphQl\Model\Context;

use Magento\Authorization\Model\UserContextInterface;
use Magento\GraphQl\Model\Query\ContextParametersInterface;
use Magento\GraphQl\Model\Query\ContextParametersProcessorInterface;

/**
* @inheritdoc
*/
class AddUserInfoToContext implements ContextParametersProcessorInterface
{
/**
* @var UserContextInterface
*/
private $userContext;

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

/**
* @inheritdoc
*/
public function execute(ContextParametersInterface $contextParameters): ContextParametersInterface
{
$currentUserId = $this->userContext->getUserId();
if (null !== $currentUserId) {
$currentUserId = (int)$currentUserId;
}

$currentUserType = $this->userContext->getUserType();
if (null !== $currentUserType) {
$currentUserType = (int)$currentUserType;
}

$contextParameters->setUserId($currentUserId);
$contextParameters->setUserType($currentUserType);
return $contextParameters;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Magento\Framework\Exception\AlreadyExistsException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Exception\GraphQlAlreadyExistsException;
use Magento\Framework\GraphQl\Exception\GraphQlAuthenticationException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Customer\Api\Data\CustomerInterface;

Expand Down Expand Up @@ -39,7 +38,6 @@ public function __construct(
*
* @param CustomerInterface $customer
* @throws GraphQlAlreadyExistsException
* @throws GraphQlAuthenticationException
* @throws GraphQlInputException
*/
public function execute(CustomerInterface $customer): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace Magento\CustomerGraphQl\Model\Resolver;

use Magento\Authorization\Model\UserContextInterface;
use Magento\CustomerGraphQl\Model\Customer\CreateCustomerAccount;
use Magento\CustomerGraphQl\Model\Customer\ExtractCustomerData;
use Magento\Framework\GraphQl\Config\Element\Field;
Expand Down Expand Up @@ -58,9 +57,6 @@ public function resolve(

$customer = $this->createCustomerAccount->execute($args['input']);

$context->setUserId((int)$customer->getId());
$context->setUserType(UserContextInterface::USER_TYPE_CUSTOMER);

$data = $this->extractCustomerData->execute($customer);
return ['customer' => $data];
}
Expand Down
16 changes: 6 additions & 10 deletions app/code/Magento/CustomerGraphQl/Model/Resolver/IsSubscribed.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Magento\CustomerGraphQl\Model\Resolver;

use Magento\CustomerGraphQl\Model\Customer\GetCustomer;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
Expand All @@ -18,25 +18,17 @@
*/
class IsSubscribed implements ResolverInterface
{
/**
* @var GetCustomer
*/
private $getCustomer;

/**
* @var SubscriberFactory
*/
private $subscriberFactory;

/**
* @param GetCustomer $getCustomer
* @param SubscriberFactory $subscriberFactory
*/
public function __construct(
GetCustomer $getCustomer,
SubscriberFactory $subscriberFactory
) {
$this->getCustomer = $getCustomer;
$this->subscriberFactory = $subscriberFactory;
}

Expand All @@ -50,7 +42,11 @@ public function resolve(
array $value = null,
array $args = null
) {
$customer = $this->getCustomer->execute($context);
if (!isset($value['model'])) {
throw new LocalizedException(__('"model" value should be specified'));
}
/** @var Customer $customer */
$customer = $value['model'];

$status = $this->subscriberFactory->create()->loadByCustomerId((int)$customer->getId())->isSubscribed();
return (bool)$status;
Expand Down
6 changes: 2 additions & 4 deletions app/code/Magento/CustomerGraphQl/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@
"type": "magento2-module",
"require": {
"php": "~7.1.3||~7.2.0",
"magento/module-customer": "*",
"magento/module-authorization": "*",
"magento/module-customer": "*",
"magento/module-eav": "*",
"magento/module-graph-ql": "*",
"magento/module-newsletter": "*",
"magento/module-integration": "*",
"magento/module-store": "*",
"magento/framework": "*"
},
"suggest": {
"magento/module-graph-ql": "*"
},
"license": [
"OSL-3.0",
"AFL-3.0"
Expand Down
12 changes: 12 additions & 0 deletions app/code/Magento/CustomerGraphQl/etc/extension_attributes.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
<extension_attributes for="Magento\GraphQl\Model\Query\ContextInterface">
<attribute code="customer" type="Magento\Customer\Api\Data\CustomerInterface"/>
</extension_attributes>
</config>
9 changes: 8 additions & 1 deletion app/code/Magento/CustomerGraphQl/etc/graphql/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,11 @@
</argument>
</arguments>
</type>
</config>
<type name="Magento\GraphQl\Model\Query\ContextFactory">
<arguments>
<argument name="contextParametersProcessors" xsi:type="array">
<item name="add_user_info_to_context" xsi:type="object">Magento\CustomerGraphQl\Model\Context\AddUserInfoToContext</item>
</argument>
</arguments>
</type>
</config>
16 changes: 13 additions & 3 deletions app/code/Magento/GraphQl/Controller/GraphQl.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Magento\Framework\GraphQl\Query\Fields as QueryFields;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\App\ObjectManager;
use Magento\GraphQl\Model\Query\ContextFactoryInterface;

/**
* Front controller for web API GraphQL area.
Expand Down Expand Up @@ -58,6 +59,7 @@ class GraphQl implements FrontControllerInterface

/**
* @var ContextInterface
* @deprecated $contextFactory is used for creating Context object
*/
private $resolverContext;

Expand All @@ -81,17 +83,23 @@ class GraphQl implements FrontControllerInterface
*/
private $httpResponse;

/**
* @var ContextFactoryInterface
*/
private $contextFactory;

/**
* @param Response $response
* @param SchemaGeneratorInterface $schemaGenerator
* @param SerializerInterface $jsonSerializer
* @param QueryProcessor $queryProcessor
* @param ExceptionFormatter $graphQlError
* @param ContextInterface $resolverContext
* @param ContextInterface $resolverContext Deprecated. $contextFactory is used for creating Context object.
* @param HttpRequestProcessor $requestProcessor
* @param QueryFields $queryFields
* @param JsonFactory|null $jsonFactory
* @param HttpResponse|null $httpResponse
* @param ContextFactoryInterface $contextFactory
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
Expand All @@ -104,7 +112,8 @@ public function __construct(
HttpRequestProcessor $requestProcessor,
QueryFields $queryFields,
JsonFactory $jsonFactory = null,
HttpResponse $httpResponse = null
HttpResponse $httpResponse = null,
ContextFactoryInterface $contextFactory = null
) {
$this->response = $response;
$this->schemaGenerator = $schemaGenerator;
Expand All @@ -116,6 +125,7 @@ public function __construct(
$this->queryFields = $queryFields;
$this->jsonFactory = $jsonFactory ?: ObjectManager::getInstance()->get(JsonFactory::class);
$this->httpResponse = $httpResponse ?: ObjectManager::getInstance()->get(HttpResponse::class);
$this->contextFactory = $contextFactory ?: ObjectManager::getInstance()->get(ContextFactoryInterface::class);
}

/**
Expand Down Expand Up @@ -144,7 +154,7 @@ public function dispatch(RequestInterface $request) : ResponseInterface
$result = $this->queryProcessor->process(
$schema,
$query,
$this->resolverContext,
$this->contextFactory->create(),
$data['variables'] ?? []
);
} catch (\Exception $error) {
Expand Down
71 changes: 71 additions & 0 deletions app/code/Magento/GraphQl/Model/Query/Context.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GraphQl\Model\Query;

/**
* Concrete implementation for @see ContextInterface
*
* The purpose for this that GraphQL specification wants to make use of such object where multiple modules can
* participate with data through extension attributes.
*/
class Context implements ContextInterface
{
/**
* @var int|null
*/
private $userType;

/**
* @var int|null
*/
private $userId;

/**
* @var ContextExtensionInterface
*/
private $extensionAttributes;

/**
* @param int|null $userType
* @param int|null $userId
* @param ContextExtensionInterface $extensionAttributes
*/
public function __construct(
?int $userType,
?int $userId,
ContextExtensionInterface $extensionAttributes
) {
$this->userType = $userType;
$this->userId = $userId;
$this->extensionAttributes = $extensionAttributes;
}

/**
* @inheritdoc
*/
public function getUserType(): ?int
{
return $this->userType;
}

/**
* @inheritdoc
*/
public function getUserId(): ?int
{
return $this->userId;
}

/**
* @inheritdoc
*/
public function getExtensionAttributes(): ContextExtensionInterface
{
return $this->extensionAttributes;
}
}
Loading

0 comments on commit 6cdc2b2

Please sign in to comment.