diff --git a/GraphQL/Mutation/AbstractMutation.php b/GraphQL/Mutation/AbstractMutation.php index f321b63..64cb3cd 100644 --- a/GraphQL/Mutation/AbstractMutation.php +++ b/GraphQL/Mutation/AbstractMutation.php @@ -58,6 +58,7 @@ protected function getInputObject(FormInterface $form): InputObjectType { $fields = []; $this->convertArgs($form, $fields); + return new InputObjectType([ 'name' => $form->getName().'Input', 'fields' => $fields, @@ -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); } @@ -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()) { diff --git a/GraphQL/Mutation/PurchaseMutation.php b/GraphQL/Mutation/PurchaseMutation.php index d1ea77d..838b35a 100644 --- a/GraphQL/Mutation/PurchaseMutation.php +++ b/GraphQL/Mutation/PurchaseMutation.php @@ -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; @@ -55,8 +54,8 @@ public function __construct( PurchaseFlow $cartPurchaseFlow, PaymentMethodLocator $locator, MailService $mailService, + FormFactoryInterface $formFactory, ) { - $this->types = $types; $this->entityManager = $entityManager; $this->cartService = $cartService; $this->orderHelper = $orderHelper; @@ -64,6 +63,8 @@ public function __construct( $this->cartPurchaseFlow = $cartPurchaseFlow; $this->locator = $locator; $this->mailService = $mailService; + $this->setTypes($types); + $this->setFormFactory($formFactory); } public function getName(): string @@ -71,16 +72,17 @@ 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) { diff --git a/GraphQL/Mutation/ShoppingMutation.php b/GraphQL/Mutation/ShoppingMutation.php index cab04ca..3c5992b 100644 --- a/GraphQL/Mutation/ShoppingMutation.php +++ b/GraphQL/Mutation/ShoppingMutation.php @@ -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; @@ -47,13 +48,15 @@ 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 @@ -61,21 +64,23 @@ 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();