Skip to content
This repository has been archived by the owner on Dec 19, 2019. It is now read-only.

GraphQL-741: Add extension point to set custom parameters to Query Context object #742

Merged
merged 16 commits into from
Jun 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
3096057
magento/graphql-ce#741: Add extension point to set custom parameters …
naydav Jun 13, 2019
e514ce0
magento/graphql-ce#741: Add extension point to set custom parameters …
naydav Jun 13, 2019
5d12f8e
magento/graphql-ce#741: Add extension point to set custom parameters …
naydav Jun 13, 2019
ed35766
magento/graphql-ce#741: Add extension point to set custom parameters …
naydav Jun 13, 2019
ac05279
magento/graphql-ce#741: Add extension point to set custom parameters …
naydav Jun 13, 2019
8831221
magento/graphql-ce#741: Add extension point to set custom parameters …
naydav Jun 13, 2019
13ea1f8
magento/graphql-ce#741: Add extension point to set custom parameters …
lenaorobei Jun 14, 2019
bb19773
magento/graphql-ce#741: Add extension point to set custom parameters …
lenaorobei Jun 14, 2019
7b3024a
magento/graphql-ce#741: Add extension point to set custom parameters …
naydav Jun 14, 2019
892c131
magento/graphql-ce#741: Add extension point to set custom parameters …
naydav Jun 14, 2019
4485e23
Merge branch 'magento/graphql-ce#741' of github.com:magento/graphql-c…
naydav Jun 14, 2019
785ab2b
Merge branch '2.3-develop' into magento/graphql-ce#741
naydav Jun 18, 2019
4788d35
Merge remote-tracking branch 'origin/2.3-develop' into magento/graphq…
naydav Jun 18, 2019
87296c1
Merge remote-tracking branch 'origin/2.3-develop' into magento/graphq…
naydav Jun 19, 2019
0d505fe
magento/graphql-ce#741: Add extension point to set custom parameters …
naydav Jun 19, 2019
28aaefa
magento/graphql-ce#741: Add extension point to set custom parameters …
naydav Jun 19, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
naydav marked this conversation as resolved.
Show resolved Hide resolved
naydav marked this conversation as resolved.
Show resolved Hide resolved
/**
* 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