Skip to content

Commit

Permalink
magento/graphql-ce#741: Add extension point to set custom parameters …
Browse files Browse the repository at this point in the history
…to Query Context object
  • Loading branch information
naydav committed Jun 19, 2019
1 parent 87296c1 commit 0d505fe
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 144 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,13 @@ class AddUserInfoToContext implements ContextParametersProcessorInterface
*/
private $userContext;

/**
* @var GetCustomer
*/
private $getCustomer;

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

/**
Expand All @@ -53,28 +45,8 @@ public function execute(ContextParametersInterface $contextParameters): ContextP
$currentUserType = (int)$currentUserType;
}

if (false === $this->isUserGuest($currentUserId, $currentUserType)) {
$customer = $this->getCustomer->execute($currentUserId);
$contextParameters->addExtensionAttribute('customer', $customer);
}

$contextParameters->setUserId($currentUserId);
$contextParameters->setUserType($currentUserType);
return $contextParameters;
}

/**
* Checking if current customer is guest
*
* @param int|null $customerId
* @param int|null $customerType
* @return bool
*/
private function isUserGuest(?int $customerId, ?int $customerType): bool
{
if (null === $customerId || null === $customerType) {
return true;
}
return 0 === (int)$customerId || (int)$customerType === UserContextInterface::USER_TYPE_GUEST;
}
}
94 changes: 0 additions & 94 deletions app/code/Magento/CustomerGraphQl/Model/Context/GetCustomer.php

This file was deleted.

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\CustomerGraphQl\Model\Customer\GetCustomer;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\GraphQl\Config\Element\Field;
Expand All @@ -19,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 Down
22 changes: 16 additions & 6 deletions app/code/Magento/GraphQl/Controller/GraphQl.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\GraphQl\Exception\ExceptionFormatter;
use Magento\Framework\GraphQl\Query\QueryProcessor;
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
use Magento\Framework\GraphQl\Schema\SchemaGeneratorInterface;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Framework\Webapi\Response;
Expand Down Expand Up @@ -57,9 +58,10 @@ class GraphQl implements FrontControllerInterface
private $graphQlError;

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

/**
* @var HttpRequestProcessor
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 ContextFactoryInterface $contextFactory
* @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 @@ -100,22 +108,24 @@ public function __construct(
SerializerInterface $jsonSerializer,
QueryProcessor $queryProcessor,
ExceptionFormatter $graphQlError,
ContextFactoryInterface $contextFactory,
ContextInterface $resolverContext,
HttpRequestProcessor $requestProcessor,
QueryFields $queryFields,
JsonFactory $jsonFactory = null,
HttpResponse $httpResponse = null
HttpResponse $httpResponse = null,
ContextFactoryInterface $contextFactory = null
) {
$this->response = $response;
$this->schemaGenerator = $schemaGenerator;
$this->jsonSerializer = $jsonSerializer;
$this->queryProcessor = $queryProcessor;
$this->graphQlError = $graphQlError;
$this->contextFactory = $contextFactory;
$this->resolverContext = $resolverContext;
$this->requestProcessor = $requestProcessor;
$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
6 changes: 4 additions & 2 deletions app/code/Magento/GraphQl/Model/Query/ContextFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ContextFactory implements ContextFactoryInterface
/**
* @var ExtensionAttributesFactory
*/
protected $extensionAttributesFactory;
private $extensionAttributesFactory;

/**
* @var ObjectManagerInterface
Expand Down Expand Up @@ -64,7 +64,9 @@ public function create(): ContextInterface

$extensionAttributes = $this->extensionAttributesFactory->create(
ContextInterface::class,
$contextParameters->getExtensionAttributesData()
[
'data' => $contextParameters->getExtensionAttributesData(),
]
);

$context = $this->objectManager->create(
Expand Down
1 change: 1 addition & 0 deletions app/code/Magento/GraphQl/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Framework\GraphQl\Schema\SchemaGeneratorInterface" type="Magento\Framework\GraphQl\Schema\SchemaGenerator" />
<preference for="Magento\Framework\GraphQl\Query\Resolver\ContextInterface" type="Magento\GraphQl\Model\Query\Resolver\Context" />
<preference for="Magento\GraphQl\Model\Query\ContextInterface" type="Magento\GraphQl\Model\Query\Context" />
<preference for="Magento\Framework\GraphQl\Schema\Type\Entity\MapperInterface" type="Magento\Framework\GraphQl\Schema\Type\Entity\DefaultMapper"/>
<preference for="Magento\Framework\GraphQl\Schema\Type\Enum\DataMapperInterface" type="Magento\Framework\GraphQl\Schema\Type\Enum\DefaultDataMapper"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public function __construct(
* @return Quote
* @throws GraphQlAuthorizationException
* @throws GraphQlNoSuchEntityException
* @throws NoSuchEntityException
*/
public function execute(string $cartHash, ?int $customerId): Quote
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ public function createBasedOnInputData(array $addressInput): QuoteAddress
* @param int $customerAddressId
* @param int $customerId
* @return QuoteAddress
* @throws GraphQlInputException
* @throws GraphQlAuthorizationException
* @throws GraphQlInputException
* @throws GraphQlNoSuchEntityException
*/
public function createBasedOnCustomerAddress(int $customerAddressId, int $customerId): QuoteAddress
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ interface SetShippingAddressesOnCartInterface
* @return void
* @throws GraphQlInputException
* @throws GraphQlAuthorizationException
* @throws GraphQlAuthenticationException
* @throws GraphQlNoSuchEntityException
*/
public function execute(ContextInterface $context, CartInterface $cart, array $shippingAddressesInput): void;
Expand Down

0 comments on commit 0d505fe

Please sign in to comment.