diff --git a/Compat/WithGraphQLContextValuesTrait.php b/Compat/WithGraphQLContextValuesTrait.php new file mode 100644 index 0000000..505ccd8 --- /dev/null +++ b/Compat/WithGraphQLContextValuesTrait.php @@ -0,0 +1,54 @@ +getExtensionAttributes(); + if (method_exists($extension, 'getStore')) { + return $extension->getStore(); + } + return $storeManager->getStore(); + } + + /** + * The isCustomer parameter in GraphQL context appeared in 2.3.3 + * see Magento\CustomerGraphQl\Model\Context\AddUserInfoToContext + * + * @param ContextInterface $context + * @return bool + */ + protected function getIsCustomer(ContextInterface $context): bool + { + $extension = $context->getExtensionAttributes(); + if (method_exists($extension, 'getIsCustomer')) { + return $extension->getIsCustomer(); + } + + $userId = $context->getUserId(); + return !empty($userId); + } +} diff --git a/Model/Resolver/CreateProductReview.php b/Model/Resolver/CreateProductReview.php index 9b0171c..1c27e2a 100644 --- a/Model/Resolver/CreateProductReview.php +++ b/Model/Resolver/CreateProductReview.php @@ -1,8 +1,10 @@ addReviewToProduct = $addReviewToProduct; $this->reviewDataMapper = $reviewDataMapper; $this->reviewHelper = $reviewHelper; $this->reviewsConfig = $reviewsConfig; + $this->storeManager = $storeManager; } /** @@ -94,7 +107,7 @@ public function resolve( $input = $args['input']; $customerId = null; - if (false !== $context->getExtensionAttributes()->getIsCustomer()) { + if (false !== $this->getIsCustomer($context)) { $customerId = (int) $context->getUserId(); } @@ -110,7 +123,7 @@ public function resolve( 'detail' => $input['text'], ]; /** @var StoreInterface $store */ - $store = $context->getExtensionAttributes()->getStore(); + $store = $this->getStore($context, $this->storeManager); $review = $this->addReviewToProduct->execute($data, $ratings, $sku, $customerId, (int) $store->getId()); return ['review' => $this->reviewDataMapper->map($review)]; diff --git a/Model/Resolver/Customer/Reviews.php b/Model/Resolver/Customer/Reviews.php index 8c0bca6..4ec9519 100644 --- a/Model/Resolver/Customer/Reviews.php +++ b/Model/Resolver/Customer/Reviews.php @@ -1,8 +1,10 @@ getExtensionAttributes()->getIsCustomer()) { + if (false === $this->getIsCustomer($context)) { throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.')); } diff --git a/Model/Resolver/Product/RatingSummary.php b/Model/Resolver/Product/RatingSummary.php index eed5034..283527d 100644 --- a/Model/Resolver/Product/RatingSummary.php +++ b/Model/Resolver/Product/RatingSummary.php @@ -1,8 +1,10 @@ summaryFactory = $summaryFactory; $this->reviewsConfig = $reviewsConfig; + $this->storeManager = $storeManager; } /** @@ -76,7 +90,7 @@ public function resolve( } /** @var StoreInterface $store */ - $store = $context->getExtensionAttributes()->getStore(); + $store = $this->getStore($context, $this->storeManager); /** @var Product $product */ $product = $value['model']; diff --git a/Model/Resolver/ProductReviewRatingsMetadata.php b/Model/Resolver/ProductReviewRatingsMetadata.php index 015c15c..fe39db4 100644 --- a/Model/Resolver/ProductReviewRatingsMetadata.php +++ b/Model/Resolver/ProductReviewRatingsMetadata.php @@ -18,13 +18,17 @@ use Magento\Review\Model\ResourceModel\Rating\CollectionFactory; use Magento\Review\Model\Review; use Magento\Review\Model\Review\Config as ReviewsConfig; +use Magento\ReviewGraphQl\Compat\WithGraphQLContextValuesTrait; use Magento\Store\Api\Data\StoreInterface; +use Magento\Store\Model\StoreManagerInterface; /** * Resolve data review rating metadata */ class ProductReviewRatingsMetadata implements ResolverInterface { + use WithGraphQLContextValuesTrait; + /** * @var CollectionFactory */ @@ -35,14 +39,23 @@ class ProductReviewRatingsMetadata implements ResolverInterface */ private $reviewsConfig; + /** + * @var StoreManagerInterface + */ + private $storeManager; + /** * @param CollectionFactory $ratingCollectionFactory * @param ReviewsConfig $reviewsConfig */ - public function __construct(CollectionFactory $ratingCollectionFactory, ReviewsConfig $reviewsConfig) - { + public function __construct( + CollectionFactory $ratingCollectionFactory, + ReviewsConfig $reviewsConfig, + StoreManagerInterface $storeManager + ) { $this->ratingCollectionFactory = $ratingCollectionFactory; $this->reviewsConfig = $reviewsConfig; + $this->storeManager = $storeManager; } /** @@ -71,7 +84,7 @@ public function resolve( $items = []; /** @var StoreInterface $store */ - $store = $context->getExtensionAttributes()->getStore(); + $store = $this->getStore($context, $this->storeManager); /** @var RatingCollection $ratingCollection */ $ratingCollection = $this->ratingCollectionFactory->create();