1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Overblog\GraphQLBundle\Definition\Type; |
4
|
|
|
|
5
|
|
|
use GraphQL\Type\Schema; |
6
|
|
|
use GraphQL\Type\SchemaConfig; |
7
|
|
|
use Overblog\GraphQLBundle\Definition\Type\SchemaExtension\SchemaExtensionInterface; |
8
|
|
|
use Overblog\GraphQLBundle\Resolver\UnresolvableException; |
9
|
|
|
|
10
|
|
|
class ExtensibleSchema extends Schema |
11
|
|
|
{ |
12
|
92 |
|
public function __construct($config) |
13
|
|
|
{ |
14
|
92 |
|
parent::__construct($this->addDefaultFallBackToTypeLoader( |
15
|
92 |
|
$config instanceof SchemaConfig ? $config : SchemaConfig::create($config) |
16
|
|
|
)); |
17
|
92 |
|
} |
18
|
|
|
|
19
|
|
|
/** @var SchemaExtensionInterface[] */ |
20
|
|
|
private $extensions = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param SchemaExtensionInterface[] $extensions |
24
|
|
|
* |
25
|
|
|
* @return $this |
26
|
|
|
*/ |
27
|
88 |
|
public function setExtensions(array $extensions) |
28
|
|
|
{ |
29
|
88 |
|
$this->extensions = []; |
30
|
88 |
|
foreach ($extensions as $extension) { |
31
|
88 |
|
$this->addExtension($extension); |
32
|
|
|
} |
33
|
|
|
|
34
|
88 |
|
return $this; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param SchemaExtensionInterface $extension |
39
|
|
|
*/ |
40
|
88 |
|
public function addExtension(SchemaExtensionInterface $extension) |
41
|
|
|
{ |
42
|
88 |
|
$this->extensions[] = $extension; |
43
|
88 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return $this |
47
|
|
|
*/ |
48
|
83 |
|
public function processExtensions() |
49
|
|
|
{ |
50
|
83 |
|
foreach ($this->extensions as $extension) { |
51
|
81 |
|
$extension->process($this); |
52
|
|
|
} |
53
|
|
|
|
54
|
83 |
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
92 |
|
private function addDefaultFallBackToTypeLoader(SchemaConfig $schemaConfig) |
58
|
|
|
{ |
59
|
92 |
|
$typeLoader = $schemaConfig->typeLoader; |
60
|
92 |
|
$loaderWrapper = null; |
61
|
|
|
$loaderWrapper = function ($name) use ($typeLoader, &$schemaConfig, &$loaderWrapper) { |
62
|
82 |
|
$type = null; |
63
|
|
|
try { |
64
|
82 |
|
$type = $typeLoader($name); |
65
|
3 |
|
} catch (UnresolvableException $e) { |
66
|
|
|
// second chance for types with un-registered name in TypeResolver |
67
|
|
|
// we disabled the custom typeLoader to force default loader usage |
68
|
3 |
|
$schemaConfig->typeLoader = null; |
69
|
3 |
|
$type = $this->getType($name); |
70
|
3 |
|
$schemaConfig->typeLoader = $loaderWrapper; |
71
|
|
|
} |
72
|
|
|
|
73
|
82 |
|
return $type; |
74
|
92 |
|
}; |
75
|
|
|
|
76
|
92 |
|
$schemaConfig->typeLoader = $loaderWrapper; |
77
|
|
|
|
78
|
92 |
|
return $schemaConfig; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|