Completed
Pull Request — master (#45)
by Daniel
04:14
created

RelayMutation   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 6
dl 0
loc 55
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C buildMutation() 0 40 7
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