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

Queryをプラグイン/カスタマイズで追加できるようにする #47

Merged
merged 1 commit into from
Jul 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 12 additions & 0 deletions DependencyInjection/Compiler/ApiCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use League\OAuth2\Server\CryptKey;
use Plugin\Api\GraphQL\AllowList;
use Plugin\Api\GraphQL\Query;
use Plugin\Api\GraphQL\Types;
use Plugin\Api\Service\WebHookEvents;
use Plugin\Api\Service\WebHookTrigger;
Expand All @@ -29,6 +30,7 @@ public function process(ContainerBuilder $container)
$this->configureTrigger($container);
$this->configureAllowList($container);
$this->configureKeyPair($container);
$this->configureQuery($container);

$plugins = $container->getParameter('eccube.plugins.enabled');
if (!in_array('Api', $plugins)) {
Expand All @@ -39,6 +41,16 @@ public function process(ContainerBuilder $container)
}
}

private function configureQuery(ContainerBuilder $container)
{
$serviceDef = $container->getDefinition('api.queries');
foreach ($container->getDefinitions() as $definition) {
if (is_subclass_of($definition->getClass(), Query::class)) {
$serviceDef->addMethodCall('append', [$definition]);
}
}
}

private function configureTrigger(ContainerBuilder $container)
{
$serviceDef = $container->getDefinition(WebHookEvents::class);
Expand Down
27 changes: 27 additions & 0 deletions GraphQL/Query.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Plugin\Api\GraphQL;

interface Query
{
/**
* @return string
*/
public function getName();

/**
* @return array
*/
public function getQuery();
}
48 changes: 48 additions & 0 deletions GraphQL/Query/CustomersQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Plugin\Api\GraphQL\Query;

use Eccube\Entity\Customer;
use Eccube\Form\Type\Admin\SearchCustomerType;
use Eccube\Repository\CustomerRepository;
use Plugin\Api\GraphQL\SearchFormQuery;

class CustomersQuery extends SearchFormQuery
{
/**
* @var CustomerRepository
*/
private $customerRepository;

/**
* CustomersQuery constructor.
* @param CustomerRepository $customerRepository
*/
public function __construct(CustomerRepository $customerRepository)
{
$this->customerRepository = $customerRepository;
}

public function getName()
{
return 'customers';
}

public function getQuery()
{
return $this->createQuery(Customer::class, SearchCustomerType::class, function ($searchData) {
return $this->customerRepository->getQueryBuilderBySearchData($searchData);
});
}
}
49 changes: 49 additions & 0 deletions GraphQL/Query/OrdersQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Plugin\Api\GraphQL\Query;

use Eccube\Entity\Order;
use Eccube\Form\Type\Admin\SearchOrderType;
use Eccube\Repository\OrderRepository;
use Plugin\Api\GraphQL\SearchFormQuery;

class OrdersQuery extends SearchFormQuery
{
/**
* @var OrderRepository
*/
private $orderRepository;

/**
* OrdersQuery constructor.
*
* @param $orderRepository
*/
public function __construct(OrderRepository $orderRepository)
{
$this->orderRepository = $orderRepository;
}

public function getName()
{
return 'orders';
}

public function getQuery()
{
return $this->createQuery(Order::class, SearchOrderType::class, function ($searchData) {
return $this->orderRepository->getQueryBuilderBySearchDataForAdmin($searchData);
});
}
}
49 changes: 49 additions & 0 deletions GraphQL/Query/ProductsQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Plugin\Api\GraphQL\Query;

use Eccube\Entity\Product;
use Eccube\Form\Type\Admin\SearchProductType;
use Eccube\Repository\ProductRepository;
use Plugin\Api\GraphQL\SearchFormQuery;

class ProductsQuery extends SearchFormQuery
{
/**
* @var ProductRepository
*/
private $productRepository;

/**
* ProductQuery constructor.
*
* @param $productRepository
*/
public function __construct(ProductRepository $productRepository)
{
$this->productRepository = $productRepository;
}

public function getName()
{
return 'products';
}

public function getQuery()
{
return $this->createQuery(Product::class, SearchProductType::class, function ($searchData) {
return $this->productRepository->getQueryBuilderBySearchDataForAdmin($searchData);
});
}
}
158 changes: 8 additions & 150 deletions GraphQL/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,168 +13,26 @@

namespace Plugin\Api\GraphQL;

use Eccube\Common\EccubeConfig;
use Eccube\Entity\Customer;
use Eccube\Entity\Order;
use Eccube\Entity\Product;
use Eccube\Form\Type\Admin\SearchCustomerType;
use Eccube\Form\Type\Admin\SearchOrderType;
use Eccube\Form\Type\Admin\SearchProductType;
use Eccube\Repository\CustomerRepository;
use Eccube\Repository\OrderRepository;
use Eccube\Repository\ProductRepository;
use Eccube\Util\StringUtil;
use ArrayObject;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use Knp\Component\Pager\Paginator;
use Plugin\Api\GraphQL\Error\FormInvalidException;
use Plugin\Api\GraphQL\Type\ConnectionType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\FormFactory;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Validator\Constraints as Assert;

class Schema extends \GraphQL\Type\Schema
{
/**
* @var Types
*/
private $types;

/**
* @var ProductRepository
*/
private $productRepository;

/**
* @var OrderRepository
*/
private $orderRepository;

/**
* @var CustomerRepository
*/
private $customerRepository;

/**
* @var Paginator
*/
private $paginator;

/**
* @var EccubeConfig
*/
private $eccubeConfig;
/**
* @var FormFactory
*/
private $formFactory;

public function __construct(
Types $types,
ProductRepository $productRepository,
OrderRepository $orderRepository,
CustomerRepository $customerRepository,
Paginator $paginator,
EccubeConfig $eccubeConfig,
FormFactory $formFactory
ArrayObject $queries
) {
$this->types = $types;
$this->productRepository = $productRepository;
$this->orderRepository = $orderRepository;
$this->customerRepository = $customerRepository;
$this->paginator = $paginator;
$this->eccubeConfig = $eccubeConfig;
$this->formFactory = $formFactory;

parent::__construct([
'query' => new ObjectType([
'name' => 'Query',
'fields' => [
'products' => $this->createQuery(Product::class, SearchProductType::class, function ($searchData) {
return $this->productRepository->getQueryBuilderBySearchDataForAdmin($searchData);
}),
'orders' => $this->createQuery(Order::class, SearchOrderType::class, function ($searchData) {
return $this->orderRepository->getQueryBuilderBySearchDataForAdmin($searchData);
}),
'customers' => $this->createQuery(Customer::class, SearchCustomerType::class, function ($searchData) {
return $this->customerRepository->getQueryBuilderBySearchData($searchData);
}),
],
'typeLoader' => function ($name) {
return $this->types->get($name);
'fields' => array_reduce($queries->getArrayCopy(), function ($acc, Query $query) {
$acc[$query->getName()] = $query->getQuery();
return $acc;
}, []),
'typeLoader' => function ($name) use ($types) {
return $types->get($name);
},
]),
]);
}

private function createQuery($entityClass, $searchFormType, $resolver)
{
$builder = $this->formFactory->createBuilder($searchFormType, null, ['csrf_protection' => false]);

// paging のためのフォームを追加
$builder->add('page', IntegerType::class, [
'label' => 'api.args.page.description',
'required' => false,
'data' => 1,
'constraints' => [
new Assert\Regex([
'pattern' => "/^\d+$/u",
'message' => 'form_error.numeric_only',
]),
],
])->add('limit', IntegerType::class, [
'label' => 'api.args.limit.description',
'required' => false,
'data' => $this->eccubeConfig->get('eccube_default_page_count'),
'constraints' => [
new Assert\Regex([
'pattern' => "/^\d+$/u",
'message' => 'form_error.numeric_only',
]),
],
]);

$args = array_reduce($builder->getForm()->all(), function ($acc, $form) {
/* @var FormInterface $form */
$formConfig = $form->getConfig();
$type = $formConfig->getType()->getInnerType() instanceof IntegerType ? Type::int() : Type::string();
if ($formConfig->getOption('multiple')) {
$type = Type::listOf($type);
}
if ($formConfig->getOption('required') && !$formConfig->getOption('multiple')) {
$type = Type::nonNull($type);
}
$defaultValue = $form->getViewData();
$acc[$form->getName()] = [
'type' => $type,
'defaultValue' => StringUtil::isNotBlank($defaultValue) ? $defaultValue : null,
'description' => $formConfig->getOption('label') ? trans($formConfig->getOption('label')) : null,
];

return $acc;
}, []);

return [
'type' => new ConnectionType($entityClass, $this->types),
'args' => $args,
'resolve' => function ($root, $args) use ($builder, $resolver) {
$form = $builder->getForm();
$form->submit($args);

if (!$form->isValid()) {
$message = '';
foreach ($form->getErrors(true) as $error) {
$message .= sprintf('%s: %s;', $error->getOrigin()->getName(), $error->getMessage());
}

throw new FormInvalidException($message);
}

$data = $form->getData();

return $this->paginator->paginate($resolver($data), $args['page'], $args['limit']);
},
];
}
}
Loading