Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added logic for graphql API #405

Open
wants to merge 15 commits into
base: develop
Choose a base branch
from
Open
4 changes: 4 additions & 0 deletions Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

//Event list
use Lovata\Shopaholic\Classes\Event\ExtendMenuHandler;
//API Events
use Lovata\Shopaholic\Classes\Api\ExtendFrontendTypeClassList;
//Brand events
use Lovata\Shopaholic\Classes\Event\Brand\BrandModelHandler;
//Category events
Expand Down Expand Up @@ -165,6 +167,8 @@ public function boot()
protected function addEventListener()
{
Event::subscribe(ExtendMenuHandler::class);
//API Events
Event::subscribe(ExtendFrontendTypeClassList::class);
//Brand events
Event::subscribe(BrandModelHandler::class);
//Category events
Expand Down
64 changes: 64 additions & 0 deletions classes/api/ExtendFrontendTypeClassList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php namespace Lovata\Shopaholic\Classes\Api;

use Lovata\Shopaholic\Classes\Api\Type\PriceDataType;
use Lovata\Toolbox\Classes\Api\Type\FrontendTypeFactory;
/** Item types */
use Lovata\Shopaholic\Classes\Api\Item\BrandItemType;
use Lovata\Shopaholic\Classes\Api\Item\CategoryItemShortType;
use Lovata\Shopaholic\Classes\Api\Item\CategoryItemType;
use Lovata\Shopaholic\Classes\Api\Item\CurrencyItemType;
use Lovata\Shopaholic\Classes\Api\Item\MeasureItemType;
use Lovata\Shopaholic\Classes\Api\Item\OfferItemType;
use Lovata\Shopaholic\Classes\Api\Item\ProductItemShortType;
use Lovata\Shopaholic\Classes\Api\Item\ProductItemType;
use Lovata\Shopaholic\Classes\Api\Item\PromoBlockItemType;
use Lovata\Shopaholic\Classes\Api\Item\TaxItemType;
/** Collection types */
use Lovata\Shopaholic\Classes\Api\Collection\BrandCollectionType;
use Lovata\Shopaholic\Classes\Api\Collection\CategoryCollectionType;
use Lovata\Shopaholic\Classes\Api\Collection\CurrencyCollectionType;
use Lovata\Shopaholic\Classes\Api\Collection\OfferCollectionType;
use Lovata\Shopaholic\Classes\Api\Collection\ProductCollectionType;
use Lovata\Shopaholic\Classes\Api\Collection\PromoBlockCollectionType;
use Lovata\Shopaholic\Classes\Api\Collection\TaxCollectionType;

/**
* ExtendFrontendTypeClassList
* @package Lovata\Shopaholic\Classes\Api
*/
class ExtendFrontendTypeClassList
{
/**
* Add listeners
*/
public function subscribe()
{
FrontendTypeFactory::extend(function (FrontendTypeFactory $obTypeFactory) {
$arClassList = [
PriceDataType::class,
/** Item types */
BrandItemType::class,
CategoryItemShortType::class,
CategoryItemType::class,
CurrencyItemType::class,
MeasureItemType::class,
OfferItemType::class,
ProductItemShortType::class,
ProductItemType::class,
PromoBlockItemType::class,
TaxItemType::class,
/** Collection types */
BrandCollectionType::class,
CategoryCollectionType::class,
CurrencyCollectionType::class,
OfferCollectionType::class,
PromoBlockCollectionType::class,
ProductCollectionType::class,
TaxCollectionType::class,
];

$obTypeFactory->addTypeClass($arClassList);
$obTypeFactory->addQueryClass($arClassList);
});
}
}
50 changes: 50 additions & 0 deletions classes/api/collection/BrandCollectionType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php namespace Lovata\Shopaholic\Classes\Api\Collection;

use GraphQL\Type\Definition\Type;

use Lovata\Shopaholic\Classes\Api\Item\BrandItemType;
use Lovata\Shopaholic\Classes\Collection\BrandCollection;

use Lovata\Toolbox\Classes\Api\Collection\AbstractCollectionType;
use Lovata\Toolbox\Classes\Api\Type\TypeFactory;

/**
* Class BrandCollectionType
* @package Lovata\Shopaholic\Classes\Api\Collection
*/
class BrandCollectionType extends AbstractCollectionType
{
const COLLECTION_CLASS = BrandCollection::class;
const TYPE_ALIAS = 'brand_list';

/** @var BrandCollectionType */
protected static $instance;

/**
* Get type fields
* @return array
* @throws \GraphQL\Error\Error
*/
protected function getFieldList(): array
{
$arFieldList = parent::getFieldList();
$arFieldList['list'] = Type::listOf(TypeFactory::instance()->get(BrandItemType::TYPE_ALIAS));
$arFieldList['item'] = TypeFactory::instance()->get(BrandItemType::TYPE_ALIAS);
$arFieldList['id'] = Type::id();

return $arFieldList;
}

/**
* Get config for "args" attribute
* @return array|null
*/
protected function getArguments(): ?array
{
$arArgumentList = parent::getArguments();
$arArgumentList['category'] = Type::id();
$arArgumentList['search'] = Type::string();

return $arArgumentList;
}
}
49 changes: 49 additions & 0 deletions classes/api/collection/CategoryCollectionType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php namespace Lovata\Shopaholic\Classes\Api\Collection;

use GraphQL\Type\Definition\Type;

use Lovata\Shopaholic\Classes\Api\Item\CategoryItemType;
use Lovata\Shopaholic\Classes\Collection\CategoryCollection;

use Lovata\Toolbox\Classes\Api\Collection\AbstractCollectionType;
use Lovata\Toolbox\Classes\Api\Type\TypeFactory;

/**
* Class CategoryCollectionType
* @package Lovata\Shopaholic\Classes\Api\Collection
*/
class CategoryCollectionType extends AbstractCollectionType
{
const COLLECTION_CLASS = CategoryCollection::class;
const TYPE_ALIAS = 'category_list';

/** @var CategoryCollectionType */
protected static $instance;

/**
* Get type fields
* @return array
* @throws \GraphQL\Error\Error
*/
protected function getFieldList(): array
{
$arFieldList = parent::getFieldList();
$arFieldList['list'] = Type::listOf(TypeFactory::instance()->get(CategoryItemType::TYPE_ALIAS));
$arFieldList['item'] = TypeFactory::instance()->get(CategoryItemType::TYPE_ALIAS);
$arFieldList['id'] = Type::id();

return $arFieldList;
}

/**
* Get config for "args" attribute
* @return array|null
*/
protected function getArguments(): ?array
{
$arArgumentList = parent::getArguments();
$arArgumentList['search'] = Type::string();

return $arArgumentList;
}
}
37 changes: 37 additions & 0 deletions classes/api/collection/CurrencyCollectionType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php namespace Lovata\Shopaholic\Classes\Api\Collection;

use GraphQL\Type\Definition\Type;

use Lovata\Shopaholic\Classes\Api\Item\CurrencyItemType;
use Lovata\Shopaholic\Classes\Collection\CurrencyCollection;

use Lovata\Toolbox\Classes\Api\Collection\AbstractCollectionType;
use Lovata\Toolbox\Classes\Api\Type\TypeFactory;

/**
* Class CurrencyCollectionType
* @package Lovata\Shopaholic\Classes\Api\Collection
*/
class CurrencyCollectionType extends AbstractCollectionType
{
const COLLECTION_CLASS = CurrencyCollection::class;
const TYPE_ALIAS = 'currency_list';

/** @var CurrencyCollectionType */
protected static $instance;

/**
* Get type fields
* @return array
* @throws \GraphQL\Error\Error
*/
protected function getFieldList(): array
{
$arFieldList = parent::getFieldList();
$arFieldList['list'] = Type::listOf(TypeFactory::instance()->get(CurrencyItemType::TYPE_ALIAS));
$arFieldList['item'] = TypeFactory::instance()->get(CurrencyItemType::TYPE_ALIAS);
$arFieldList['id'] = Type::id();

return $arFieldList;
}
}
50 changes: 50 additions & 0 deletions classes/api/collection/OfferCollectionType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php namespace Lovata\Shopaholic\Classes\Api\Collection;

use GraphQL\Type\Definition\Type;

use Lovata\Shopaholic\Classes\Api\Item\OfferItemType;
use Lovata\Shopaholic\Classes\Collection\OfferCollection;

use Lovata\Toolbox\Classes\Api\Collection\AbstractCollectionType;
use Lovata\Toolbox\Classes\Api\Type\TypeFactory;

/**
* Class OfferCollectionType
* @package Lovata\Shopaholic\Classes\Api\Collection
*/
class OfferCollectionType extends AbstractCollectionType
{
const COLLECTION_CLASS = OfferCollection::class;
const TYPE_ALIAS = 'offer_list';

/** @var OfferCollectionType */
protected static $instance;

/**
* Get type fields
* @return array
* @throws \GraphQL\Error\Error
*/
protected function getFieldList(): array
{
$arFieldList = parent::getFieldList();
$arFieldList['list'] = Type::listOf(TypeFactory::instance()->get(OfferItemType::TYPE_ALIAS));
$arFieldList['item'] = TypeFactory::instance()->get(OfferItemType::TYPE_ALIAS);
$arFieldList['id'] = Type::id();

return $arFieldList;
}

/**
* Get config for "args" attribute
* @return array|null
*/
protected function getArguments(): ?array
{
$arArgumentList = parent::getArguments();
$arArgumentList['priceTypeId'] = Type::id();
$arArgumentList['sort'] = Type::string();

return $arArgumentList;
}
}
73 changes: 73 additions & 0 deletions classes/api/collection/ProductCollectionType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php namespace Lovata\Shopaholic\Classes\Api\Collection;

use Illuminate\Support\Arr;

use GraphQL\Type\Definition\Type;

use Lovata\Shopaholic\Classes\Api\Item\OfferItemType;
use Lovata\Shopaholic\Classes\Api\Item\ProductItemType;
use Lovata\Shopaholic\Classes\Collection\ProductCollection;

use Lovata\Toolbox\Classes\Api\Collection\AbstractCollectionType;
use Lovata\Toolbox\Classes\Api\Type\Custom\Type as CustomType;
use Lovata\Toolbox\Classes\Api\Type\TypeFactory;

/**
* Class ProductCollectionType
* @package Lovata\Shopaholic\Classes\Api\Collection
*/
class ProductCollectionType extends AbstractCollectionType
{
const COLLECTION_CLASS = ProductCollection::class;
const TYPE_ALIAS = 'product_list';

/** @var ProductCollectionType */
protected static $instance;

/**
* Get type fields
* @return array
* @throws \GraphQL\Error\Error
*/
protected function getFieldList(): array
{
$arFieldList = parent::getFieldList();
$arFieldList['list'] = Type::listOf(TypeFactory::instance()->get(ProductItemType::TYPE_ALIAS));
$arFieldList['item'] = TypeFactory::instance()->get(OfferItemType::TYPE_ALIAS);
$arFieldList['id'] = Type::id();

return $arFieldList;
}

/**
* Get config for "args" attribute
* @return array|null
*/
protected function getArguments(): ?array
{
$arArgumentList = parent::getArguments();
$arArgumentList['brand'] = Type::id();
$arArgumentList['categoryList'] = CustomType::array();
$arArgumentList['categoryWithChildren'] = Type::boolean();
$arArgumentList['priceTypeId'] = Type::id();
$arArgumentList['getOfferMaxPrice'] = Type::string();
$arArgumentList['getOfferMinPrice'] = Type::string();
$arArgumentList['promo'] = Type::id();
$arArgumentList['promoBlock'] = Type::id();
$arArgumentList['sort'] = Type::string();

return $arArgumentList;
}

protected function getCategoryParam($arArgumentList)
{
$arResult = Arr::get($arArgumentList, 'categoryList');
$bWithChildren = (boolean) Arr::get($arArgumentList, 'categoryWithChildren');

if ($bWithChildren) {
$arResult[] = true;
}

return $arResult;
}
}
37 changes: 37 additions & 0 deletions classes/api/collection/PromoBlockCollectionType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php namespace Lovata\Shopaholic\Classes\Api\Collection;

use GraphQL\Type\Definition\Type;

use Lovata\Shopaholic\Classes\Api\Item\PromoBlockItemType;
use Lovata\Shopaholic\Classes\Collection\PromoBlockCollection;

use Lovata\Toolbox\Classes\Api\Collection\AbstractCollectionType;
use Lovata\Toolbox\Classes\Api\Type\TypeFactory;

/**
* Class PromoBlockCollectionType
* @package Lovata\Shopaholic\Classes\Api\Collection
*/
class PromoBlockCollectionType extends AbstractCollectionType
{
const COLLECTION_CLASS = PromoBlockCollection::class;
const TYPE_ALIAS = 'promo_block_list';

/** @var PromoBlockCollectionType */
protected static $instance;

/**
* Get type fields
* @return array
* @throws \GraphQL\Error\Error
*/
protected function getFieldList(): array
{
$arFieldList = parent::getFieldList();
$arFieldList['list'] = Type::listOf(TypeFactory::instance()->get(PromoBlockItemType::TYPE_ALIAS));
$arFieldList['item'] = TypeFactory::instance()->get(PromoBlockItemType::TYPE_ALIAS);
$arFieldList['id'] = Type::id();

return $arFieldList;
}
}
Loading