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 |
|
|
|
|
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
|
|
|
} |