Skip to content

Commit

Permalink
GraphQL-152: Allow scalars as resolver return type
Browse files Browse the repository at this point in the history
  • Loading branch information
Valeriy Nayda committed Sep 11, 2018
1 parent 61fb87f commit 8984c54
Show file tree
Hide file tree
Showing 43 changed files with 248 additions and 904 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@

namespace Magento\BundleGraphQl\Model\Resolver;

use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\BundleGraphQl\Model\Resolver\Links\Collection;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\Resolver\Value;
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
use Magento\Framework\GraphQl\Query\ResolverInterface;

/**
* {@inheritdoc}
* @inheritdoc
*/
class BundleItemLinks implements ResolverInterface
{
Expand All @@ -42,16 +42,14 @@ public function __construct(
}

/**
* {@inheritDoc}
* @inheritdoc
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) : Value
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
if (!isset($value['option_id']) || !isset($value['parent_id'])) {
$result = function () {
return null;
};
return $this->valueFactory->create($result);
throw new GraphQlInputException(__('"option_id" and "parent_id" values should be specified'));
}

$this->linkCollection->addIdFilters((int)$value['option_id'], (int)$value['parent_id']);
$result = function () use ($value) {
return $this->linkCollection->getLinksForOptionId((int)$value['option_id']);
Expand Down
9 changes: 4 additions & 5 deletions app/code/Magento/BundleGraphQl/Model/Resolver/BundleItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Framework\EntityManager\MetadataPool;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\Resolver\Value;
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
use Magento\Framework\GraphQl\Query\ResolverInterface;

/**
* {@inheritdoc}
* @inheritdoc
*/
class BundleItems implements ResolverInterface
{
Expand All @@ -35,7 +34,7 @@ class BundleItems implements ResolverInterface
/**
* @var MetadataPool
*/
private $metdataPool;
private $metadataPool;

/**
* @param Collection $bundleOptionCollection
Expand All @@ -57,9 +56,9 @@ public function __construct(
*
* {@inheritDoc}
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) : Value
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
$linkField = $this->metdataPool->getMetadata(ProductInterface::class)->getLinkField();
$linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
if ($value['type_id'] !== Type::TYPE_CODE
|| !isset($value[$linkField])
|| !isset($value[ProductInterface::SKU])
Expand Down
20 changes: 8 additions & 12 deletions app/code/Magento/BundleGraphQl/Model/Resolver/Options/Label.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

namespace Magento\BundleGraphQl\Model\Resolver\Options;

use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Deferred\Product;
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Deferred\Product as ProductDataProvider;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\Resolver\Value;
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
use Magento\Framework\GraphQl\Query\ResolverInterface;

Expand All @@ -19,42 +19,38 @@
*/
class Label implements ResolverInterface
{

/**
* @var ValueFactory
*/
private $valueFactory;

/**
* @var Product
* @var ProductDataProvider
*/
private $product;

/**
* @param ValueFactory $valueFactory
* @param Product $product
* @param ProductDataProvider $product
*/
public function __construct(ValueFactory $valueFactory, Product $product)
public function __construct(ValueFactory $valueFactory, ProductDataProvider $product)
{
$this->valueFactory = $valueFactory;
$this->product = $product;
}

/**
* @inheritDoc
* @inheritdoc
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
): Value {
) {
if (!isset($value['sku'])) {
$result = function () {
return null;
};
return $this->valueFactory->create($result);
throw new GraphQlInputException(__('"sku" value should be specified'));
}

$this->product->addProductSku($value['sku']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,33 @@
*/
declare(strict_types=1);


namespace Magento\BundleGraphQl\Model\Resolver\Product\Fields;

use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Bundle\Model\Product\Type as Bundle;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\Resolver\Value;
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
use Magento\Framework\GraphQl\Query\ResolverInterface;

/**
* {@inheritdoc}
* @inheritdoc
*/
class DynamicPrice implements ResolverInterface
{
/**
* @var ValueFactory
*/
private $valueFactory;

/**
* @param ValueFactory $valueFactory
*/
public function __construct(ValueFactory $valueFactory)
{
$this->valueFactory = $valueFactory;
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
): Value {
) {
$result = null;
if ($value['type_id'] === Bundle::TYPE_CODE) {
$result = isset($value['price_type']) ? !$value['price_type'] : null;
}

return $this->valueFactory->create(
function () use ($result) {
return $result;
}
);
return $result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,33 @@
*/
declare(strict_types=1);


namespace Magento\BundleGraphQl\Model\Resolver\Product\Fields;

use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Bundle\Model\Product\Type as Bundle;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\Resolver\Value;
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
use Magento\Framework\GraphQl\Query\ResolverInterface;

/**
* {@inheritdoc}
* @inheritdoc
*/
class DynamicSku implements ResolverInterface
{
/**
* @var ValueFactory
*/
private $valueFactory;

/**
* @param ValueFactory $valueFactory
*/
public function __construct(ValueFactory $valueFactory)
{
$this->valueFactory = $valueFactory;
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
): Value {
$result = function () {
return null;
};
) {
$result = null;
if ($value['type_id'] === Bundle::TYPE_CODE) {
$result = isset($value['sku_type']) ? !$value['sku_type'] : null;
}

return $this->valueFactory->create(
function () use ($result) {
return $result;
}
);
return $result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,33 @@
*/
declare(strict_types=1);


namespace Magento\BundleGraphQl\Model\Resolver\Product\Fields;

use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Bundle\Model\Product\Type as Bundle;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\Resolver\Value;
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
use Magento\Framework\GraphQl\Query\ResolverInterface;

/**
* {@inheritdoc}
* @inheritdoc
*/
class DynamicWeight implements ResolverInterface
{
/**
* @var ValueFactory
*/
private $valueFactory;

/**
* @param ValueFactory $valueFactory
*/
public function __construct(ValueFactory $valueFactory)
{
$this->valueFactory = $valueFactory;
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
): Value {
$result = function () {
return null;
};
) {
$result = null;
if ($value['type_id'] === Bundle::TYPE_CODE) {
$result = isset($value['weight_type']) ? !$value['weight_type'] : null;
}

return $this->valueFactory->create(
function () use ($result) {
return $result;
}
);
return $result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,16 @@
*/
declare(strict_types=1);


namespace Magento\BundleGraphQl\Model\Resolver\Product\Fields;

use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Bundle\Model\Product\Type as Bundle;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\EnumLookup;
use Magento\Framework\GraphQl\Query\Resolver\Value;
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
use Magento\Framework\GraphQl\Query\ResolverInterface;

/**
* {@inheritdoc}
* @inheritdoc
*/
class PriceView implements ResolverInterface
{
Expand All @@ -26,43 +23,30 @@ class PriceView implements ResolverInterface
*/
private $enumLookup;

/**
* @var ValueFactory
*/
private $valueFactory;

/**
* @param EnumLookup $enumLookup
* @param ValueFactory $valueFactory
*/
public function __construct(EnumLookup $enumLookup, ValueFactory $valueFactory)
public function __construct(EnumLookup $enumLookup)
{
$this->enumLookup = $enumLookup;
$this->valueFactory = $valueFactory;
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
): Value {
$result = function () {
return null;
};
) {
$result = null;
if ($value['type_id'] === Bundle::TYPE_CODE) {
$result = isset($value['price_view'])
? $this->enumLookup->getEnumValueFromField('PriceViewEnum', $value['price_view']) : null;
}

return $this->valueFactory->create(
function () use ($result) {
return $result;
}
);
return $result;
}
}
Loading

0 comments on commit 8984c54

Please sign in to comment.