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

ParallelExecutionStrategy::executeFields()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 8
nop 4
dl 0
loc 27
rs 9.4888
c 0
b 0
f 0
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\promiseForMap;
9
10
/**
11
 * Implements the "Evaluating selection sets" section of the spec
12
 * for "read" mode.
13
 *
14
 * Class ParallelExecutionStrategy
15
 * @package Digia\GraphQL\Execution\Strategy
16
 */
17
class ParallelExecutionStrategy extends AbstractExecutionStrategy
18
{
19
    /**
20
     * @inheritdoc
21
     */
22
    public function executeFields(ObjectType $parentType, $rootValue, array $path, array $fields)
23
    {
24
        $results            = [];
25
        $doesContainPromise = false;
26
27
        foreach ($fields as $fieldName => $fieldNodes) {
28
            $fieldPath   = $path;
29
            $fieldPath[] = $fieldName;
30
31
            try {
32
                $result = $this->resolveField($parentType, $rootValue, $fieldNodes, $fieldPath);
33
            } catch (UndefinedFieldException $exception) {
34
                continue;
35
            }
36
37
            $doesContainPromise  = $doesContainPromise || $result instanceof PromiseInterface;
38
            $results[$fieldName] = $result;
39
        }
40
41
        if (!$doesContainPromise) {
42
            return $results;
43
        }
44
45
        // Otherwise, results is a map from field name to the result of resolving that
46
        // field, which is possibly a promise. Return a promise that will return this
47
        // same map, but with any promises replaced with the values they resolved to.
48
        return promiseForMap($results);
49
    }
50
}
51