1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaraComponents\GraphQL; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use LaraComponents\GraphQL\Contracts\GraphQLManager as GraphQLManagerContract; |
7
|
|
|
|
8
|
|
|
class GraphQLManager implements GraphQLManagerContract |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The application instance. |
12
|
|
|
* |
13
|
|
|
* @var \Illuminate\Foundation\Application |
14
|
|
|
*/ |
15
|
|
|
protected $app; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The array of resolved schemas. |
19
|
|
|
* |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $schemas = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Create a new manager instance. |
26
|
|
|
* |
27
|
|
|
* @param \Illuminate\Foundation\Application $app |
28
|
|
|
* @return void |
|
|
|
|
29
|
|
|
*/ |
30
|
|
|
public function __construct($app) |
31
|
|
|
{ |
32
|
|
|
$this->app = $app; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Get a schema implementation by name. |
37
|
|
|
* |
38
|
|
|
* @param string $name |
39
|
|
|
* @return void |
40
|
|
|
*/ |
41
|
|
|
public function schema($name = null) |
42
|
|
|
{ |
43
|
|
|
$name = $name ?: $this->getDefaultSchema(); |
44
|
|
|
|
45
|
|
|
return $this->schemas[$name] = $this->get($name); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Attempt to get the schema from the local cache. |
50
|
|
|
* |
51
|
|
|
* @param string $name |
52
|
|
|
* @return \Youshido\GraphQL\Schema\AbstractSchema |
53
|
|
|
*/ |
54
|
|
|
protected function get($name) |
55
|
|
|
{ |
56
|
|
|
return isset($this->schemas[$name]) ? $this->schemas[$name] : $this->resolve($name); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Resolve the given store. |
61
|
|
|
* |
62
|
|
|
* @param string $name |
63
|
|
|
* @return \Youshido\GraphQL\Schema\AbstractSchema |
64
|
|
|
* |
65
|
|
|
* @throws \InvalidArgumentException |
66
|
|
|
*/ |
67
|
|
|
protected function resolve($name) |
68
|
|
|
{ |
69
|
|
|
$class = $this->getClass($name); |
70
|
|
|
|
71
|
|
|
if (is_null($class)) { |
72
|
|
|
throw new InvalidArgumentException("Schema [{$name}] is not defined."); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $this->app->make($class); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get the schema configuration. |
80
|
|
|
* |
81
|
|
|
* @param string $name |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
|
|
protected function getClass($name) |
85
|
|
|
{ |
86
|
|
|
return $this->app['config']["graphql.schemas.{$name}"]; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get the default schema name. |
91
|
|
|
* |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
|
public function getDefaultSchema() |
95
|
|
|
{ |
96
|
|
|
return $this->app['config']['graphql.schema']; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.