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