1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the OverblogGraphQLBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Overblog <http://github.com/overblog/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Overblog\GraphQLBundle\Relay\Mutation; |
13
|
|
|
|
14
|
|
|
use GraphQL\Type\Definition\Config; |
15
|
|
|
use GraphQL\Type\Definition\Type; |
16
|
|
|
use GraphQL\Utils; |
17
|
|
|
use Overblog\GraphQLBundle\Definition\FieldInterface; |
18
|
|
|
use Overblog\GraphQLBundle\Definition\MergeFieldTrait; |
19
|
|
|
|
20
|
|
|
class MutationField implements FieldInterface |
21
|
|
|
{ |
22
|
|
|
use MergeFieldTrait; |
23
|
|
|
|
24
|
|
|
public function toFieldDefinition(array $config) |
25
|
|
|
{ |
26
|
|
|
Utils::invariant(!empty($config['name']), 'Every type is expected to have name'); |
27
|
|
|
|
28
|
|
|
Config::validate($config, [ |
29
|
|
|
'name' => Config::STRING | Config::REQUIRED, |
30
|
|
|
'mutateAndGetPayload' => Config::CALLBACK | Config::REQUIRED, |
31
|
|
|
'payloadType' => Config::OBJECT_TYPE | Config::CALLBACK | Config::REQUIRED, |
32
|
|
|
'inputType' => Config::INPUT_TYPE | Config::CALLBACK | Config::REQUIRED, |
33
|
|
|
'description' => Config::STRING, |
34
|
|
|
]); |
35
|
|
|
|
36
|
|
|
$name = $config['name']; |
37
|
|
|
|
38
|
|
|
$mutateAndGetPayload = $config['mutateAndGetPayload']; |
39
|
|
|
$description = isset($config['description']) ? $config['description'] : null; |
40
|
|
|
$payloadType = $config['payloadType']; |
41
|
|
|
$inputType = $config['inputType']; |
42
|
|
|
|
43
|
|
|
return [ |
44
|
|
|
'name' => $name, |
45
|
|
|
'description' => $description, |
46
|
|
|
'type' => $payloadType, |
47
|
|
|
'args' => [ |
48
|
|
|
'input' => ['type' => Type::nonNull($inputType)], |
49
|
|
|
], |
50
|
|
|
'resolve' => function ($_, $input, $info) use ($mutateAndGetPayload, $name) { |
51
|
|
|
$payload = $mutateAndGetPayload($input['input'], $info); |
52
|
|
|
$payload['clientMutationId'] = $input['input']['clientMutationId']; |
53
|
|
|
|
54
|
|
|
return $payload; |
55
|
|
|
}, |
56
|
|
|
]; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|