Skip to content

Commit aaa5b7a

Browse files
committed
Global config; descriptor moved to appropriate namespace; minor cleanup
1 parent 2965440 commit aaa5b7a

File tree

10 files changed

+105
-64
lines changed

10 files changed

+105
-64
lines changed

benchmarks/HugeSchemaBench.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class HugeSchemaBench
2222
private $schemaBuilder;
2323

2424
/**
25-
* @var Schema\Descriptor
25+
* @var \GraphQL\Type\Descriptor
2626
*/
2727
private $descriptor;
2828

@@ -47,7 +47,7 @@ public function setUp()
4747
$this->schema = $this->schemaBuilder->buildSchema();
4848

4949
$queryBuilder = new QueryGenerator($this->schema, 0.05);
50-
$this->descriptor = $this->schema->getDescriptor();
50+
$this->descriptor = $this->schema->describe();
5151
$this->smallQuery = $queryBuilder->buildQuery();
5252
}
5353

@@ -78,7 +78,7 @@ public function benchSmallQueryLazy()
7878
private function createLazySchema()
7979
{
8080
return new Schema(
81-
Schema\Config::create()
81+
\GraphQL\Config::create()
8282
->setQuery($this->schemaBuilder->buildQueryType())
8383
// ->setDescriptor($this->descriptor)
8484
->setTypeLoader(function($name) {

src/Schema/Config.php renamed to src/Config.php

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
2-
namespace GraphQL\Schema;
2+
namespace GraphQL;
33

4+
use GraphQL\Executor\Promise\PromiseAdapter;
5+
use GraphQL\Type\Descriptor;
46
use GraphQL\Type\Definition\Directive;
57
use GraphQL\Type\Definition\ObjectType;
68
use GraphQL\Type\Definition\Type;
@@ -10,9 +12,9 @@
1012
* Class Config
1113
* Note: properties are marked as public for performance reasons. They should be considered read-only.
1214
*
13-
* @package GraphQL\Schema
15+
* @package GraphQL
1416
*/
15-
final class Config
17+
class Config
1618
{
1719
/**
1820
* @var ObjectType
@@ -49,6 +51,11 @@ final class Config
4951
*/
5052
public $typeLoader;
5153

54+
/**
55+
* @var PromiseAdapter
56+
*/
57+
public $promiseAdapter;
58+
5259
/**
5360
* @param array $options
5461
* @return Config
@@ -61,55 +68,72 @@ public static function create(array $options = [])
6168
if (isset($options['query'])) {
6269
Utils::invariant(
6370
$options['query'] instanceof ObjectType,
64-
"Schema query must be Object Type if provided but got: " . Utils::getVariableType($options['query'])
71+
'Schema query must be Object Type if provided but got: %s',
72+
Utils::getVariableType($options['query'])
6573
);
6674
$config->setQuery($options['query']);
6775
}
6876

6977
if (isset($options['mutation'])) {
7078
Utils::invariant(
7179
$options['mutation'] instanceof ObjectType,
72-
"Schema mutation must be Object Type if provided but got: " . Utils::getVariableType($options['mutation'])
80+
'Schema mutation must be Object Type if provided but got: %s',
81+
Utils::getVariableType($options['mutation'])
7382
);
7483
$config->setMutation($options['mutation']);
7584
}
7685

7786
if (isset($options['subscription'])) {
7887
Utils::invariant(
7988
$options['subscription'] instanceof ObjectType,
80-
"Schema subscription must be Object Type if provided but got: " . Utils::getVariableType($options['subscription'])
89+
'Schema subscription must be Object Type if provided but got: %s',
90+
Utils::getVariableType($options['subscription'])
8191
);
8292
$config->setSubscription($options['subscription']);
8393
}
8494

8595
if (isset($options['types'])) {
8696
Utils::invariant(
8797
is_array($options['types']),
88-
"Schema types must be array if provided but got: " . Utils::getVariableType($options['types'])
98+
'Schema types must be array if provided but got: %s',
99+
Utils::getVariableType($options['types'])
89100
);
90101
$config->setTypes($options['types']);
91102
}
92103

93104
if (isset($options['directives'])) {
94105
Utils::invariant(
95106
is_array($options['directives']),
96-
"Schema directives must be array if provided but got: " . Utils::getVariableType($options['directives'])
107+
'Schema directives must be array if provided but got: %s',
108+
Utils::getVariableType($options['directives'])
97109
);
98110
$config->setDirectives($options['directives']);
99111
}
100112

101113
if (isset($options['typeLoader'])) {
102114
Utils::invariant(
103115
is_callable($options['typeLoader']),
104-
"Schema type loader must be callable if provided but got: " . Utils::getVariableType($options['typeLoader'])
116+
'Schema type loader must be callable if provided but got: %s',
117+
Utils::getVariableType($options['typeLoader'])
105118
);
106119
}
107120

108121
if (isset($options['descriptor'])) {
109122
Utils::invariant(
110123
$options['descriptor'] instanceof Descriptor,
111-
"Schema descriptor must be instance of GraphQL\\Schema\\Descriptor but got: " . Utils::getVariableType($options['descriptor'])
124+
'Schema descriptor must be instance of GraphQL\Type\Descriptor but got: %s',
125+
Utils::getVariableType($options['descriptor'])
126+
);
127+
$config->setDescriptor($options['descriptor']);
128+
}
129+
130+
if (isset($options['promiseAdapter'])) {
131+
Utils::invariant(
132+
$options['promiseAdapter'] instanceof PromiseAdapter,
133+
'Promise adapter must be an instance of GraphQL\Executor\Promise\PromiseAdapter but got: %s',
134+
Utils::getVariableType($options['promiseAdapter'])
112135
);
136+
$config->setPromiseAdapter($options['promiseAdapter']);
113137
}
114138
}
115139

@@ -259,4 +283,22 @@ public function setTypeLoader(callable $typeLoader)
259283
$this->typeLoader = $typeLoader;
260284
return $this;
261285
}
286+
287+
/**
288+
* @return PromiseAdapter
289+
*/
290+
public function getPromiseAdapter()
291+
{
292+
return $this->promiseAdapter;
293+
}
294+
295+
/**
296+
* @param PromiseAdapter $promiseAdapter
297+
* @return Config
298+
*/
299+
public function setPromiseAdapter($promiseAdapter)
300+
{
301+
$this->promiseAdapter = $promiseAdapter;
302+
return $this;
303+
}
262304
}

src/Error/UserError.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
/**
55
* Class UserError
66
*
7-
* Note:
8-
* Error that can be display safely to client...
7+
* Error that can be safely displayed to client...
98
*
109
* @package GraphQL\Error
1110
*/

src/GraphQL.php

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,6 @@
1616

1717
class GraphQL
1818
{
19-
const WARNING_ON_IMPLEMENTATION_RESOLUTION = 1;
20-
21-
private static $ignoredErrors = [];
22-
23-
public static function setIgnoreError($errorCode, $set = true)
24-
{
25-
self::$ignoredErrors[$errorCode] = $set ? true : null;
26-
}
27-
28-
public static function isIgnoredError($errorCode)
29-
{
30-
return isset(self::$ignoredErrors[$errorCode]);
31-
}
32-
3319
/**
3420
* This is the primary entry point function for fulfilling GraphQL operations
3521
* by parsing, validating, and executing a GraphQL document along side a
@@ -73,7 +59,7 @@ public static function execute(
7359
$contextValue = null,
7460
$variableValues = null,
7561
$operationName = null,
76-
$fieldResolver = null
62+
callable $fieldResolver = null
7763
)
7864
{
7965
$result = self::executeAndReturnResult(
@@ -104,6 +90,7 @@ public static function execute(
10490
* @param Schema $schema
10591
* @param string|DocumentNode $source
10692
* @param mixed $rootValue
93+
* @param mixed $contextValue
10794
* @param array|null $variableValues
10895
* @param string|null $operationName
10996
* @param callable $fieldResolver
@@ -116,7 +103,7 @@ public static function executeAndReturnResult(
116103
$contextValue = null,
117104
$variableValues = null,
118105
$operationName = null,
119-
$fieldResolver = null
106+
callable $fieldResolver = null
120107
)
121108
{
122109
try {

src/Schema.php

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<?php
22
namespace GraphQL;
33

4-
use GraphQL\Schema\Config;
5-
use GraphQL\Schema\Descriptor;
4+
use GraphQL\Type\Descriptor;
65
use GraphQL\Type\Definition\AbstractType;
76
use GraphQL\Type\Definition\Directive;
87
use GraphQL\Type\Definition\InterfaceType;
@@ -83,12 +82,13 @@ public function __construct($config = null)
8382
);
8483
list($queryType, $mutationType, $subscriptionType) = func_get_args() + [null, null, null];
8584

86-
$config = Config::create()
87-
->setQuery($queryType)
88-
->setMutation($mutationType)
89-
->setSubscription($subscriptionType)
90-
;
91-
} else if (is_array($config)) {
85+
$config = [
86+
'query' => $queryType,
87+
'mutation' => $mutationType,
88+
'subscription' => $subscriptionType
89+
];
90+
}
91+
if (is_array($config)) {
9292
$config = Config::create($config);
9393
}
9494

@@ -101,8 +101,8 @@ public function __construct($config = null)
101101
'subscription',
102102
'types',
103103
'directives',
104-
'descriptor',
105-
'typeLoader'
104+
'typeLoader',
105+
'descriptor'
106106
]),
107107
Utils::getVariableType($config)
108108
);
@@ -116,6 +116,8 @@ public function __construct($config = null)
116116
}
117117

118118
/**
119+
* Returns schema query type
120+
*
119121
* @return ObjectType
120122
*/
121123
public function getQueryType()
@@ -124,6 +126,8 @@ public function getQueryType()
124126
}
125127

126128
/**
129+
* Returns schema mutation type
130+
*
127131
* @return ObjectType|null
128132
*/
129133
public function getMutationType()
@@ -132,6 +136,8 @@ public function getMutationType()
132136
}
133137

134138
/**
139+
* Returns schema subscription
140+
*
135141
* @return ObjectType|null
136142
*/
137143
public function getSubscriptionType()
@@ -148,14 +154,16 @@ public function getConfig()
148154
}
149155

150156
/**
151-
* Returns full map of types in this schema.
157+
* Returns array of all types in this schema. Keys of this array represent type names, values are instances
158+
* of corresponding type definitions
152159
*
153160
* @return Type[]
154161
*/
155162
public function getTypeMap()
156163
{
157164
if (!$this->fullyLoaded) {
158165
if ($this->config->descriptor && $this->config->typeLoader) {
166+
// Following is still faster than $this->collectAllTypes() because it won't init fields
159167
$typesToResolve = array_diff_key($this->config->descriptor->typeMap, $this->resolvedTypes);
160168
foreach ($typesToResolve as $typeName => $_) {
161169
$this->resolvedTypes[$typeName] = $this->loadType($typeName);
@@ -185,22 +193,12 @@ public function getType($name)
185193
*
186194
* @return Descriptor
187195
*/
188-
public function getDescriptor()
196+
public function describe()
189197
{
190198
if ($this->descriptor) {
191199
return $this->descriptor;
192200
}
193-
if ($this->config->descriptor) {
194-
return $this->config->descriptor;
195-
}
196-
return $this->descriptor = $this->buildDescriptor();
197-
}
198201

199-
/**
200-
* @return Descriptor
201-
*/
202-
public function buildDescriptor()
203-
{
204202
$this->resolvedTypes = $this->collectAllTypes();
205203
$this->fullyLoaded = true;
206204

@@ -243,6 +241,9 @@ private function collectAllTypes()
243241
}
244242

245243
/**
244+
* Returns all possible concrete types for given abstract type
245+
* (implementations for interfaces and members of union type for unions)
246+
*
246247
* @param AbstractType $abstractType
247248
* @return ObjectType[]
248249
*/
@@ -253,7 +254,7 @@ public function getPossibleTypes(AbstractType $abstractType)
253254
}
254255

255256
/** @var InterfaceType $abstractType */
256-
$descriptor = $this->getDescriptor();
257+
$descriptor = $this->config->descriptor ?: $this->describe();
257258

258259
$result = [];
259260
if (isset($descriptor->possibleTypeMap[$abstractType->name])) {
@@ -264,6 +265,13 @@ public function getPossibleTypes(AbstractType $abstractType)
264265
return $result;
265266
}
266267

268+
/**
269+
* Accepts name of type or type instance and returns type instance. If type with given name is not loaded yet -
270+
* will load it first.
271+
*
272+
* @param $typeOrName
273+
* @return Type
274+
*/
267275
public function resolveType($typeOrName)
268276
{
269277
if ($typeOrName instanceof Type) {
@@ -292,6 +300,9 @@ private function loadType($typeName)
292300
}
293301

294302
/**
303+
* Returns true if object type is concrete type of given abstract type
304+
* (implementation for interfaces and members of union type for unions)
305+
*
295306
* @param AbstractType $abstractType
296307
* @param ObjectType $possibleType
297308
* @return bool
@@ -311,6 +322,8 @@ public function isPossibleType(AbstractType $abstractType, ObjectType $possibleT
311322
}
312323

313324
/**
325+
* Returns a list of directives supported by this schema
326+
*
314327
* @return Directive[]
315328
*/
316329
public function getDirectives()
@@ -319,6 +332,8 @@ public function getDirectives()
319332
}
320333

321334
/**
335+
* Returns instance of directive by name
336+
*
322337
* @param $name
323338
* @return Directive
324339
*/

src/Schema/Descriptor.php renamed to src/Type/Descriptor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
namespace GraphQL\Schema;
2+
namespace GraphQL\Type;
33

44
use GraphQL\Utils\Utils;
55

0 commit comments

Comments
 (0)