|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Overblog\GraphQLBundle\Relay\Mutation; |
|
6
|
|
|
|
|
7
|
|
|
use Overblog\GraphQLBundle\Definition\Builder\MappingInterface; |
|
8
|
|
|
use \InvalidArgumentException; |
|
9
|
|
|
|
|
10
|
|
|
final class MutationFieldDefinition implements MappingInterface |
|
11
|
5 |
|
{ |
|
12
|
|
|
private const KEY_MUTATE_GET_PAYLOAD = 'mutateAndGetPayload'; |
|
13
|
5 |
|
|
|
14
|
1 |
|
public function toMappingDefinition(array $config): array |
|
15
|
|
|
{ |
|
16
|
|
|
$mutateAndGetPayload = $this->cleanMutateAndGetPayload($config); |
|
17
|
4 |
|
|
|
18
|
4 |
|
return [ |
|
19
|
4 |
|
'type' => $this->extractPayloadType($config), |
|
20
|
|
|
'args' => [ |
|
21
|
|
|
'input' => ['type' => $this->extractInputType($config)], |
|
22
|
4 |
|
], |
|
23
|
|
|
'resolve' => "@=resolver('relay_mutation_field', [args, context, info, mutateAndGetPayloadCallback($mutateAndGetPayload)])", |
|
24
|
4 |
|
]; |
|
25
|
|
|
} |
|
26
|
4 |
|
|
|
27
|
|
|
private function cleanMutateAndGetPayload($config): string |
|
28
|
|
|
{ |
|
29
|
|
|
$mutateAndGetPayload = $config[self::KEY_MUTATE_GET_PAYLOAD] ?? null; |
|
30
|
4 |
|
$this->ensureValidMutateAndGetPayloadConfiguration($mutateAndGetPayload); |
|
31
|
|
|
|
|
32
|
4 |
|
if (\is_string($mutateAndGetPayload)) { |
|
33
|
3 |
|
return \substr($mutateAndGetPayload, 2); |
|
34
|
|
|
} |
|
35
|
3 |
|
|
|
36
|
|
|
return \json_encode($mutateAndGetPayload); |
|
37
|
|
|
} |
|
38
|
4 |
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param mixed $mutateAndGetPayload |
|
41
|
|
|
* @throws InvalidArgumentException |
|
42
|
|
|
*/ |
|
43
|
|
|
private function ensureValidMutateAndGetPayloadConfiguration($mutateAndGetPayload): void |
|
44
|
|
|
{ |
|
45
|
|
|
if (is_string($mutateAndGetPayload) && \strpos($mutateAndGetPayload, '@=') === 0) { |
|
46
|
|
|
return; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
if ($mutateAndGetPayload === null) { |
|
50
|
|
|
throw new InvalidArgumentException('Mutation "' . self::KEY_MUTATE_GET_PAYLOAD . '" config is required.'); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
if (is_string($mutateAndGetPayload)) { |
|
54
|
|
|
throw new InvalidArgumentException('Cannot parse "' . self::KEY_MUTATE_GET_PAYLOAD . '" configuration string.'); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if (! is_array($mutateAndGetPayload)) { |
|
58
|
|
|
throw new InvalidArgumentException('Invalid format for "' . self::KEY_MUTATE_GET_PAYLOAD . '" configuration.'); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
private function extractPayloadType(array $config): ?string |
|
63
|
|
|
{ |
|
64
|
|
|
return isset($config['payloadType']) && \is_string($config['payloadType']) ? $config['payloadType'] : null; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
private function extractInputType(array $config): ?string |
|
68
|
|
|
{ |
|
69
|
|
|
return isset($config['inputType']) && \is_string($config['inputType']) ? $config['inputType'].'!' : null; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|