1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Date: 17.05.16 |
4
|
|
|
* |
5
|
|
|
* @author Portey Vasil <[email protected]> |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Youshido\GraphQL\Relay; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
use Youshido\GraphQL\Execution\ResolveInfo; |
12
|
|
|
use Youshido\GraphQL\Field\Field; |
13
|
|
|
use Youshido\GraphQL\Field\InputField; |
14
|
|
|
use Youshido\GraphQL\Type\InputObject\InputObjectType; |
15
|
|
|
use Youshido\GraphQL\Type\NonNullType; |
16
|
|
|
use Youshido\GraphQL\Type\Object\AbstractObjectType; |
17
|
|
|
use Youshido\GraphQL\Type\Object\ObjectType; |
18
|
|
|
use Youshido\GraphQL\Type\Scalar\StringType; |
19
|
|
|
|
20
|
|
|
class RelayMutation |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param string $name |
25
|
|
|
* @param array $args |
26
|
|
|
* @param AbstractObjectType|array $output |
27
|
|
|
* @param callable $resolveFunction |
28
|
|
|
* |
29
|
|
|
* @return Field |
30
|
|
|
* |
31
|
|
|
* @throws \Exception |
32
|
|
|
*/ |
33
|
2 |
|
public static function buildMutation($name, array $args, $output, callable $resolveFunction) |
34
|
|
|
{ |
35
|
2 |
|
if (!is_array($output) || (is_object($output) && !($output instanceof AbstractObjectType))) { |
36
|
1 |
|
throw new \Exception('Output can be instance of AbstractObjectType or array of fields'); |
37
|
|
|
} |
38
|
|
|
|
39
|
1 |
|
return new Field([ |
40
|
1 |
|
'name' => $name, |
41
|
|
|
'args' => [ |
42
|
1 |
|
new InputField([ |
43
|
1 |
|
'name' => 'input', |
44
|
1 |
|
'type' => new NonNullType(new InputObjectType([ |
45
|
1 |
|
'name' => ucfirst($name) . 'Input', |
46
|
1 |
|
'fields' => array_merge( |
47
|
|
|
$args, |
48
|
1 |
|
[new InputField(['name' => 'clientMutationId', 'type' => new NonNullType(new StringType())])] |
49
|
|
|
) |
50
|
|
|
])) |
51
|
|
|
]) |
52
|
|
|
], |
53
|
1 |
|
'type' => new ObjectType([ |
54
|
1 |
|
'fields' => is_object($output) ? $output : array_merge( |
55
|
|
|
$output, |
56
|
1 |
|
[new Field(['name' => 'clientMutationId', 'type' => new NonNullType(new StringType())])] |
57
|
|
|
), |
58
|
1 |
|
'name' => ucfirst($name) . 'Payload' |
59
|
|
|
]), |
60
|
1 |
|
'resolve' => function ($value, $args, ResolveInfo $info) use ($resolveFunction) { |
61
|
|
|
$resolveValue = $resolveFunction($value, $args['input'], $args, $info); |
62
|
|
|
|
63
|
|
|
if (is_object($resolveValue)) { |
64
|
|
|
$resolveValue->clientMutationId = $args['input']['clientMutationId']; |
65
|
|
|
} elseif (is_array($resolveValue)) { |
66
|
|
|
$resolveValue['clientMutationId'] = $args['input']['clientMutationId']; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $resolveValue; |
70
|
1 |
|
} |
71
|
|
|
]); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
} |
75
|
|
|
|