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
|
|
|
* |
42
|
|
|
* @throws InvalidArgumentException |
43
|
|
|
*/ |
44
|
10 |
|
private function ensureValidMutateAndGetPayloadConfiguration($mutateAndGetPayload): void |
45
|
|
|
{ |
46
|
10 |
|
if (\is_string($mutateAndGetPayload) && 0 === \strpos($mutateAndGetPayload, '@=')) { |
47
|
5 |
|
return; |
48
|
|
|
} |
49
|
|
|
|
50
|
7 |
|
if (null === $mutateAndGetPayload) { |
51
|
2 |
|
throw new InvalidArgumentException(\sprintf('Mutation "%s" config is required.', self::KEY_MUTATE_GET_PAYLOAD)); |
52
|
|
|
} |
53
|
|
|
|
54
|
5 |
|
if (\is_string($mutateAndGetPayload)) { |
55
|
1 |
|
throw new InvalidArgumentException(\sprintf('Cannot parse "%s" configuration string.', self::KEY_MUTATE_GET_PAYLOAD)); |
56
|
|
|
} |
57
|
|
|
|
58
|
4 |
|
if (!\is_array($mutateAndGetPayload)) { |
59
|
1 |
|
throw new InvalidArgumentException(\sprintf('Invalid format for "%s" configuration.', self::KEY_MUTATE_GET_PAYLOAD)); |
60
|
|
|
} |
61
|
3 |
|
} |
62
|
|
|
|
63
|
6 |
|
private function extractPayloadType(array $config): ?string |
64
|
|
|
{ |
65
|
6 |
|
return isset($config['payloadType']) && \is_string($config['payloadType']) ? $config['payloadType'] : null; |
66
|
|
|
} |
67
|
|
|
|
68
|
6 |
|
private function extractInputType(array $config): ?string |
69
|
|
|
{ |
70
|
6 |
|
return isset($config['inputType']) && \is_string($config['inputType']) ? $config['inputType'].'!' : null; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|