|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Digia\Lumen\GraphQL; |
|
4
|
|
|
|
|
5
|
|
|
use Digia\Lumen\GraphQL\Contracts\TypeResolverInterface; |
|
6
|
|
|
use Digia\Lumen\GraphQL\Exceptions\InvalidConfigurationException; |
|
7
|
|
|
use Digia\Lumen\GraphQL\Http\GraphiQLTokenMiddleware; |
|
8
|
|
|
use Illuminate\Contracts\Cache\Repository as CacheRepository; |
|
9
|
|
|
use Illuminate\Support\ServiceProvider; |
|
10
|
|
|
use Laravel\Lumen\Application; |
|
11
|
|
|
use Youshido\GraphQL\Execution\Processor; |
|
12
|
|
|
|
|
13
|
|
|
class GraphQLServiceProvider extends ServiceProvider |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @inheritdoc |
|
18
|
|
|
*/ |
|
19
|
|
|
public function register() |
|
20
|
|
|
{ |
|
21
|
|
|
// Configure |
|
22
|
|
|
$this->app->configure('graphql'); |
|
23
|
|
|
$this->loadViewsFrom(dirname(__DIR__) . '/resources/views', 'graphql'); |
|
24
|
|
|
$config = config('graphql'); |
|
25
|
|
|
$this->validateConfig($config); |
|
26
|
|
|
|
|
27
|
|
|
// Bind things to the container |
|
28
|
|
|
$this->app->singleton(GraphQLService::class, function (Application $app) use ($config) { |
|
29
|
|
|
$processor = $config['processor'] ?? Processor::class; |
|
30
|
|
|
|
|
31
|
|
|
return new GraphQLService(new $processor(new $config['schema']), $app->make(CacheRepository::class)); |
|
32
|
|
|
}); |
|
33
|
|
|
|
|
34
|
|
|
$this->app->singleton(TypeResolverInterface::class, function () use ($config) { |
|
35
|
|
|
return new $config['type_resolver'](); |
|
36
|
|
|
}); |
|
37
|
|
|
|
|
38
|
|
|
$this->app->bind(GraphiQLTokenMiddleware::class, function () use ($config) { |
|
39
|
|
|
return new GraphiQLTokenMiddleware($config['enable_graphiql'], $config['graphiql_token'] ?? null); |
|
40
|
|
|
}); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param array $config |
|
45
|
|
|
* |
|
46
|
|
|
* @throws InvalidConfigurationException |
|
47
|
|
|
*/ |
|
48
|
|
|
protected function validateConfig(array $config) |
|
49
|
|
|
{ |
|
50
|
|
|
if (!isset($config['schema'])) { |
|
51
|
|
|
throw new InvalidConfigurationException('Configuration value `schema` is required.'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
if (!isset($config['type_resolver'])) { |
|
55
|
|
|
throw new InvalidConfigurationException('Configuration value `type_resolver` is required.'); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|