1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2015–2018 Alexandr Viniychuk <http://youshido.com>. |
4
|
|
|
* Copyright (c) 2015–2018 Portey Vasil <https://github.com/portey>. |
5
|
|
|
* Copyright (c) 2018 Ryan Parman <https://github.com/skyzyx>. |
6
|
|
|
* Copyright (c) 2018 Ashley Hutson <https://github.com/asheliahut>. |
7
|
|
|
* Copyright (c) 2015–2018 Contributors. |
8
|
|
|
* |
9
|
|
|
* http://opensource.org/licenses/MIT |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
/* |
14
|
|
|
* This file is a part of GraphQL project. |
15
|
|
|
* |
16
|
|
|
* @author Alexandr Viniychuk <[email protected]> |
17
|
|
|
* created: 5/19/16 9:00 AM |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Youshido\GraphQL\Execution\Context; |
21
|
|
|
|
22
|
|
|
use Youshido\GraphQL\Execution\Container\ContainerInterface; |
23
|
|
|
use Youshido\GraphQL\Execution\Request; |
24
|
|
|
use Youshido\GraphQL\Field\Field; |
25
|
|
|
use Youshido\GraphQL\Introspection\Field\SchemaField; |
26
|
|
|
use Youshido\GraphQL\Introspection\Field\TypeDefinitionField; |
27
|
|
|
use Youshido\GraphQL\Schema\AbstractSchema; |
28
|
|
|
use Youshido\GraphQL\Type\Object\AbstractObjectType; |
29
|
|
|
use Youshido\GraphQL\Validator\ErrorContainer\ErrorContainerTrait; |
30
|
|
|
use Youshido\GraphQL\Validator\SchemaValidator\SchemaValidator; |
31
|
|
|
|
32
|
|
|
class ExecutionContext implements ExecutionContextInterface |
33
|
|
|
{ |
34
|
|
|
use ErrorContainerTrait; |
35
|
|
|
|
36
|
|
|
/** @var AbstractSchema */ |
37
|
|
|
private $schema; |
38
|
|
|
|
39
|
73 |
|
/** @var Request */ |
40
|
|
|
private $request; |
41
|
73 |
|
|
42
|
73 |
|
/** @var ContainerInterface */ |
43
|
|
|
private $container; |
44
|
73 |
|
|
45
|
73 |
|
/** @var array */ |
46
|
|
|
private $typeFieldLookupTable; |
47
|
73 |
|
|
48
|
|
|
/** |
49
|
|
|
* ExecutionContext constructor. |
50
|
73 |
|
* |
51
|
1 |
|
* @param AbstractSchema $schema |
52
|
1 |
|
*/ |
53
|
|
|
public function __construct(AbstractSchema $schema) |
54
|
73 |
|
{ |
55
|
|
|
$this->typeFieldLookupTable = []; |
56
|
73 |
|
$this->schema = $schema; |
57
|
|
|
$this->validateSchema(); |
58
|
73 |
|
|
59
|
73 |
|
$this->introduceIntrospectionFields(); |
60
|
73 |
|
} |
61
|
73 |
|
|
62
|
|
|
/** |
63
|
|
|
* @return AbstractSchema |
64
|
|
|
*/ |
65
|
|
|
public function getSchema() |
66
|
67 |
|
{ |
67
|
|
|
return $this->schema; |
68
|
67 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param AbstractSchema $schema |
72
|
|
|
* |
73
|
|
|
* @return $this |
74
|
|
|
*/ |
75
|
|
|
public function setSchema(AbstractSchema $schema) |
76
|
|
|
{ |
77
|
|
|
$this->schema = $schema; |
78
|
|
|
|
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param AbstractObjectType $type |
84
|
|
|
* @param string $fieldName |
85
|
|
|
* |
86
|
67 |
|
* @return Field |
87
|
|
|
*/ |
88
|
67 |
|
public function getField(AbstractObjectType $type, $fieldName) |
89
|
|
|
{ |
90
|
|
|
$typeName = $type->getName(); |
91
|
|
|
|
92
|
|
|
if (!\array_key_exists($typeName, $this->typeFieldLookupTable)) { |
93
|
|
|
$this->typeFieldLookupTable[$typeName] = []; |
94
|
|
|
} |
95
|
|
|
|
96
|
67 |
|
if (!\array_key_exists($fieldName, $this->typeFieldLookupTable[$typeName])) { |
97
|
|
|
$this->typeFieldLookupTable[$typeName][$fieldName] = $type->getField($fieldName); |
98
|
67 |
|
} |
99
|
|
|
|
100
|
67 |
|
return $this->typeFieldLookupTable[$typeName][$fieldName]; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return Request |
105
|
|
|
*/ |
106
|
|
|
public function getRequest() |
107
|
|
|
{ |
108
|
|
|
return $this->request; |
109
|
|
|
} |
110
|
|
|
|
111
|
1 |
|
/** |
112
|
|
|
* @param Request $request |
113
|
1 |
|
* |
114
|
|
|
* @return $this |
115
|
|
|
*/ |
116
|
|
|
public function setRequest(Request $request) |
117
|
|
|
{ |
118
|
|
|
$this->request = $request; |
119
|
|
|
|
120
|
|
|
return $this; |
121
|
70 |
|
} |
122
|
|
|
|
123
|
70 |
|
public function get($id) |
124
|
|
|
{ |
125
|
70 |
|
return $this->container->get($id); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return ContainerInterface |
130
|
|
|
*/ |
131
|
|
|
public function getContainer() |
132
|
|
|
{ |
133
|
|
|
return $this->container; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param ContainerInterface $container |
138
|
|
|
* |
139
|
|
|
* @return $this |
140
|
|
|
*/ |
141
|
|
|
public function setContainer(ContainerInterface $container) |
142
|
|
|
{ |
143
|
|
|
$this->container = $container; |
144
|
|
|
|
145
|
|
|
return $this; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
protected function validateSchema(): void |
149
|
|
|
{ |
150
|
|
|
try { |
151
|
|
|
(new SchemaValidator())->validate($this->schema); |
152
|
|
|
} catch (\Exception $e) { |
153
|
|
|
$this->addError($e); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
protected function introduceIntrospectionFields(): void |
158
|
|
|
{ |
159
|
|
|
$schemaField = new SchemaField(); |
160
|
|
|
$this->schema->addQueryField($schemaField); |
161
|
|
|
$this->schema->addQueryField(new TypeDefinitionField()); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|