Passed
Pull Request — master (#321)
by Christoffer
04:21 queued 01:06
created

SerialExecutionStrategy   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 30
rs 10
c 0
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A executeFields() 0 25 3
1
<?php
2
3
namespace Digia\GraphQL\Execution\Strategy;
4
5
use Digia\GraphQL\Execution\UndefinedFieldException;
6
use Digia\GraphQL\Type\Definition\ObjectType;
7
use React\Promise\PromiseInterface;
8
use function Digia\GraphQL\Util\promiseReduce;
9
10
/**
11
 * Implements the "Evaluating selection sets" section of the spec
12
 * for "write" mode.
13
 *
14
 * Class SerialExecutionStrategy
15
 * @package Digia\GraphQL\Execution\Strategy
16
 */
17
class SerialExecutionStrategy extends AbstractExecutionStrategy
18
{
19
    /**
20
     * @inheritdoc
21
     */
22
    public function executeFields(ObjectType $parentType, $rootValue, array $path, array $fields)
23
    {
24
        return promiseReduce(
25
            \array_keys($fields),
26
            function ($results, $fieldName) use ($parentType, $rootValue, $path, $fields) {
27
                $fieldNodes  = $fields[$fieldName];
28
                $fieldPath   = $path;
29
                $fieldPath[] = $fieldName;
30
31
                try {
32
                    $result = $this->resolveField($parentType, $rootValue, $fieldNodes, $fieldPath);
33
                } catch (UndefinedFieldException $exception) {
34
                    return null;
35
                }
36
37
                if ($result instanceof PromiseInterface) {
38
                    return $result->then(function ($resolvedResult) use ($fieldName, $results) {
39
                        $results[$fieldName] = $resolvedResult;
40
                        return $results;
41
                    });
42
                }
43
44
                $results[$fieldName] = $result;
45
46
                return $results;
47
            }
48
        );
49
    }
50
}
51