-
-
Notifications
You must be signed in to change notification settings - Fork 432
/
LighthouseServiceProvider.php
194 lines (171 loc) · 7.7 KB
/
LighthouseServiceProvider.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
namespace Nuwave\Lighthouse;
use Closure;
use Exception;
use Illuminate\Contracts\Config\Repository as ConfigRepository;
use Illuminate\Contracts\Container\Container;
use Illuminate\Foundation\Application as LaravelApplication;
use Illuminate\Routing\Router;
use Illuminate\Support\Arr;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
use Illuminate\Validation\Factory as ValidationFactory;
use Illuminate\Validation\Validator;
use Laravel\Lumen\Application as LumenApplication;
use Nuwave\Lighthouse\Console\CacheCommand;
use Nuwave\Lighthouse\Console\ClearCacheCommand;
use Nuwave\Lighthouse\Console\DirectiveCommand;
use Nuwave\Lighthouse\Console\IdeHelperCommand;
use Nuwave\Lighthouse\Console\InterfaceCommand;
use Nuwave\Lighthouse\Console\MutationCommand;
use Nuwave\Lighthouse\Console\PrintSchemaCommand;
use Nuwave\Lighthouse\Console\QueryCommand;
use Nuwave\Lighthouse\Console\ScalarCommand;
use Nuwave\Lighthouse\Console\SubscriptionCommand;
use Nuwave\Lighthouse\Console\UnionCommand;
use Nuwave\Lighthouse\Console\ValidateSchemaCommand;
use Nuwave\Lighthouse\Execution\ContextFactory;
use Nuwave\Lighthouse\Execution\ErrorPool;
use Nuwave\Lighthouse\Execution\GraphQLRequest;
use Nuwave\Lighthouse\Execution\GraphQLValidator;
use Nuwave\Lighthouse\Execution\LighthouseRequest;
use Nuwave\Lighthouse\Execution\MultipartFormRequest;
use Nuwave\Lighthouse\Execution\SingleResponse;
use Nuwave\Lighthouse\Execution\Utils\GlobalId;
use Nuwave\Lighthouse\Execution\ValidationRulesProvider;
use Nuwave\Lighthouse\Schema\AST\ASTBuilder;
use Nuwave\Lighthouse\Schema\Factories\DirectiveFactory;
use Nuwave\Lighthouse\Schema\NodeRegistry;
use Nuwave\Lighthouse\Schema\ResolverProvider;
use Nuwave\Lighthouse\Schema\Source\SchemaSourceProvider;
use Nuwave\Lighthouse\Schema\Source\SchemaStitcher;
use Nuwave\Lighthouse\Schema\TypeRegistry;
use Nuwave\Lighthouse\Schema\Values\FieldValue;
use Nuwave\Lighthouse\Support\AppVersion;
use Nuwave\Lighthouse\Support\Compatibility\LaravelMiddlewareAdapter;
use Nuwave\Lighthouse\Support\Compatibility\LumenMiddlewareAdapter;
use Nuwave\Lighthouse\Support\Compatibility\MiddlewareAdapter;
use Nuwave\Lighthouse\Support\Contracts\CanStreamResponse;
use Nuwave\Lighthouse\Support\Contracts\CreatesContext;
use Nuwave\Lighthouse\Support\Contracts\CreatesResponse;
use Nuwave\Lighthouse\Support\Contracts\GlobalId as GlobalIdContract;
use Nuwave\Lighthouse\Support\Contracts\ProvidesResolver;
use Nuwave\Lighthouse\Support\Contracts\ProvidesSubscriptionResolver;
use Nuwave\Lighthouse\Support\Contracts\ProvidesValidationRules;
use Nuwave\Lighthouse\Support\Http\Responses\ResponseStream;
use Nuwave\Lighthouse\Testing\TestingServiceProvider;
class LighthouseServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*/
public function boot(ValidationFactory $validationFactory, ConfigRepository $configRepository): void
{
$this->publishes([
__DIR__.'/lighthouse.php' => $this->app->configPath().'/lighthouse.php',
], 'lighthouse-config');
$this->publishes([
__DIR__.'/../assets/default-schema.graphql' => $configRepository->get('lighthouse.schema.register'),
], 'lighthouse-schema');
$this->loadRoutesFrom(__DIR__.'/Support/Http/routes.php');
$validationFactory->resolver(
function ($translator, array $data, array $rules, array $messages, array $customAttributes): Validator {
// This determines whether we are resolving a GraphQL field
return Arr::has($customAttributes, ['root', 'context', 'resolveInfo'])
? new GraphQLValidator($translator, $data, $rules, $messages, $customAttributes)
: new Validator($translator, $data, $rules, $messages, $customAttributes);
}
);
}
/**
* Load routes from provided path.
*
* @param string $path
* @param array $attributes
*/
protected function loadRoutesFrom($path, array $attributes = []): void
{
if (AppVersion::isLumen()) {
require realpath($path);
return;
}
parent::loadRoutesFrom($path, $attributes);
}
/**
* Register any application services.
*/
public function register(): void
{
$this->mergeConfigFrom(__DIR__.'/lighthouse.php', 'lighthouse');
$this->app->singleton(GraphQL::class);
$this->app->singleton(ASTBuilder::class);
$this->app->singleton(DirectiveFactory::class);
$this->app->singleton(NodeRegistry::class);
$this->app->singleton(TypeRegistry::class);
$this->app->singleton(ErrorPool::class);
$this->app->singleton(CreatesContext::class, ContextFactory::class);
$this->app->singleton(CanStreamResponse::class, ResponseStream::class);
$this->app->bind(CreatesResponse::class, SingleResponse::class);
$this->app->bind(GlobalIdContract::class, GlobalId::class);
$this->app->singleton(GraphQLRequest::class, function (Container $app): GraphQLRequest {
/** @var \Illuminate\Http\Request $request */
$request = $app->make('request');
/** @var string $contentType */
$contentType = $request->header('Content-Type') ?? '';
$isMultipartFormRequest = Str::startsWith($contentType, 'multipart/form-data');
return $isMultipartFormRequest
? new MultipartFormRequest($request)
: new LighthouseRequest($request);
});
$this->app->singleton(SchemaSourceProvider::class, function (): SchemaStitcher {
return new SchemaStitcher(
config('lighthouse.schema.register', '')
);
});
$this->app->bind(ProvidesResolver::class, ResolverProvider::class);
$this->app->bind(ProvidesSubscriptionResolver::class, function (): ProvidesSubscriptionResolver {
return new class implements ProvidesSubscriptionResolver {
public function provideSubscriptionResolver(FieldValue $fieldValue): Closure
{
throw new Exception(
'Add the SubscriptionServiceProvider to your config/app.php to enable subscriptions.'
);
}
};
});
$this->app->bind(ProvidesValidationRules::class, ValidationRulesProvider::class);
$this->app->singleton(MiddlewareAdapter::class, function (Container $app): MiddlewareAdapter {
// prefer using fully-qualified class names here when referring to Laravel-only or Lumen-only classes
if ($app instanceof LaravelApplication) {
return new LaravelMiddlewareAdapter(
$app->get(Router::class)
);
}
if ($app instanceof LumenApplication) {
return new LumenMiddlewareAdapter($app);
}
throw new Exception(
'Could not correctly determine Laravel framework flavor, got '.get_class($app).'.'
);
});
if ($this->app->runningInConsole()) {
$this->commands([
CacheCommand::class,
ClearCacheCommand::class,
DirectiveCommand::class,
IdeHelperCommand::class,
InterfaceCommand::class,
MutationCommand::class,
PrintSchemaCommand::class,
QueryCommand::class,
ScalarCommand::class,
SubscriptionCommand::class,
UnionCommand::class,
ValidateSchemaCommand::class,
]);
}
if ($this->app->runningUnitTests()) {
$this->app->register(TestingServiceProvider::class);
}
}
}