-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENGCOM-5288: GraphQL-741: Add extension point to set custom parameter…
…s to Query Context object #742
- Loading branch information
Showing
23 changed files
with
481 additions
and
205 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
app/code/Magento/CustomerGraphQl/Model/Context/AddUserInfoToContext.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
app/code/Magento/CustomerGraphQl/etc/extension_attributes.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.