Skip to content

Commit

Permalink
Use extends AbstractMutation
Browse files Browse the repository at this point in the history
  • Loading branch information
nanasess committed Sep 13, 2023
1 parent f977999 commit 750c291
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 28 deletions.
17 changes: 11 additions & 6 deletions GraphQL/Mutation/AbstractMutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ protected function getInputObject(FormInterface $form): InputObjectType
{
$fields = [];
$this->convertArgs($form, $fields);

return new InputObjectType([
'name' => $form->getName().'Input',
'fields' => $fields,
Expand Down Expand Up @@ -133,16 +134,18 @@ protected function convertArgs(FormInterface $form, array &$args = []): void

public function getMutation(): array
{
$args = [];
$builder = $this->formFactory->createBuilder($this->getArgsType(), null, ['csrf_protection' => false]);
$form = $builder->getForm();
if ($form->getName() !== 'form') {
$args['input'] = [
'type' => $this->getInputObject($form),
];
}

return [
'type' => $this->types->get($this->getTypesClass()),
'args' => [
'input' => [
'type' => $this->getInputObject($form),
],
],
'args' => $args,
'resolve' => function ($value, array $args, $context, ResolveInfo $info) use ($form) {
return $this->resolver($value, $args, $context, $info, $form);
}
Expand Down Expand Up @@ -198,7 +201,9 @@ protected function convertFormValues(FormInterface $form, &$formValues = [], $i
protected function resolver($value, array $args, $context, ResolveInfo $info, FormInterface $form): mixed
{
$formValues = [];
$this->convertFormValues($form, $formValues, $args['input']);
if (array_key_exists('input', $args)) {
$this->convertFormValues($form, $formValues, $args['input']);
}

$form->submit($formValues);
if (!$form->isValid()) {
Expand Down
26 changes: 14 additions & 12 deletions GraphQL/Mutation/PurchaseMutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,12 @@
use Eccube\Service\PurchaseFlow\PurchaseFlow;
use Eccube\Service\PurchaseFlow\PurchaseFlowResult;
use Plugin\Api42\GraphQL\Error\InvalidArgumentException;
use Plugin\Api42\GraphQL\Mutation;
use Plugin\Api42\GraphQL\Types;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\FormFactoryInterface;

class PurchaseMutation implements Mutation
class PurchaseMutation extends AbstractMutation
{
private Types $types;

private EntityManager $entityManager;
private CartService $cartService;
private OrderHelper $orderHelper;
Expand All @@ -55,32 +54,35 @@ public function __construct(
PurchaseFlow $cartPurchaseFlow,
PaymentMethodLocator $locator,
MailService $mailService,
FormFactoryInterface $formFactory,
) {
$this->types = $types;
$this->entityManager = $entityManager;
$this->cartService = $cartService;
$this->orderHelper = $orderHelper;
$this->securityContext = $securityContext;
$this->cartPurchaseFlow = $cartPurchaseFlow;
$this->locator = $locator;
$this->mailService = $mailService;
$this->setTypes($types);
$this->setFormFactory($formFactory);
}

public function getName(): string
{
return 'purchaseMutation';
}

public function getMutation(): array
public function getTypesClass(): string
{
return Order::class;
}

public function getArgsType(): string
{
return [
'type' => $this->types->get(Order::class),
'args' => [],
'resolve' => [$this, 'purchaseMutation'],
];
return FormType::class;
}

public function purchaseMutation()
public function executeMutation($root, array $args): mixed
{
$Customer = $this->securityContext->getLoginUser();
if (!$Customer instanceof Customer) {
Expand Down
25 changes: 15 additions & 10 deletions GraphQL/Mutation/ShoppingMutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@
use Eccube\Service\PurchaseFlow\PurchaseFlowResult;
use Plugin\Api42\GraphQL\Error\InvalidArgumentException;
use Plugin\Api42\GraphQL\Error\ShoppingException;
use Plugin\Api42\GraphQL\Mutation;
use Plugin\Api42\GraphQL\Types;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\FormFactoryInterface;

class ShoppingMutation implements Mutation
class ShoppingMutation extends AbstractMutation
{
private Types $types;

Expand All @@ -47,35 +48,39 @@ public function __construct(
OrderHelper $orderHelper,
SecurityContext $securityContext,
PurchaseFlow $cartPurchaseFlow,
FormFactoryInterface $formFactory,
) {
$this->types = $types;
$this->entityManager = $entityManager;
$this->cartService = $cartService;
$this->orderHelper = $orderHelper;
$this->securityContext = $securityContext;
$this->cartPurchaseFlow = $cartPurchaseFlow;
$this->setTypes($types);
$this->setFormFactory($formFactory);
}

public function getName(): string
{
return 'orderMutation';
}

public function getMutation(): array
public function getTypesClass(): string
{
return [
'type' => $this->types->get(Order::class),
'args' => [],
'resolve' => [$this, 'orderMutation'],
];
return Order::class;
}

public function getArgsType(): string
{
return FormType::class;
}

/**
* @return Order|null
* @throws InvalidArgumentException
* @throws ORMException
* @throws ForeignKeyConstraintViolationException
*/
public function orderMutation($root, $args): ?Order
public function executeMutation($root, $args): mixed
{
$user = $this->securityContext->getLoginUser();

Expand Down

0 comments on commit 750c291

Please sign in to comment.