From 3096057a8cf057590e54d5d85e07a7d338029f2f Mon Sep 17 00:00:00 2001 From: Valerii Naida Date: Thu, 13 Jun 2019 15:11:59 -0500 Subject: [PATCH 01/12] magento/graphql-ce#741: Add extension point to set custom parameters to Query Context object --- .../Model/Context/AddUserInfoToContext.php | 80 +++++++++++ .../Model/Context/GetCustomer.php | 94 +++++++++++++ .../CustomerGraphQl/etc/graphql/di.xml | 7 + .../Magento/GraphQl/Controller/GraphQl.php | 14 +- .../Magento/GraphQl/Model/Query/Context.php | 72 ++++++++++ .../GraphQl/Model/Query/ContextFactory.php | 77 +++++++++++ .../Model/Query/ContextFactoryInterface.php | 21 +++ .../GraphQl/Model/Query/ContextInterface.php | 42 ++++++ .../GraphQl/Model/Query/ContextParameters.php | 77 +++++++++++ .../Query/ContextParametersInterface.php | 60 +++++++++ .../ContextParametersProcessorInterface.php | 22 +++ .../GraphQl/Model/Query/Resolver/Context.php | 125 ------------------ app/code/Magento/GraphQl/etc/di.xml | 4 +- .../Query/Resolver/ContextInterface.php | 53 +------- 14 files changed, 563 insertions(+), 185 deletions(-) create mode 100644 app/code/Magento/CustomerGraphQl/Model/Context/AddUserInfoToContext.php create mode 100644 app/code/Magento/CustomerGraphQl/Model/Context/GetCustomer.php create mode 100644 app/code/Magento/GraphQl/Model/Query/Context.php create mode 100644 app/code/Magento/GraphQl/Model/Query/ContextFactory.php create mode 100644 app/code/Magento/GraphQl/Model/Query/ContextFactoryInterface.php create mode 100644 app/code/Magento/GraphQl/Model/Query/ContextInterface.php create mode 100644 app/code/Magento/GraphQl/Model/Query/ContextParameters.php create mode 100644 app/code/Magento/GraphQl/Model/Query/ContextParametersInterface.php create mode 100644 app/code/Magento/GraphQl/Model/Query/ContextParametersProcessorInterface.php delete mode 100644 app/code/Magento/GraphQl/Model/Query/Resolver/Context.php diff --git a/app/code/Magento/CustomerGraphQl/Model/Context/AddUserInfoToContext.php b/app/code/Magento/CustomerGraphQl/Model/Context/AddUserInfoToContext.php new file mode 100644 index 000000000000..a871d7f62ba0 --- /dev/null +++ b/app/code/Magento/CustomerGraphQl/Model/Context/AddUserInfoToContext.php @@ -0,0 +1,80 @@ +userContext = $userContext; + $this->getCustomer = $getCustomer; + } + + /** + * @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; + } + + 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; + } +} diff --git a/app/code/Magento/CustomerGraphQl/Model/Context/GetCustomer.php b/app/code/Magento/CustomerGraphQl/Model/Context/GetCustomer.php new file mode 100644 index 000000000000..f680f1516889 --- /dev/null +++ b/app/code/Magento/CustomerGraphQl/Model/Context/GetCustomer.php @@ -0,0 +1,94 @@ +authentication = $authentication; + $this->customerRepository = $customerRepository; + $this->accountManagement = $accountManagement; + } + + /** + * Get customer + * + * @param int $customerId + * @return void + * @throws GraphQlAuthenticationException + * @throws GraphQlAuthorizationException + * @throws GraphQlInputException + * @throws GraphQlNoSuchEntityException + */ + public function execute(int $customerId): CustomerInterface + { + try { + $customer = $this->customerRepository->getById($customerId); + } catch (NoSuchEntityException $e) { + throw new GraphQlNoSuchEntityException( + __('Customer with id "%customer_id" does not exist.', ['customer_id' => $customerId]), + $e + ); + } catch (LocalizedException $e) { + throw new GraphQlInputException(__($e->getMessage())); + } + + if (true === $this->authentication->isLocked($customerId)) { + throw new GraphQlAuthenticationException(__('The account is locked.')); + } + + try { + $confirmationStatus = $this->accountManagement->getConfirmationStatus($customerId); + } catch (LocalizedException $e) { + throw new GraphQlInputException(__($e->getMessage())); + } + + if ($confirmationStatus === AccountManagementInterface::ACCOUNT_CONFIRMATION_REQUIRED) { + throw new GraphQlAuthenticationException(__("This account isn't confirmed. Verify and try again.")); + } + return $customer; + } +} diff --git a/app/code/Magento/CustomerGraphQl/etc/graphql/di.xml b/app/code/Magento/CustomerGraphQl/etc/graphql/di.xml index f1bd3563fda3..15e0f080b41f 100644 --- a/app/code/Magento/CustomerGraphQl/etc/graphql/di.xml +++ b/app/code/Magento/CustomerGraphQl/etc/graphql/di.xml @@ -13,4 +13,11 @@ + + + + Magento\CustomerGraphQl\Model\Context\AddUserInfoToContext + + + \ No newline at end of file diff --git a/app/code/Magento/GraphQl/Controller/GraphQl.php b/app/code/Magento/GraphQl/Controller/GraphQl.php index 75b3ad277c60..c0d3817ee682 100644 --- a/app/code/Magento/GraphQl/Controller/GraphQl.php +++ b/app/code/Magento/GraphQl/Controller/GraphQl.php @@ -13,7 +13,6 @@ 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; @@ -21,6 +20,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. @@ -57,9 +57,9 @@ class GraphQl implements FrontControllerInterface private $graphQlError; /** - * @var ContextInterface + * @var ContextFactoryInterface */ - private $resolverContext; + private $contextFactory; /** * @var HttpRequestProcessor @@ -87,7 +87,7 @@ class GraphQl implements FrontControllerInterface * @param SerializerInterface $jsonSerializer * @param QueryProcessor $queryProcessor * @param ExceptionFormatter $graphQlError - * @param ContextInterface $resolverContext + * @param ContextFactoryInterface $contextFactory * @param HttpRequestProcessor $requestProcessor * @param QueryFields $queryFields * @param JsonFactory|null $jsonFactory @@ -100,7 +100,7 @@ public function __construct( SerializerInterface $jsonSerializer, QueryProcessor $queryProcessor, ExceptionFormatter $graphQlError, - ContextInterface $resolverContext, + ContextFactoryInterface $contextFactory, HttpRequestProcessor $requestProcessor, QueryFields $queryFields, JsonFactory $jsonFactory = null, @@ -111,7 +111,7 @@ public function __construct( $this->jsonSerializer = $jsonSerializer; $this->queryProcessor = $queryProcessor; $this->graphQlError = $graphQlError; - $this->resolverContext = $resolverContext; + $this->contextFactory = $contextFactory; $this->requestProcessor = $requestProcessor; $this->queryFields = $queryFields; $this->jsonFactory = $jsonFactory ?: ObjectManager::getInstance()->get(JsonFactory::class); @@ -144,7 +144,7 @@ public function dispatch(RequestInterface $request) : ResponseInterface $result = $this->queryProcessor->process( $schema, $query, - $this->resolverContext, + $this->contextFactory->create(), $data['variables'] ?? [] ); } catch (\Exception $error) { diff --git a/app/code/Magento/GraphQl/Model/Query/Context.php b/app/code/Magento/GraphQl/Model/Query/Context.php new file mode 100644 index 000000000000..333592b4769a --- /dev/null +++ b/app/code/Magento/GraphQl/Model/Query/Context.php @@ -0,0 +1,72 @@ +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; + } +} diff --git a/app/code/Magento/GraphQl/Model/Query/ContextFactory.php b/app/code/Magento/GraphQl/Model/Query/ContextFactory.php new file mode 100644 index 000000000000..47176b41ba44 --- /dev/null +++ b/app/code/Magento/GraphQl/Model/Query/ContextFactory.php @@ -0,0 +1,77 @@ +extensionAttributesFactory = $extensionAttributesFactory; + $this->objectManager = $objectManager; + $this->contextParametersProcessors = $contextParametersProcessors; + } + + /** + * @inheritdoc + */ + public function create(): ContextInterface + { + $contextParameters = $this->objectManager->create(ContextParametersInterface::class); + + foreach ($this->contextParametersProcessors as $contextParametersProcessor) { + if (!$contextParametersProcessor instanceof ContextParametersProcessorInterface) { + throw new LocalizedException( + __('ContextParametersProcessors must implement %1', ContextParametersProcessorInterface::class) + ); + } + $contextParameters = $contextParametersProcessor->execute($contextParameters); + } + + $extensionAttributes = $this->extensionAttributesFactory->create( + ContextInterface::class, + $contextParameters->getExtensionAttributes() + ); + + $context = $this->objectManager->create(ContextInterface::class, [ + 'userType' => $contextParameters->getUserType(), + 'userId' => $contextParameters->getUserId(), + 'extensionAttributes' => $extensionAttributes, + ]); + return $context; + } +} diff --git a/app/code/Magento/GraphQl/Model/Query/ContextFactoryInterface.php b/app/code/Magento/GraphQl/Model/Query/ContextFactoryInterface.php new file mode 100644 index 000000000000..ceea98c39ce3 --- /dev/null +++ b/app/code/Magento/GraphQl/Model/Query/ContextFactoryInterface.php @@ -0,0 +1,21 @@ +userType = $userType; + } + + /** + * @inheritdoc + */ + public function getUserType(): ?int + { + return $this->userType; + } + + /** + * @inheritdoc + */ + public function setUserId(int $userId): void + { + $this->userId = $userId; + } + + /** + * @inheritdoc + */ + public function getUserId(): ?int + { + return $this->userId; + } + + /** + * @inheritdoc + */ + public function addExtensionAttribute(string $key, $value): void + { + $this->extensionAttributesData[$key] = $value; + } + + /** + * @inheritdoc + */ + public function getExtensionAttributes(): array + { + return $this->extensionAttributesData; + } +} diff --git a/app/code/Magento/GraphQl/Model/Query/ContextParametersInterface.php b/app/code/Magento/GraphQl/Model/Query/ContextParametersInterface.php new file mode 100644 index 000000000000..ce875c6131f9 --- /dev/null +++ b/app/code/Magento/GraphQl/Model/Query/ContextParametersInterface.php @@ -0,0 +1,60 @@ +setId($data['id']); - } - if (isset($data['type'])) { - $this->setId($data['type']); - } - $this->userContext = $userContext; - } - - /** - * {@inheritdoc} - * - * @return \Magento\Framework\GraphQl\Query\Resolver\ContextExtensionInterface - */ - public function getExtensionAttributes() : \Magento\Framework\GraphQl\Query\Resolver\ContextExtensionInterface - { - return $this->_getExtensionAttributes(); - } - - /** - * {@inheritdoc} - * - * @param \Magento\Framework\GraphQl\Query\Resolver\ContextExtensionInterface $extensionAttributes - * @return ContextInterface - */ - public function setExtensionAttributes( - \Magento\Framework\GraphQl\Query\Resolver\ContextExtensionInterface $extensionAttributes - ) : ContextInterface { - return $this->_setExtensionAttributes($extensionAttributes); - } - - /** - * @inheritDoc - */ - public function getUserId() : int - { - if (!$this->getData(self::USER_ID)) { - $this->setUserId((int) $this->userContext->getUserId()); - } - return (int) $this->getData(self::USER_ID); - } - - /** - * @inheritDoc - */ - public function setUserId(int $userId) : ContextInterface - { - return $this->setData(self::USER_ID, $userId); - } - - /** - * @inheritDoc - */ - public function getUserType() : int - { - if (!$this->getData(self::USER_TYPE_ID)) { - $this->setUserType($this->userContext->getUserType()); - } - return (int) $this->getData(self::USER_TYPE_ID); - } - - /** - * @inheritDoc - */ - public function setUserType(int $typeId) : ContextInterface - { - return $this->setData(self::USER_TYPE_ID, $typeId); - } -} diff --git a/app/code/Magento/GraphQl/etc/di.xml b/app/code/Magento/GraphQl/etc/di.xml index 6acb78f9c7f9..d76101e5556c 100644 --- a/app/code/Magento/GraphQl/etc/di.xml +++ b/app/code/Magento/GraphQl/etc/di.xml @@ -7,10 +7,12 @@ --> - + + + diff --git a/lib/internal/Magento/Framework/GraphQl/Query/Resolver/ContextInterface.php b/lib/internal/Magento/Framework/GraphQl/Query/Resolver/ContextInterface.php index 9bf708539ef0..7cece8dd7e39 100644 --- a/lib/internal/Magento/Framework/GraphQl/Query/Resolver/ContextInterface.php +++ b/lib/internal/Magento/Framework/GraphQl/Query/Resolver/ContextInterface.php @@ -7,7 +7,6 @@ namespace Magento\Framework\GraphQl\Query\Resolver; -use Magento\Framework\Api\ExtensibleDataInterface; use Magento\Framework\GraphQl\Query\ResolverInterface; /** @@ -16,56 +15,6 @@ * GraphQL will pass the same instance of this interface to each field resolver, so these resolvers could have * shared access to the same data for ease of implementation purposes. */ -interface ContextInterface extends ExtensibleDataInterface +interface ContextInterface { - /** - * Get the type of a user - * - * @see \Magento\Authorization\Model\UserContextInterface for corespondent values - * - * @return int - */ - public function getUserType() : int; - - /** - * Set type of a user - * - * @see \Magento\Authorization\Model\UserContextInterface for corespondent values - * - * @param int $typeId - * @return ContextInterface - */ - public function setUserType(int $typeId) : ContextInterface; - - /** - * Get id of the user - * - * @return int - */ - public function getUserId() : int; - - /** - * Set id of a user - * - * @param int $userId - * @return ContextInterface - */ - public function setUserId(int $userId) : ContextInterface; - - /** - * Retrieve existing extension attributes object or create a new one. - * - * @return \Magento\Framework\GraphQl\Query\Resolver\ContextExtensionInterface - */ - public function getExtensionAttributes(); - - /** - * Set an extension attributes object. - * - * @param \Magento\Framework\GraphQl\Query\Resolver\ContextExtensionInterface $extensionAttributes - * @return ContextInterface - */ - public function setExtensionAttributes( - \Magento\Framework\GraphQl\Query\Resolver\ContextExtensionInterface $extensionAttributes - ) : ContextInterface; } From e514ce0a9b73113fd87c6a446c83b60d35604faf Mon Sep 17 00:00:00 2001 From: Valerii Naida Date: Thu, 13 Jun 2019 15:12:39 -0500 Subject: [PATCH 02/12] magento/graphql-ce#741: Add extension point to set custom parameters to Query Context object --- app/code/Magento/CustomerGraphQl/etc/graphql/di.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/CustomerGraphQl/etc/graphql/di.xml b/app/code/Magento/CustomerGraphQl/etc/graphql/di.xml index 15e0f080b41f..961eab2a8320 100644 --- a/app/code/Magento/CustomerGraphQl/etc/graphql/di.xml +++ b/app/code/Magento/CustomerGraphQl/etc/graphql/di.xml @@ -16,7 +16,7 @@ - Magento\CustomerGraphQl\Model\Context\AddUserInfoToContext + Magento\CustomerGraphQl\Model\Context\AddUserInfoToContext From 5d12f8e8b5da3307a960c2d8dac9b397ba95dcd9 Mon Sep 17 00:00:00 2001 From: Valerii Naida Date: Thu, 13 Jun 2019 17:13:28 -0500 Subject: [PATCH 03/12] magento/graphql-ce#741: Add extension point to set custom parameters to Query Context object --- app/code/Magento/CustomerGraphQl/composer.json | 6 ++---- app/code/Magento/GraphQl/Model/Query/Context.php | 1 - .../Api/_files/ExtensibleInterfacesTest/blacklist_ce.txt | 3 ++- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/app/code/Magento/CustomerGraphQl/composer.json b/app/code/Magento/CustomerGraphQl/composer.json index 722aba3aa065..39721fe79f69 100644 --- a/app/code/Magento/CustomerGraphQl/composer.json +++ b/app/code/Magento/CustomerGraphQl/composer.json @@ -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" diff --git a/app/code/Magento/GraphQl/Model/Query/Context.php b/app/code/Magento/GraphQl/Model/Query/Context.php index 333592b4769a..a9d4bbd990f3 100644 --- a/app/code/Magento/GraphQl/Model/Query/Context.php +++ b/app/code/Magento/GraphQl/Model/Query/Context.php @@ -45,7 +45,6 @@ public function __construct( $this->extensionAttributes = $extensionAttributes; } - /** * @inheritdoc */ diff --git a/dev/tests/static/testsuite/Magento/Test/Integrity/Magento/Framework/Api/_files/ExtensibleInterfacesTest/blacklist_ce.txt b/dev/tests/static/testsuite/Magento/Test/Integrity/Magento/Framework/Api/_files/ExtensibleInterfacesTest/blacklist_ce.txt index 0fd245561e6b..e9b1e6e428e0 100644 --- a/dev/tests/static/testsuite/Magento/Test/Integrity/Magento/Framework/Api/_files/ExtensibleInterfacesTest/blacklist_ce.txt +++ b/dev/tests/static/testsuite/Magento/Test/Integrity/Magento/Framework/Api/_files/ExtensibleInterfacesTest/blacklist_ce.txt @@ -1,3 +1,4 @@ module Magento_Payment Model/Info.php module Magento_Customer Model/Address/AbstractAddress.php -library magento/framework MessageQueue/Rpc/Publisher.php \ No newline at end of file +library magento/framework MessageQueue/Rpc/Publisher.php +module Magento_GraphQl Model/Query/ContextInterface.php \ No newline at end of file From ed35766bf6218ba97c99575a7e2c3484c3caafa2 Mon Sep 17 00:00:00 2001 From: Valerii Naida Date: Thu, 13 Jun 2019 17:37:11 -0500 Subject: [PATCH 04/12] magento/graphql-ce#741: Add extension point to set custom parameters to Query Context object --- .../Magento/GraphQl/Model/Query/ContextFactory.php | 13 ++++++++----- app/code/Magento/GraphQl/composer.json | 1 - 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/app/code/Magento/GraphQl/Model/Query/ContextFactory.php b/app/code/Magento/GraphQl/Model/Query/ContextFactory.php index 47176b41ba44..8b74eb8f6e48 100644 --- a/app/code/Magento/GraphQl/Model/Query/ContextFactory.php +++ b/app/code/Magento/GraphQl/Model/Query/ContextFactory.php @@ -67,11 +67,14 @@ public function create(): ContextInterface $contextParameters->getExtensionAttributes() ); - $context = $this->objectManager->create(ContextInterface::class, [ - 'userType' => $contextParameters->getUserType(), - 'userId' => $contextParameters->getUserId(), - 'extensionAttributes' => $extensionAttributes, - ]); + $context = $this->objectManager->create( + ContextInterface::class, + [ + 'userType' => $contextParameters->getUserType(), + 'userId' => $contextParameters->getUserId(), + 'extensionAttributes' => $extensionAttributes, + ] + ); return $context; } } diff --git a/app/code/Magento/GraphQl/composer.json b/app/code/Magento/GraphQl/composer.json index 3a1e8d1bfd9f..51e93f01a6e5 100644 --- a/app/code/Magento/GraphQl/composer.json +++ b/app/code/Magento/GraphQl/composer.json @@ -4,7 +4,6 @@ "type": "magento2-module", "require": { "php": "~7.1.3||~7.2.0", - "magento/module-authorization": "*", "magento/module-eav": "*", "magento/framework": "*" }, From ac052797838d14fd26d07595f93e950c55e76d2e Mon Sep 17 00:00:00 2001 From: Valerii Naida Date: Thu, 13 Jun 2019 17:49:31 -0500 Subject: [PATCH 05/12] magento/graphql-ce#741: Add extension point to set custom parameters to Query Context object --- .../Magento/CustomerGraphQl/Model/Resolver/CreateCustomer.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/app/code/Magento/CustomerGraphQl/Model/Resolver/CreateCustomer.php b/app/code/Magento/CustomerGraphQl/Model/Resolver/CreateCustomer.php index 1ae22bcc1279..a42afb49db88 100644 --- a/app/code/Magento/CustomerGraphQl/Model/Resolver/CreateCustomer.php +++ b/app/code/Magento/CustomerGraphQl/Model/Resolver/CreateCustomer.php @@ -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; @@ -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]; } From 883122105a47114ebcb8ce1a5ed5deefc330d207 Mon Sep 17 00:00:00 2001 From: Valerii Naida Date: Thu, 13 Jun 2019 18:03:18 -0500 Subject: [PATCH 06/12] magento/graphql-ce#741: Add extension point to set custom parameters to Query Context object --- .../CustomerGraphQl/Model/Resolver/IsSubscribed.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/CustomerGraphQl/Model/Resolver/IsSubscribed.php b/app/code/Magento/CustomerGraphQl/Model/Resolver/IsSubscribed.php index fc5691d97cbf..f956949616c9 100644 --- a/app/code/Magento/CustomerGraphQl/Model/Resolver/IsSubscribed.php +++ b/app/code/Magento/CustomerGraphQl/Model/Resolver/IsSubscribed.php @@ -8,6 +8,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; @@ -50,7 +51,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; From 13ea1f8a1b7edc0d3cdb5e9b65abe31109d34511 Mon Sep 17 00:00:00 2001 From: Lena Orobei Date: Fri, 14 Jun 2019 10:05:33 -0500 Subject: [PATCH 07/12] magento/graphql-ce#741: Add extension point to set custom parameters to Query Context object - added extended docs --- app/code/Magento/CustomerGraphQl/etc/graphql/di.xml | 2 +- .../Model/Query/ContextParametersProcessorInterface.php | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/CustomerGraphQl/etc/graphql/di.xml b/app/code/Magento/CustomerGraphQl/etc/graphql/di.xml index 961eab2a8320..cb4ab0b7b27f 100644 --- a/app/code/Magento/CustomerGraphQl/etc/graphql/di.xml +++ b/app/code/Magento/CustomerGraphQl/etc/graphql/di.xml @@ -20,4 +20,4 @@ - \ No newline at end of file + diff --git a/app/code/Magento/GraphQl/Model/Query/ContextParametersProcessorInterface.php b/app/code/Magento/GraphQl/Model/Query/ContextParametersProcessorInterface.php index e79ffa7f00c7..bcea6383e7fb 100644 --- a/app/code/Magento/GraphQl/Model/Query/ContextParametersProcessorInterface.php +++ b/app/code/Magento/GraphQl/Model/Query/ContextParametersProcessorInterface.php @@ -8,7 +8,12 @@ namespace Magento\GraphQl\Model\Query; /** - * Extension point for adding custom parameters to context object + * Adding custom parameters to context object: + * + * - Add new contextParametersProcessors item to ContextFactory in the di.xml. + * - Class must extend ContextParametersProcessorInterface. + * - Implement execute method which adds additional data to the context though extension attributes. + * - This data will be present in each resolver. */ interface ContextParametersProcessorInterface { From bb197739e5308dc68849978833f6193c3ce83d62 Mon Sep 17 00:00:00 2001 From: Lena Orobei Date: Fri, 14 Jun 2019 10:07:32 -0500 Subject: [PATCH 08/12] magento/graphql-ce#741: Add extension point to set custom parameters to Query Context object - added extended docs --- .../GraphQl/Model/Query/ContextParametersProcessorInterface.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/GraphQl/Model/Query/ContextParametersProcessorInterface.php b/app/code/Magento/GraphQl/Model/Query/ContextParametersProcessorInterface.php index bcea6383e7fb..30f1835f2a0f 100644 --- a/app/code/Magento/GraphQl/Model/Query/ContextParametersProcessorInterface.php +++ b/app/code/Magento/GraphQl/Model/Query/ContextParametersProcessorInterface.php @@ -10,7 +10,7 @@ /** * Adding custom parameters to context object: * - * - Add new contextParametersProcessors item to ContextFactory in the di.xml. + * - Add new processors argument item to ContextFactory in the di.xml. * - Class must extend ContextParametersProcessorInterface. * - Implement execute method which adds additional data to the context though extension attributes. * - This data will be present in each resolver. From 7b3024a28e8541fc8b750b3e8332c3b2dae41d14 Mon Sep 17 00:00:00 2001 From: Valerii Naida Date: Fri, 14 Jun 2019 10:05:17 -0500 Subject: [PATCH 09/12] magento/graphql-ce#741: Add extension point to set custom parameters to Query Context object --- .../CustomerGraphQl/etc/extension_attributes.xml | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 app/code/Magento/CustomerGraphQl/etc/extension_attributes.xml diff --git a/app/code/Magento/CustomerGraphQl/etc/extension_attributes.xml b/app/code/Magento/CustomerGraphQl/etc/extension_attributes.xml new file mode 100644 index 000000000000..288f9ec1183f --- /dev/null +++ b/app/code/Magento/CustomerGraphQl/etc/extension_attributes.xml @@ -0,0 +1,12 @@ + + + + + + + \ No newline at end of file From 892c1311c4dce1ab098a9da81b5d7febedb7c913 Mon Sep 17 00:00:00 2001 From: Valerii Naida Date: Fri, 14 Jun 2019 11:18:37 -0500 Subject: [PATCH 10/12] magento/graphql-ce#741: Add extension point to set custom parameters to Query Context object --- app/code/Magento/GraphQl/Model/Query/ContextFactory.php | 2 +- app/code/Magento/GraphQl/Model/Query/ContextParameters.php | 2 +- .../Magento/GraphQl/Model/Query/ContextParametersInterface.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/GraphQl/Model/Query/ContextFactory.php b/app/code/Magento/GraphQl/Model/Query/ContextFactory.php index 8b74eb8f6e48..0dd11018983f 100644 --- a/app/code/Magento/GraphQl/Model/Query/ContextFactory.php +++ b/app/code/Magento/GraphQl/Model/Query/ContextFactory.php @@ -64,7 +64,7 @@ public function create(): ContextInterface $extensionAttributes = $this->extensionAttributesFactory->create( ContextInterface::class, - $contextParameters->getExtensionAttributes() + $contextParameters->getExtensionAttributesData() ); $context = $this->objectManager->create( diff --git a/app/code/Magento/GraphQl/Model/Query/ContextParameters.php b/app/code/Magento/GraphQl/Model/Query/ContextParameters.php index 62fb03824d2b..8e71a96f7e1d 100644 --- a/app/code/Magento/GraphQl/Model/Query/ContextParameters.php +++ b/app/code/Magento/GraphQl/Model/Query/ContextParameters.php @@ -70,7 +70,7 @@ public function addExtensionAttribute(string $key, $value): void /** * @inheritdoc */ - public function getExtensionAttributes(): array + public function getExtensionAttributesData(): array { return $this->extensionAttributesData; } diff --git a/app/code/Magento/GraphQl/Model/Query/ContextParametersInterface.php b/app/code/Magento/GraphQl/Model/Query/ContextParametersInterface.php index ce875c6131f9..9c49c2e2b120 100644 --- a/app/code/Magento/GraphQl/Model/Query/ContextParametersInterface.php +++ b/app/code/Magento/GraphQl/Model/Query/ContextParametersInterface.php @@ -56,5 +56,5 @@ public function addExtensionAttribute(string $key, $value): void; * * @return array */ - public function getExtensionAttributes(): array; + public function getExtensionAttributesData(): array; } From 0d505feb486a9d1922e0c7d9393a3433b9099cd4 Mon Sep 17 00:00:00 2001 From: Valerii Naida Date: Wed, 19 Jun 2019 15:40:54 -0500 Subject: [PATCH 11/12] magento/graphql-ce#741: Add extension point to set custom parameters to Query Context object --- .../Model/Context/AddUserInfoToContext.php | 30 +----- .../Model/Context/GetCustomer.php | 94 ------------------- .../Model/Customer/SaveCustomer.php | 2 - .../Model/Resolver/IsSubscribed.php | 9 -- .../Magento/GraphQl/Controller/GraphQl.php | 22 +++-- .../GraphQl/Model/Query/ContextFactory.php | 6 +- app/code/Magento/GraphQl/etc/di.xml | 1 + .../Model/Cart/GetCartForUser.php | 1 + .../Model/Cart/QuoteAddressFactory.php | 2 +- .../SetShippingAddressesOnCartInterface.php | 1 - 10 files changed, 24 insertions(+), 144 deletions(-) delete mode 100644 app/code/Magento/CustomerGraphQl/Model/Context/GetCustomer.php diff --git a/app/code/Magento/CustomerGraphQl/Model/Context/AddUserInfoToContext.php b/app/code/Magento/CustomerGraphQl/Model/Context/AddUserInfoToContext.php index a871d7f62ba0..c0e46b2bb727 100644 --- a/app/code/Magento/CustomerGraphQl/Model/Context/AddUserInfoToContext.php +++ b/app/code/Magento/CustomerGraphQl/Model/Context/AddUserInfoToContext.php @@ -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; } /** @@ -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; - } } diff --git a/app/code/Magento/CustomerGraphQl/Model/Context/GetCustomer.php b/app/code/Magento/CustomerGraphQl/Model/Context/GetCustomer.php deleted file mode 100644 index f680f1516889..000000000000 --- a/app/code/Magento/CustomerGraphQl/Model/Context/GetCustomer.php +++ /dev/null @@ -1,94 +0,0 @@ -authentication = $authentication; - $this->customerRepository = $customerRepository; - $this->accountManagement = $accountManagement; - } - - /** - * Get customer - * - * @param int $customerId - * @return void - * @throws GraphQlAuthenticationException - * @throws GraphQlAuthorizationException - * @throws GraphQlInputException - * @throws GraphQlNoSuchEntityException - */ - public function execute(int $customerId): CustomerInterface - { - try { - $customer = $this->customerRepository->getById($customerId); - } catch (NoSuchEntityException $e) { - throw new GraphQlNoSuchEntityException( - __('Customer with id "%customer_id" does not exist.', ['customer_id' => $customerId]), - $e - ); - } catch (LocalizedException $e) { - throw new GraphQlInputException(__($e->getMessage())); - } - - if (true === $this->authentication->isLocked($customerId)) { - throw new GraphQlAuthenticationException(__('The account is locked.')); - } - - try { - $confirmationStatus = $this->accountManagement->getConfirmationStatus($customerId); - } catch (LocalizedException $e) { - throw new GraphQlInputException(__($e->getMessage())); - } - - if ($confirmationStatus === AccountManagementInterface::ACCOUNT_CONFIRMATION_REQUIRED) { - throw new GraphQlAuthenticationException(__("This account isn't confirmed. Verify and try again.")); - } - return $customer; - } -} diff --git a/app/code/Magento/CustomerGraphQl/Model/Customer/SaveCustomer.php b/app/code/Magento/CustomerGraphQl/Model/Customer/SaveCustomer.php index 1605c63b62f4..56ab70d12b1c 100644 --- a/app/code/Magento/CustomerGraphQl/Model/Customer/SaveCustomer.php +++ b/app/code/Magento/CustomerGraphQl/Model/Customer/SaveCustomer.php @@ -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; @@ -39,7 +38,6 @@ public function __construct( * * @param CustomerInterface $customer * @throws GraphQlAlreadyExistsException - * @throws GraphQlAuthenticationException * @throws GraphQlInputException */ public function execute(CustomerInterface $customer): void diff --git a/app/code/Magento/CustomerGraphQl/Model/Resolver/IsSubscribed.php b/app/code/Magento/CustomerGraphQl/Model/Resolver/IsSubscribed.php index f956949616c9..4e49891b5870 100644 --- a/app/code/Magento/CustomerGraphQl/Model/Resolver/IsSubscribed.php +++ b/app/code/Magento/CustomerGraphQl/Model/Resolver/IsSubscribed.php @@ -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; @@ -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; } diff --git a/app/code/Magento/GraphQl/Controller/GraphQl.php b/app/code/Magento/GraphQl/Controller/GraphQl.php index c0d3817ee682..2d72fde91b03 100644 --- a/app/code/Magento/GraphQl/Controller/GraphQl.php +++ b/app/code/Magento/GraphQl/Controller/GraphQl.php @@ -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; @@ -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 @@ -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( @@ -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); } /** diff --git a/app/code/Magento/GraphQl/Model/Query/ContextFactory.php b/app/code/Magento/GraphQl/Model/Query/ContextFactory.php index 0dd11018983f..bfcd0d548757 100644 --- a/app/code/Magento/GraphQl/Model/Query/ContextFactory.php +++ b/app/code/Magento/GraphQl/Model/Query/ContextFactory.php @@ -19,7 +19,7 @@ class ContextFactory implements ContextFactoryInterface /** * @var ExtensionAttributesFactory */ - protected $extensionAttributesFactory; + private $extensionAttributesFactory; /** * @var ObjectManagerInterface @@ -64,7 +64,9 @@ public function create(): ContextInterface $extensionAttributes = $this->extensionAttributesFactory->create( ContextInterface::class, - $contextParameters->getExtensionAttributesData() + [ + 'data' => $contextParameters->getExtensionAttributesData(), + ] ); $context = $this->objectManager->create( diff --git a/app/code/Magento/GraphQl/etc/di.xml b/app/code/Magento/GraphQl/etc/di.xml index d76101e5556c..b356f33c4f4b 100644 --- a/app/code/Magento/GraphQl/etc/di.xml +++ b/app/code/Magento/GraphQl/etc/di.xml @@ -7,6 +7,7 @@ --> + diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/GetCartForUser.php b/app/code/Magento/QuoteGraphQl/Model/Cart/GetCartForUser.php index 3506ffc8c879..12987509b025 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/GetCartForUser.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/GetCartForUser.php @@ -58,6 +58,7 @@ public function __construct( * @return Quote * @throws GraphQlAuthorizationException * @throws GraphQlNoSuchEntityException + * @throws NoSuchEntityException */ public function execute(string $cartHash, ?int $customerId): Quote { diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php b/app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php index 13d6a1d3dce7..a66c0ddb1a53 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php @@ -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 diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressesOnCartInterface.php b/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressesOnCartInterface.php index eb0f3522102c..de299e34d9c0 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressesOnCartInterface.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressesOnCartInterface.php @@ -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; From 28aaefa35551bd8ab2667db3d86368e9aca06484 Mon Sep 17 00:00:00 2001 From: Valerii Naida Date: Wed, 19 Jun 2019 15:58:21 -0500 Subject: [PATCH 12/12] magento/graphql-ce#741: Add extension point to set custom parameters to Query Context object --- .../GraphQl/Model/Query/Resolver/Context.php | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 app/code/Magento/GraphQl/Model/Query/Resolver/Context.php diff --git a/app/code/Magento/GraphQl/Model/Query/Resolver/Context.php b/app/code/Magento/GraphQl/Model/Query/Resolver/Context.php new file mode 100644 index 000000000000..2b8e3fabd686 --- /dev/null +++ b/app/code/Magento/GraphQl/Model/Query/Resolver/Context.php @@ -0,0 +1,89 @@ +_getExtensionAttributes(); + } + + /** + * Set extension attributes + * + * @param \Magento\Framework\GraphQl\Query\Resolver\ContextExtensionInterface $extensionAttributes + * @return ContextInterface + */ + public function setExtensionAttributes( + \Magento\Framework\GraphQl\Query\Resolver\ContextExtensionInterface $extensionAttributes + ) : ContextInterface { + return $this->_setExtensionAttributes($extensionAttributes); + } + + /** + * Get user id + * + * @return int + */ + public function getUserId() : int + { + return (int) $this->getData(self::USER_ID); + } + + /** + * Set user id + * + * @param int $userId + * @return ContextInterface + */ + public function setUserId(int $userId) : ContextInterface + { + return $this->setData(self::USER_ID, $userId); + } + + /** + * Get user type + * + * @return int + */ + public function getUserType() : int + { + return (int) $this->getData(self::USER_TYPE_ID); + } + + /** + * Set user type + * + * @param int $typeId + * @return ContextInterface + */ + public function setUserType(int $typeId) : ContextInterface + { + return $this->setData(self::USER_TYPE_ID, $typeId); + } +}