forked from rebing/graphql-laravel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphQLServiceProvider.php
148 lines (125 loc) · 4.31 KB
/
GraphQLServiceProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
declare(strict_types = 1);
namespace Rebing\GraphQL;
use GraphQL\Validator\DocumentValidator;
use GraphQL\Validator\Rules\DisableIntrospection;
use GraphQL\Validator\Rules\QueryComplexity;
use GraphQL\Validator\Rules\QueryDepth;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Contracts\Container\Container;
use Illuminate\Support\ServiceProvider;
use Rebing\GraphQL\Console\EnumMakeCommand;
use Rebing\GraphQL\Console\FieldMakeCommand;
use Rebing\GraphQL\Console\InputMakeCommand;
use Rebing\GraphQL\Console\InterfaceMakeCommand;
use Rebing\GraphQL\Console\MiddlewareMakeCommand;
use Rebing\GraphQL\Console\MutationMakeCommand;
use Rebing\GraphQL\Console\QueryMakeCommand;
use Rebing\GraphQL\Console\ScalarMakeCommand;
use Rebing\GraphQL\Console\SchemaConfigMakeCommand;
use Rebing\GraphQL\Console\TypeMakeCommand;
use Rebing\GraphQL\Console\UnionMakeCommand;
class GraphQLServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*/
public function boot(): void
{
$this->bootPublishes();
$this->bootRouter();
}
/**
* Bootstrap router.
*/
protected function bootRouter(): void
{
$this->loadRoutesFrom(__DIR__ . '/routes.php');
}
/**
* Bootstrap publishes.
*/
protected function bootPublishes(): void
{
$configPath = __DIR__ . '/../config';
$this->mergeConfigFrom($configPath . '/config.php', 'graphql');
$this->publishes([
$configPath . '/config.php' => $this->app->configPath() . '/graphql.php',
], 'config');
}
/**
* Add types from config.
*/
protected function bootTypes(GraphQL $graphQL): void
{
$configTypes = $graphQL->getConfigRepository()->get('graphql.types', []);
$graphQL->addTypes($configTypes);
}
/**
* Configure security from config.
*/
protected function applySecurityRules(Repository $config): void
{
$maxQueryComplexity = $config->get('graphql.security.query_max_complexity');
if (null !== $maxQueryComplexity) {
/** @var QueryComplexity $queryComplexity */
$queryComplexity = DocumentValidator::getRule(QueryComplexity::class);
$queryComplexity->setMaxQueryComplexity($maxQueryComplexity);
}
$maxQueryDepth = $config->get('graphql.security.query_max_depth');
if (null !== $maxQueryDepth) {
/** @var QueryDepth $queryDepth */
$queryDepth = DocumentValidator::getRule(QueryDepth::class);
$queryDepth->setMaxQueryDepth($maxQueryDepth);
}
$disableIntrospection = $config->get('graphql.security.disable_introspection');
if (true === $disableIntrospection) {
/** @var DisableIntrospection $disableIntrospection */
$disableIntrospection = DocumentValidator::getRule(DisableIntrospection::class);
$disableIntrospection->setEnabled(DisableIntrospection::ENABLED);
}
}
/**
* Register any application services.
*/
public function register(): void
{
$this->registerGraphQL();
if ($this->app->runningInConsole()) {
$this->registerConsole();
}
}
public function registerGraphQL(): void
{
$this->app->singleton(GraphQL::class, function (Container $app): GraphQL {
$config = $app->make(Repository::class);
$graphql = new GraphQL($app, $config);
$this->applySecurityRules($config);
return $graphql;
});
$this->app->alias(GraphQL::class, 'graphql');
$this->app->afterResolving(GraphQL::class, function (GraphQL $graphQL): void {
$this->bootTypes($graphQL);
});
}
/**
* Register console commands.
*/
public function registerConsole(): void
{
$this->commands([
EnumMakeCommand::class,
FieldMakeCommand::class,
InputMakeCommand::class,
InterfaceMakeCommand::class,
InterfaceMakeCommand::class,
MiddlewareMakeCommand::class,
MutationMakeCommand::class,
QueryMakeCommand::class,
ScalarMakeCommand::class,
SchemaConfigMakeCommand::class,
TypeMakeCommand::class,
UnionMakeCommand::class,
]);
}
}