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
|
|
|
array_pop($path); |
62
|
|
|
} while (count($path)); |
63
|
|
|
|
64
|
|
|
return $default; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Sets a contextual value for the current field and its decendents. |
69
|
|
|
* |
70
|
|
|
* Allows field resolvers to set contextual values which can be inherited by |
71
|
|
|
* their descendents. |
72
|
|
|
* |
73
|
|
|
* @param string $name |
74
|
|
|
* The name of the context. |
75
|
|
|
* @param $value |
76
|
|
|
* The value of the context. |
77
|
|
|
* @param \GraphQL\Type\Definition\ResolveInfo $info |
78
|
|
|
* The resolve info object. |
79
|
|
|
* |
80
|
|
|
* @return $this |
81
|
|
|
*/ |
82
|
|
|
public function setContext($name, $value, ResolveInfo $info) { |
83
|
|
|
$operation = isset($info->operation->name->value) ? $info->operation->name->value : $info->operation->operation; |
84
|
|
|
$key = implode('.', $info->path); |
85
|
|
|
$this->contexts[$operation][$key][$name] = $value; |
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Retrieve a global/static parameter value. |
92
|
|
|
* |
93
|
|
|
* @param string $name |
94
|
|
|
* The name of the global parameter. |
95
|
|
|
* @param mixed $default |
96
|
|
|
* An arbitrary default value in case the context is not set. |
97
|
|
|
* |
98
|
|
|
* @return mixed|null |
99
|
|
|
* The requested global parameter value or the given default value if the |
100
|
|
|
* parameter is not set. |
101
|
|
|
*/ |
102
|
|
|
public function getGlobal($name, $default = NULL) { |
103
|
|
|
if (isset($this->globals[$name])) { |
104
|
|
|
return $this->globals[$name]; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $default; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
} |
111
|
|
|
|