1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Overblog\GraphQLBundle\GraphQL\Relay\Mutation; |
6
|
|
|
|
7
|
|
|
use Closure; |
8
|
|
|
use GraphQL\Executor\Promise\Promise; |
9
|
|
|
use GraphQL\Executor\Promise\PromiseAdapter; |
10
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
11
|
|
|
use Overblog\GraphQLBundle\Definition\ArgumentFactory; |
12
|
|
|
use Overblog\GraphQLBundle\Definition\ArgumentInterface; |
13
|
|
|
use Overblog\GraphQLBundle\Definition\Resolver\AliasedInterface; |
14
|
|
|
use Overblog\GraphQLBundle\Definition\Resolver\QueryInterface; |
15
|
|
|
use function is_array; |
16
|
|
|
use function is_object; |
17
|
|
|
|
18
|
|
|
final class MutationFieldQuery implements QueryInterface, AliasedInterface |
19
|
|
|
{ |
20
|
|
|
private PromiseAdapter $promiseAdapter; |
21
|
|
|
private ArgumentFactory $argumentFactory; |
22
|
|
|
|
23
|
|
|
public function __construct(PromiseAdapter $promiseAdapter, ArgumentFactory $argumentFactory) |
24
|
|
|
{ |
25
|
|
|
$this->promiseAdapter = $promiseAdapter; |
26
|
|
|
$this->argumentFactory = $argumentFactory; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param mixed $context |
31
|
|
|
*/ |
32
|
|
|
public function __invoke(ArgumentInterface $args, $context, ResolveInfo $info, Closure $mutateAndGetPayloadCallback): Promise |
33
|
|
|
{ |
34
|
|
|
$input = $this->argumentFactory->create($args['input']); |
35
|
|
|
|
36
|
|
|
return $this->promiseAdapter->createFulfilled($mutateAndGetPayloadCallback($input, $context, $info)) |
37
|
|
|
->then(function ($payload) use ($input) { |
38
|
|
|
$this->setObjectOrArrayValue($payload, 'clientMutationId', $input['clientMutationId']); |
39
|
|
|
|
40
|
|
|
return $payload; |
41
|
|
|
}); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
public static function getAliases(): array |
48
|
|
|
{ |
49
|
|
|
return ['__invoke' => 'relay_mutation_field']; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param object|array $objectOrArray |
54
|
|
|
* @param mixed $value |
55
|
|
|
*/ |
56
|
|
|
private function setObjectOrArrayValue(&$objectOrArray, string $fieldName, $value): void |
57
|
|
|
{ |
58
|
|
|
if (is_array($objectOrArray)) { |
59
|
|
|
$objectOrArray[$fieldName] = $value; |
60
|
|
|
} elseif (is_object($objectOrArray)) { |
61
|
|
|
$objectOrArray->$fieldName = $value; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|