|
11 | 11 | use Magento\Framework\GraphQl\Config\ConfigElementInterface;
|
12 | 12 | use Magento\Framework\GraphQl\Config\Element\TypeInterface;
|
13 | 13 | use Magento\Framework\GraphQl\Exception\GraphQlInputException;
|
| 14 | +use Magento\Framework\GraphQl\Query\ResolverInterface; |
14 | 15 | use Magento\Framework\GraphQl\Schema\Type\Output\ElementMapper\Formatter\Fields;
|
| 16 | +use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; |
15 | 17 | use Magento\ReCaptchaValidationApi\Api\ValidatorInterface;
|
16 | 18 | use Magento\ReCaptchaWebapiApi\Api\WebapiValidationConfigProviderInterface;
|
17 | 19 | use Magento\ReCaptchaWebapiApi\Model\Data\EndpointFactory;
|
| 20 | +use Magento\Framework\GraphQl\Config\Element\Field; |
18 | 21 |
|
19 | 22 | /**
|
20 | 23 | * Validate ReCaptcha for GraphQl mutations.
|
@@ -62,35 +65,39 @@ public function __construct(
|
62 | 65 | /**
|
63 | 66 | * Validate ReCaptcha for mutations if needed.
|
64 | 67 | *
|
65 |
| - * @param Fields $subject |
66 |
| - * @param ConfigElementInterface $configElement |
| 68 | + * @param ResolverInterface $subject |
| 69 | + * @param Field $fieldInfo |
| 70 | + * @param mixed $context |
| 71 | + * @param ResolveInfo $resolveInfo |
67 | 72 | * @throws GraphQlInputException
|
68 | 73 | * @return void
|
69 | 74 | *
|
70 | 75 | * @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
71 | 76 | */
|
72 |
| - public function beforeFormat( |
73 |
| - Fields $subject, |
74 |
| - ConfigElementInterface $configElement |
| 77 | + public function beforeResolve( |
| 78 | + ResolverInterface $subject, |
| 79 | + Field $fieldInfo, |
| 80 | + $context, |
| 81 | + ResolveInfo $resolveInfo |
75 | 82 | ): void {
|
76 |
| - if ($configElement->getName() === 'Mutation' && $configElement instanceof TypeInterface) { |
77 |
| - foreach ($configElement->getFields() as $field) { |
78 |
| - $reCaptchaConfig = $this->configProvider->getConfigFor( |
79 |
| - $this->endpointFactory->create([ |
80 |
| - 'class' => ltrim($field->getResolver(), '\\'), |
81 |
| - 'method' => 'resolve', |
82 |
| - 'name' => $field->getName() |
83 |
| - ]) |
84 |
| - ); |
85 |
| - if ($reCaptchaConfig |
86 |
| - && !$this->validator->isValid( |
87 |
| - (string)$this->request->getHeader('X-ReCaptcha'), |
88 |
| - $reCaptchaConfig |
89 |
| - )->isValid() |
90 |
| - ) { |
91 |
| - throw new GraphQlInputException(__('ReCaptcha validation failed, please try again')); |
92 |
| - } |
93 |
| - } |
| 83 | + if ($resolveInfo->operation->operation !== 'mutation') { |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + $reCaptchaConfig = $this->configProvider->getConfigFor( |
| 88 | + $this->endpointFactory->create([ |
| 89 | + 'class' => ltrim($fieldInfo->getResolver(), '\\'), |
| 90 | + 'method' => 'resolve', |
| 91 | + 'name' => $fieldInfo->getName() |
| 92 | + ]) |
| 93 | + ); |
| 94 | + if ($reCaptchaConfig |
| 95 | + && !$this->validator->isValid( |
| 96 | + (string)$this->request->getHeader('X-ReCaptcha'), |
| 97 | + $reCaptchaConfig |
| 98 | + )->isValid() |
| 99 | + ) { |
| 100 | + throw new GraphQlInputException(__('ReCaptcha validation failed, please try again')); |
94 | 101 | }
|
95 | 102 | }
|
96 | 103 | }
|
0 commit comments