-
-
Notifications
You must be signed in to change notification settings - Fork 469
Expand file tree
/
Copy pathLighthouseServiceProvider.php
More file actions
197 lines (172 loc) · 8 KB
/
Copy pathLighthouseServiceProvider.php
File metadata and controls
197 lines (172 loc) · 8 KB
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
195
196
197
<?php declare(strict_types=1);
namespace Nuwave\Lighthouse;
use GraphQL\Error\ClientAware;
use GraphQL\Error\Error;
use GraphQL\Error\ProvidesExtensions;
use GraphQL\Executor\ExecutionResult;
use GraphQL\Executor\Executor;
use GraphQL\Utils\Utils;
use Illuminate\Contracts\Config\Repository as ConfigRepository;
use Illuminate\Contracts\Debug\ExceptionHandler as ExceptionHandlerContract;
use Illuminate\Contracts\Events\Dispatcher as EventsDispatcher;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\ServiceProvider;
use Nuwave\Lighthouse\Console\CacheCommand;
use Nuwave\Lighthouse\Console\ClearCacheCommand;
use Nuwave\Lighthouse\Console\ClearQueryCacheCommand;
use Nuwave\Lighthouse\Console\ClearSchemaCacheCommand;
use Nuwave\Lighthouse\Console\DirectiveCommand;
use Nuwave\Lighthouse\Console\FieldCommand;
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\Console\ValidatorCommand;
use Nuwave\Lighthouse\Events\RegisterDirectiveNamespaces;
use Nuwave\Lighthouse\Execution\CacheableValidationRulesProvider;
use Nuwave\Lighthouse\Execution\ContextFactory;
use Nuwave\Lighthouse\Execution\ContextSerializer;
use Nuwave\Lighthouse\Execution\ErrorPool;
use Nuwave\Lighthouse\Execution\ResolveInfo;
use Nuwave\Lighthouse\Execution\SingleResponse;
use Nuwave\Lighthouse\Http\Responses\ResponseStream;
use Nuwave\Lighthouse\Schema\AST\ASTBuilder;
use Nuwave\Lighthouse\Schema\DirectiveLocator;
use Nuwave\Lighthouse\Schema\ResolverProvider;
use Nuwave\Lighthouse\Schema\SchemaBuilder;
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\Contracts\CanStreamResponse;
use Nuwave\Lighthouse\Support\Contracts\CreatesContext;
use Nuwave\Lighthouse\Support\Contracts\CreatesResponse;
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;
use Nuwave\Lighthouse\Support\Contracts\ProvidesResolver;
use Nuwave\Lighthouse\Support\Contracts\ProvidesSubscriptionResolver;
use Nuwave\Lighthouse\Support\Contracts\ProvidesValidationRules;
use Nuwave\Lighthouse\Support\Contracts\SerializesContext;
class LighthouseServiceProvider extends ServiceProvider
{
/** @var array<int, class-string<\Illuminate\Console\Command>> */
protected const COMMANDS = [
CacheCommand::class,
ClearCacheCommand::class,
ClearQueryCacheCommand::class,
ClearSchemaCacheCommand::class,
DirectiveCommand::class,
FieldCommand::class,
IdeHelperCommand::class,
InterfaceCommand::class,
MutationCommand::class,
PrintSchemaCommand::class,
QueryCommand::class,
ScalarCommand::class,
SubscriptionCommand::class,
UnionCommand::class,
ValidateSchemaCommand::class,
ValidatorCommand::class,
];
public function register(): void
{
$this->mergeConfigFrom(__DIR__ . '/lighthouse.php', 'lighthouse');
$this->app->singleton(GraphQL::class);
$this->app->singleton(ASTBuilder::class);
$this->app->singleton(SchemaBuilder::class);
$this->app->singleton(DirectiveLocator::class);
$this->app->singleton(TypeRegistry::class);
$this->app->singleton(ErrorPool::class);
$this->app->bind(CanStreamResponse::class, ResponseStream::class);
$this->app->bind(CreatesContext::class, ContextFactory::class);
$this->app->bind(SerializesContext::class, ContextSerializer::class);
$this->app->bind(CreatesResponse::class, SingleResponse::class);
$this->app->singleton(SchemaSourceProvider::class, static fn (): SchemaStitcher => new SchemaStitcher(
config('lighthouse.schema_path', ''),
));
$this->app->bind(ProvidesResolver::class, ResolverProvider::class);
$this->app->bind(ProvidesSubscriptionResolver::class, static fn (): ProvidesSubscriptionResolver => new class() implements ProvidesSubscriptionResolver {
public function provideSubscriptionResolver(FieldValue $fieldValue): \Closure
{
throw new \Exception('Register the SubscriptionServiceProvider to enable subscriptions.');
}
});
$this->app->bind(ProvidesValidationRules::class, CacheableValidationRulesProvider::class);
}
public function boot(ConfigRepository $configRepository, EventsDispatcher $dispatcher): void
{
$dispatcher->listen(RegisterDirectiveNamespaces::class, static fn (): string => __NAMESPACE__ . '\\Schema\\Directives');
$this->commands(self::COMMANDS);
$this->publishes([
__DIR__ . '/lighthouse.php' => $this->app->configPath() . '/lighthouse.php',
], 'lighthouse-config');
$this->publishes([
__DIR__ . '/default-schema.graphql' => $configRepository->get('lighthouse.schema_path'),
], 'lighthouse-schema');
$this->loadRoutesFrom(__DIR__ . '/Http/routes.php');
$exceptionHandler = $this->app->make(ExceptionHandlerContract::class);
// @phpstan-ignore-next-line larastan overly eager assumes this will always be a concrete instance
if ($exceptionHandler instanceof ExceptionHandler) {
$exceptionHandler->renderable(function (ClientAware $error): JsonResponse {
assert($error instanceof \Throwable);
if (! $error instanceof Error) {
$error = new Error(
$error->getMessage(),
null,
null,
[],
null,
$error,
$error instanceof ProvidesExtensions ? $error->getExtensions() : [],
);
}
$graphQL = $this->app->make(GraphQL::class);
$executionResult = new ExecutionResult(null, [$error]);
$serializableResult = $graphQL->toSerializableArray($executionResult);
return new JsonResponse($serializableResult);
});
}
Executor::setDefaultFieldResolver([static::class, 'defaultFieldResolver']); // @phpstan-ignore argument.type (callable not recognized)
}
/**
* The default field resolver for GraphQL queries.
*
* This method is used to resolve fields on the object-like value returned by a resolver.
* It checks if the value is an Eloquent model and retrieves the attribute or property accordingly.
* Otherwise, it falls back to the default behavior from webonyx/graphql-php's default field resolver.
*
* @see \GraphQL\Executor\Executor::defaultFieldResolver()
*
* @param array<string, mixed> $args
*/
public static function defaultFieldResolver(mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): mixed
{
$fieldName = $resolveInfo->fieldName;
if ($root instanceof Model) {
$property = $root->getAttribute($fieldName);
if ($property === null && property_exists($root, $fieldName)) {
$property = $root->{$fieldName};
}
} else {
$property = Utils::extractKey($root, $fieldName);
}
return $property instanceof \Closure
? $property($root, $args, $context, $resolveInfo)
: $property;
}
protected function loadRoutesFrom($path): void
{
if (AppVersion::isLumen()) {
require \Safe\realpath($path);
return;
}
parent::loadRoutesFrom($path);
}
}