|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\graphql\GraphQL\Execution; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\Core\Cache\RefinableCacheableDependencyInterface; |
|
6
|
|
|
use Drupal\Core\Cache\RefinableCacheableDependencyTrait; |
|
7
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
|
8
|
|
|
|
|
9
|
|
|
class ResolveContext implements RefinableCacheableDependencyInterface { |
|
10
|
|
|
use RefinableCacheableDependencyTrait; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Read-only list of global values. |
|
14
|
|
|
* |
|
15
|
|
|
* @var array |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $globals; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The context stack. |
|
21
|
|
|
* |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $contexts = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* ResolveContext constructor. |
|
28
|
|
|
* |
|
29
|
|
|
* @param array $globals |
|
30
|
|
|
* List of global values to expose to field resolvers. |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct(array $globals = []) { |
|
33
|
|
|
$this->globals = $globals; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Get a contextual value for the current field. |
|
38
|
|
|
* |
|
39
|
|
|
* Allows field resolvers to inherit contextual values from their ancestors. |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $name |
|
42
|
|
|
* The name of the context. |
|
43
|
|
|
* @param \GraphQL\Type\Definition\ResolveInfo $info |
|
44
|
|
|
* The resolve info object. |
|
45
|
|
|
* @param mixed $default |
|
46
|
|
|
* An arbitrary default value in case the context is not set. |
|
47
|
|
|
* |
|
48
|
|
|
* @return mixed |
|
49
|
|
|
* The current value of the given context or the given default value if the |
|
50
|
|
|
* context wasn't set. |
|
51
|
|
|
*/ |
|
52
|
|
|
public function getContext($name, ResolveInfo $info, $default = NULL) { |
|
53
|
|
|
$operation = isset($info->operation->name->value) ? $info->operation->name->value : $info->operation->operation; |
|
54
|
|
|
$path = $info->path; |
|
55
|
|
|
|
|
56
|
|
|
do { |
|
57
|
|
|
$key = implode('.', $path); |
|
58
|
|
|
if (isset($this->contexts[$operation][$key][$name])) { |
|
59
|
|
|
return $this->contexts[$operation][$key][$name]; |
|
60
|
|
|
} |
|
61
|
|
|
} while (array_pop($path) && !empty($path)); |
|
62
|
|
|
|
|
63
|
|
|
return $default; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Sets a contextual value for the current field and its decendents. |
|
68
|
|
|
* |
|
69
|
|
|
* Allows field resolvers to set contextual values which can be inherited by |
|
70
|
|
|
* their descendents. |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $name |
|
73
|
|
|
* The name of the context. |
|
74
|
|
|
* @param $value |
|
75
|
|
|
* The value of the context. |
|
76
|
|
|
* @param \GraphQL\Type\Definition\ResolveInfo $info |
|
77
|
|
|
* The resolve info object. |
|
78
|
|
|
* |
|
79
|
|
|
* @return $this |
|
80
|
|
|
*/ |
|
81
|
|
|
public function setContext($name, $value, ResolveInfo $info) { |
|
82
|
|
|
$operation = isset($info->operation->name->value) ? $info->operation->name->value : $info->operation->operation; |
|
83
|
|
|
$key = implode('.', $info->path); |
|
84
|
|
|
$this->contexts[$operation][$key][$name] = $value; |
|
85
|
|
|
|
|
86
|
|
|
return $this; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Retrieve a global/static parameter value. |
|
91
|
|
|
* |
|
92
|
|
|
* @param string $name |
|
93
|
|
|
* The name of the global parameter. |
|
94
|
|
|
* @param mixed $default |
|
95
|
|
|
* An arbitrary default value in case the context is not set. |
|
96
|
|
|
* |
|
97
|
|
|
* @return mixed|null |
|
98
|
|
|
* The requested global parameter value or the given default value if the |
|
99
|
|
|
* parameter is not set. |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getGlobal($name, $default = NULL) { |
|
102
|
|
|
if (isset($this->globals[$name])) { |
|
103
|
|
|
return $this->globals[$name]; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return $default; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
} |