Completed
Push — master ( 2ab509...fc8247 )
by Quang
02:16
created

ExecutionContextBuilder   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 64
rs 10
c 0
b 0
f 0
wmc 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C buildContext() 0 51 10
1
<?php
2
3
namespace Digia\GraphQL\Execution;
4
5
use Digia\GraphQL\Error\GraphQLError;
6
use Digia\GraphQL\Language\AST\Node\DocumentNode;
7
use Digia\GraphQL\Language\AST\NodeKindEnum;
8
use Digia\GraphQL\Type\Schema;
9
10
/**
11
 * Class ExecutionContextBuilder
12
 * @package Digia\GraphQL\Execution
13
 */
14
class ExecutionContextBuilder
15
{
16
    /**
17
     * @param Schema        $schema
18
     * @param DocumentNode  $documentNode
19
     * @param               $rootValue
20
     * @param               $contextValue
21
     * @param               $rawVariableValues
22
     * @param null          $operationName
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $operationName is correct as it would always require null to be passed?
Loading history...
23
     * @param callable|null $fieldResolver
24
     * @throws GraphQLError
25
     * @return ExecutionContext
26
     */
27
    public function buildContext(
28
        Schema $schema,
29
        DocumentNode $documentNode,
30
        $rootValue,
31
        $contextValue,
32
        $rawVariableValues,
33
        $operationName = null,
34
        callable $fieldResolver = null
35
    ): ExecutionContext {
36
        //@TODO validate $rawVariableValues?
37
38
        $errors    = [];
39
        $fragments = [];
40
        $operation = null;
41
42
        foreach ($documentNode->getDefinitions() as $definition) {
43
            switch ($definition->getKind()) {
44
                case NodeKindEnum::OPERATION_DEFINITION:
45
                    if (!$operationName && $operation) {
46
                        throw new GraphQLError(
47
                            'Must provide operation name if query contains multiple operations.'
48
                        );
49
                    }
50
51
                    if (!$operationName || (!empty($definition->getName()) && $definition->getName()->getValue() === $operationName)) {
52
                        $operation = $definition;
53
                    }
54
                    break;
55
                case NodeKindEnum::FRAGMENT_DEFINITION:
56
                case NodeKindEnum::FRAGMENT_SPREAD:
57
                    $fragments[$definition->getName()->getValue()] = $definition;
58
                    break;
59
                default:
60
                    throw new GraphQLError(
61
                        "GraphQL cannot execute a request containing a {$definition->getKind()}."
62
                    );
63
            }
64
        }
65
66
        $executionContext = new ExecutionContext(
67
            $schema,
68
            $fragments,
69
            $rootValue,
70
            $contextValue,
71
            $rawVariableValues,
72
            $fieldResolver,
73
            $operation,
74
            $errors
75
        );
76
77
        return $executionContext;
78
    }
79
}