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 0000000000000..c0e46b2bb727f --- /dev/null +++ b/app/code/Magento/CustomerGraphQl/Model/Context/AddUserInfoToContext.php @@ -0,0 +1,52 @@ +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; + } +} diff --git a/app/code/Magento/CustomerGraphQl/Model/Customer/SaveCustomer.php b/app/code/Magento/CustomerGraphQl/Model/Customer/SaveCustomer.php index 1605c63b62f4c..56ab70d12b1c8 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/CreateCustomer.php b/app/code/Magento/CustomerGraphQl/Model/Resolver/CreateCustomer.php index 1ae22bcc12792..a42afb49db888 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]; } diff --git a/app/code/Magento/CustomerGraphQl/Model/Resolver/IsSubscribed.php b/app/code/Magento/CustomerGraphQl/Model/Resolver/IsSubscribed.php index fc5691d97cbfe..4e49891b5870a 100644 --- a/app/code/Magento/CustomerGraphQl/Model/Resolver/IsSubscribed.php +++ b/app/code/Magento/CustomerGraphQl/Model/Resolver/IsSubscribed.php @@ -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; @@ -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; } @@ -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; diff --git a/app/code/Magento/CustomerGraphQl/composer.json b/app/code/Magento/CustomerGraphQl/composer.json index 722aba3aa0658..39721fe79f69f 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/CustomerGraphQl/etc/extension_attributes.xml b/app/code/Magento/CustomerGraphQl/etc/extension_attributes.xml new file mode 100644 index 0000000000000..288f9ec1183f6 --- /dev/null +++ b/app/code/Magento/CustomerGraphQl/etc/extension_attributes.xml @@ -0,0 +1,12 @@ + + + + + + + \ No newline at end of file diff --git a/app/code/Magento/CustomerGraphQl/etc/graphql/di.xml b/app/code/Magento/CustomerGraphQl/etc/graphql/di.xml index f1bd3563fda3d..cb4ab0b7b27f4 100644 --- a/app/code/Magento/CustomerGraphQl/etc/graphql/di.xml +++ b/app/code/Magento/CustomerGraphQl/etc/graphql/di.xml @@ -13,4 +13,11 @@ - \ No newline at end of file + + + + Magento\CustomerGraphQl\Model\Context\AddUserInfoToContext + + + + diff --git a/app/code/Magento/GraphQl/Controller/GraphQl.php b/app/code/Magento/GraphQl/Controller/GraphQl.php index 75b3ad277c603..2d72fde91b031 100644 --- a/app/code/Magento/GraphQl/Controller/GraphQl.php +++ b/app/code/Magento/GraphQl/Controller/GraphQl.php @@ -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. @@ -58,6 +59,7 @@ class GraphQl implements FrontControllerInterface /** * @var ContextInterface + * @deprecated $contextFactory is used for creating Context object */ private $resolverContext; @@ -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( @@ -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; @@ -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); } /** @@ -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) { 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 0000000000000..a9d4bbd990f35 --- /dev/null +++ b/app/code/Magento/GraphQl/Model/Query/Context.php @@ -0,0 +1,71 @@ +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 0000000000000..bfcd0d548757a --- /dev/null +++ b/app/code/Magento/GraphQl/Model/Query/ContextFactory.php @@ -0,0 +1,82 @@ +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, + [ + 'data' => $contextParameters->getExtensionAttributesData(), + ] + ); + + $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 0000000000000..ceea98c39ce3e --- /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 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 new file mode 100644 index 0000000000000..9c49c2e2b120c --- /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} + * Get extension attributes * * @return \Magento\Framework\GraphQl\Query\Resolver\ContextExtensionInterface */ @@ -74,7 +34,7 @@ public function getExtensionAttributes() : \Magento\Framework\GraphQl\Query\Reso } /** - * {@inheritdoc} + * Set extension attributes * * @param \Magento\Framework\GraphQl\Query\Resolver\ContextExtensionInterface $extensionAttributes * @return ContextInterface @@ -86,18 +46,20 @@ public function setExtensionAttributes( } /** - * @inheritDoc + * Get user id + * + * @return int */ public function getUserId() : int { - if (!$this->getData(self::USER_ID)) { - $this->setUserId((int) $this->userContext->getUserId()); - } return (int) $this->getData(self::USER_ID); } /** - * @inheritDoc + * Set user id + * + * @param int $userId + * @return ContextInterface */ public function setUserId(int $userId) : ContextInterface { @@ -105,18 +67,20 @@ public function setUserId(int $userId) : ContextInterface } /** - * @inheritDoc + * Get user type + * + * @return int */ 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 + * Set user type + * + * @param int $typeId + * @return ContextInterface */ public function setUserType(int $typeId) : ContextInterface { diff --git a/app/code/Magento/GraphQl/composer.json b/app/code/Magento/GraphQl/composer.json index 3a1e8d1bfd9f4..51e93f01a6e5c 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": "*" }, diff --git a/app/code/Magento/GraphQl/etc/di.xml b/app/code/Magento/GraphQl/etc/di.xml index 6acb78f9c7f9e..b356f33c4f4bf 100644 --- a/app/code/Magento/GraphQl/etc/di.xml +++ b/app/code/Magento/GraphQl/etc/di.xml @@ -8,9 +8,12 @@ + + + diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/GetCartForUser.php b/app/code/Magento/QuoteGraphQl/Model/Cart/GetCartForUser.php index 3506ffc8c8792..12987509b025e 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 13d6a1d3dce70..a66c0ddb1a536 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 eb0f3522102cf..de299e34d9c06 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; 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 0fd245561e6be..e9b1e6e428e03 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 diff --git a/lib/internal/Magento/Framework/GraphQl/Query/Resolver/ContextInterface.php b/lib/internal/Magento/Framework/GraphQl/Query/Resolver/ContextInterface.php index 9bf708539ef07..7cece8dd7e39b 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; }