From 405fcd2a05e5847f21259c991e18e560cc6e9694 Mon Sep 17 00:00:00 2001 From: Alan Colant <19172637+alancolant@users.noreply.github.com> Date: Fri, 7 Jul 2023 16:07:58 +0200 Subject: [PATCH 1/9] Update GraphQLController.php Correction find schema when prefix contains parameters --- src/GraphQLController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/GraphQLController.php b/src/GraphQLController.php index 62cfc41b..434e125e 100644 --- a/src/GraphQLController.php +++ b/src/GraphQLController.php @@ -76,7 +76,7 @@ protected function createBatchingNotSupportedResponse(array $input): array protected function findSchemaNameInRequest(Request $request, string $routePrefix): ?string { - $path = $request->getPathInfo(); + $path = "/" . $request->route()->uri(); if (!Str::startsWith($path, $routePrefix)) { return null; From b55992ed39f30dc9a3b66ee97f7200d60705e322 Mon Sep 17 00:00:00 2001 From: Colant Alan <19172637+alancolant@users.noreply.github.com> Date: Sat, 8 Jul 2023 14:07:57 +0200 Subject: [PATCH 2/9] add test for route parameters schema --- tests/Unit/EndpointParamsTest.php | 47 +++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 tests/Unit/EndpointParamsTest.php diff --git a/tests/Unit/EndpointParamsTest.php b/tests/Unit/EndpointParamsTest.php new file mode 100644 index 00000000..bfacecde --- /dev/null +++ b/tests/Unit/EndpointParamsTest.php @@ -0,0 +1,47 @@ +call('GET', '/graphql/arbitrary_param', [ + 'query' => $this->queries['examples'], + ]); + + self::assertEquals(200, $response->getStatusCode()); + + $content = $response->getData(true); + self::assertArrayHasKey('data', $content); + self::assertEquals($content['data'], [ + 'examples' => $this->data, + ]); + } + public function testGetCustomSchemaWithRouteParameter(): void + { + $response = $this->call('GET', '/graphql/arbitrary_param/custom', [ + 'query' => $this->queries['examplesCustom'], + ]); + self::assertEquals(200, $response->getStatusCode()); + $content = $response->getData(true); + self::assertArrayHasKey('data', $content); + self::assertEquals($content['data'], [ + 'examplesCustom' => $this->data, + ]); + } + + protected function getEnvironmentSetUp($app): void + { + parent::getEnvironmentSetUp($app); + $app['config']->set('graphql.route.prefix', 'graphql/{parameter}'); + } + +} From 8f8668dc5ad445d9d3b0e8ec64f769f33e1077a5 Mon Sep 17 00:00:00 2001 From: Colant Alan <19172637+alancolant@users.noreply.github.com> Date: Sun, 9 Jul 2023 13:46:07 +0200 Subject: [PATCH 3/9] correction empty prefix schema detection --- src/GraphQLController.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/GraphQLController.php b/src/GraphQLController.php index 434e125e..328c64fa 100644 --- a/src/GraphQLController.php +++ b/src/GraphQLController.php @@ -1,6 +1,7 @@ route()->parameter('schemaName', $config->get('graphql.default_schema', 'default')); $routePrefix = $config->get('graphql.route.prefix', 'graphql'); $schemaName = $this->findSchemaNameInRequest($request, "/$routePrefix") ?: $config->get('graphql.default_schema', 'default'); @@ -57,6 +59,7 @@ function (BaseOperationParams $baseOperationParams) use ($schemaName, $graphql): */ protected function createBatchingNotSupportedResponse(array $input): array { + $count = min(\count($input), 100); $data = []; @@ -76,12 +79,10 @@ protected function createBatchingNotSupportedResponse(array $input): array protected function findSchemaNameInRequest(Request $request, string $routePrefix): ?string { - $path = "/" . $request->route()->uri(); - - if (!Str::startsWith($path, $routePrefix)) { + $path = str($request->route()->uri())->ltrim('/')->start('/'); + if (!$path->startsWith($routePrefix)) { return null; } - - return trim(Str::after($path, $routePrefix), '/'); + return $path->after($routePrefix)->trim('/')->toString(); } } From d475c602670da28a0c0c73e32880233e7b3191e6 Mon Sep 17 00:00:00 2001 From: Colant Alan <19172637+alancolant@users.noreply.github.com> Date: Sun, 9 Jul 2023 16:02:00 +0200 Subject: [PATCH 4/9] switch schema detection and route config using HttpMiddleware with server context --- src/GraphQLController.php | 19 +--- src/GraphQLHttpMiddleware.php | 22 +++++ src/routes.php | 150 ++++++++++++++++++++---------- tests/Unit/EndpointParamsTest.php | 7 +- tests/Unit/RoutesTest.php | 7 ++ 5 files changed, 133 insertions(+), 72 deletions(-) create mode 100644 src/GraphQLHttpMiddleware.php diff --git a/src/GraphQLController.php b/src/GraphQLController.php index 328c64fa..57ca94a6 100644 --- a/src/GraphQLController.php +++ b/src/GraphQLController.php @@ -1,7 +1,6 @@ route()->parameter('schemaName', $config->get('graphql.default_schema', 'default')); - $routePrefix = $config->get('graphql.route.prefix', 'graphql'); - $schemaName = $this->findSchemaNameInRequest($request, "/$routePrefix") ?: $config->get('graphql.default_schema', 'default'); - + $schemaName = $request->server('graphql.schemaName'); $operations = $parser->parseRequest($request); $headers = $config->get('graphql.headers', []); @@ -59,7 +54,6 @@ function (BaseOperationParams $baseOperationParams) use ($schemaName, $graphql): */ protected function createBatchingNotSupportedResponse(array $input): array { - $count = min(\count($input), 100); $data = []; @@ -76,13 +70,4 @@ protected function createBatchingNotSupportedResponse(array $input): array return $data; } - - protected function findSchemaNameInRequest(Request $request, string $routePrefix): ?string - { - $path = str($request->route()->uri())->ltrim('/')->start('/'); - if (!$path->startsWith($routePrefix)) { - return null; - } - return $path->after($routePrefix)->trim('/')->toString(); - } } diff --git a/src/GraphQLHttpMiddleware.php b/src/GraphQLHttpMiddleware.php new file mode 100644 index 00000000..d9f27087 --- /dev/null +++ b/src/GraphQLHttpMiddleware.php @@ -0,0 +1,22 @@ +server->set('graphql.schemaName', $schemaName); + + return $next($request); + } +} diff --git a/src/routes.php b/src/routes.php index 0a3e0d19..e4f8fdad 100644 --- a/src/routes.php +++ b/src/routes.php @@ -2,63 +2,115 @@ declare(strict_types = 1); -use Illuminate\Container\Container; -use Illuminate\Contracts\Config\Repository; -use Illuminate\Routing\Router; +use Illuminate\Support\Facades\Route; use Rebing\GraphQL\GraphQL; use Rebing\GraphQL\GraphQLController; +use Rebing\GraphQL\GraphQLHttpMiddleware; +use Rebing\GraphQL\Support\Contracts\ConfigConvertible; -/** @var Repository $config */ -$config = Container::getInstance()->make(Repository::class); - -$routeConfig = $config->get('graphql.route'); - -if ($routeConfig) { - /** @var Router $router */ - $router = app('router'); +if (!\function_exists('registerGraphQLRoute')) { + function registerGraphQLRoute($schemaName, $schemaConfig, $routeConfig, $alias = null): void + { + if (null !== $alias) { + $routeName = $alias ? ".$alias" : ''; + } else { + $routeName = $schemaName ? ".$schemaName" : ''; + } - $routeGroupAttributes = array_merge( - [ - 'prefix' => $routeConfig['prefix'] ?? 'graphql', - 'middleware' => $routeConfig['middleware'] ?? [], - ], - $routeConfig['group_attributes'] ?? [] - ); + Route::match( + $schemaConfig['method'] ?? ['GET', 'POST'], + $alias ?? $schemaName, + $schemaConfig['controller'] ?? $routeConfig['controller'] ?? [GraphQLController::class, 'query'], + )->middleware(array_merge( + [GraphQLHttpMiddleware::class . ":$schemaName"], + $schemaConfig['middleware'] ?? [] + ))->name($routeName); + } +} - $router->group( - $routeGroupAttributes, - function (Router $router) use ($config, $routeConfig): void { - $schemas = GraphQL::getNormalizedSchemasConfiguration(); - $defaultSchema = $config->get('graphql.default_schema', 'default'); +$routeConfig = config('graphql.route'); - foreach ($schemas as $schemaName => $schemaConfig) { - $method = $schemaConfig['method'] ?? ['GET', 'POST']; - $actions = array_filter([ - 'uses' => $schemaConfig['controller'] ?? $routeConfig['controller'] ?? GraphQLController::class . '@query', - 'middleware' => $schemaConfig['middleware'] ?? $routeConfig['middleware'] ?? null, - ]); +if (empty($routeConfig)) { + return; +} - // Support array syntax: `[Some::class, 'method']` - if (\is_array($actions['uses']) && isset($actions['uses'][0], $actions['uses'][1])) { - $actions['uses'] = $actions['uses'][0] . '@' . $actions['uses'][1]; - } +$defaultSchemaName = config('graphql.default_schema', 'default'); +$schemasConfig = GraphQL::getNormalizedSchemasConfiguration(); - // Add route for each schema… - $router->addRoute( - $method, - $schemaName, - $actions + ['as' => "graphql.$schemaName"] - ); +Route::group([ + 'prefix' => $routeConfig['prefix'] ?? 'graphql', + 'middleware' => $routeConfig['middleware'] ?? [], + 'as' => 'graphql', + ...$routeConfig['group_attributes'] ?? [], +], function () use (&$routeConfig, $defaultSchemaName): void { + foreach (config('graphql.schemas', []) as $schemaName => $schemaConfig) { + if (\is_string($schemaConfig) && class_exists($schemaConfig)) { + $classSchema = (new $schemaConfig()); - // … and the default schema against the group itself - if ($schemaName === $defaultSchema) { - $router->addRoute( - $method, - '', - $actions + ['as' => 'graphql'] - ); - } + if ($classSchema instanceof ConfigConvertible) { + $schemaConfig = $classSchema->toConfig(); } } - ); -} + + if ($defaultSchemaName === $schemaName) { + registerGraphQLRoute($schemaName, $schemaConfig, $routeConfig, ''); + } + registerGraphQLRoute($schemaName, $schemaConfig, $routeConfig); + } +}); + +// +///** @var Repository $config */ +//$config = Container::getInstance()->make(Repository::class); +// +//$routeConfig = $config->get('graphql.route'); +// +//if ($routeConfig) { +// /** @var Router $router */ +// $router = app('router'); +// +// $routeGroupAttributes = array_merge( +// [ +// 'prefix' => $routeConfig['prefix'] ?? 'graphql', +// 'middleware' => $routeConfig['middleware'] ?? [], +// ], +// $routeConfig['group_attributes'] ?? [] +// ); +// +// $router->group( +// $routeGroupAttributes, +// function (Router $router) use ($config, $routeConfig): void { +// $schemas = GraphQL::getNormalizedSchemasConfiguration(); +// $defaultSchema = $config->get('graphql.default_schema', 'default'); +// +// foreach ($schemas as $schemaName => $schemaConfig) { +// $method = $schemaConfig['method'] ?? ['GET', 'POST']; +// $actions = array_filter([ +// 'uses' => $schemaConfig['controller'] ?? $routeConfig['controller'] ?? GraphQLController::class . '@query', +// 'middleware' => $schemaConfig['middleware'] ?? $routeConfig['middleware'] ?? null, +// ]); +// +// // Support array syntax: `[Some::class, 'method']` +// if (\is_array($actions['uses']) && isset($actions['uses'][0], $actions['uses'][1])) { +// $actions['uses'] = $actions['uses'][0] . '@' . $actions['uses'][1]; +// } +// +// // Add route for each schema… +// $router->addRoute( +// $method, +// $schemaName, +// $actions + ['as' => "graphql.$schemaName"] +// ); +// +// // … and the default schema against the group itself +// if ($schemaName === $defaultSchema) { +// $router->addRoute( +// $method, +// '', +// $actions + ['as' => 'graphql'] +// ); +// } +// } +// } +// ); +//} diff --git a/tests/Unit/EndpointParamsTest.php b/tests/Unit/EndpointParamsTest.php index bfacecde..32cb9d66 100644 --- a/tests/Unit/EndpointParamsTest.php +++ b/tests/Unit/EndpointParamsTest.php @@ -1,12 +1,8 @@ set('graphql.route.prefix', 'graphql/{parameter}'); } - } diff --git a/tests/Unit/RoutesTest.php b/tests/Unit/RoutesTest.php index c64c5745..c84113bd 100644 --- a/tests/Unit/RoutesTest.php +++ b/tests/Unit/RoutesTest.php @@ -5,6 +5,7 @@ use Illuminate\Routing\Route; use Illuminate\Support\Collection; +use Rebing\GraphQL\GraphQLHttpMiddleware; use Rebing\GraphQL\Tests\Support\Objects\ExampleMiddleware; use Rebing\GraphQL\Tests\Support\Objects\ExampleSchema; use Rebing\GraphQL\Tests\Support\Objects\ExampleSchemaWithMethod; @@ -47,6 +48,7 @@ public function testRoutes(): void ], 'uri' => 'graphql_test', 'middleware' => [ + GraphQLHttpMiddleware::class . ':default', ExampleMiddleware::class, ], ], @@ -58,6 +60,7 @@ public function testRoutes(): void ], 'uri' => 'graphql_test/default', 'middleware' => [ + GraphQLHttpMiddleware::class . ':default', ExampleMiddleware::class, ], ], @@ -69,6 +72,7 @@ public function testRoutes(): void ], 'uri' => 'graphql_test/custom', 'middleware' => [ + GraphQLHttpMiddleware::class . ':custom', ExampleMiddleware::class, ], ], @@ -78,6 +82,7 @@ public function testRoutes(): void ], 'uri' => 'graphql_test/with_methods', 'middleware' => [ + GraphQLHttpMiddleware::class . ':with_methods', ExampleMiddleware::class, ], ], @@ -89,6 +94,7 @@ public function testRoutes(): void ], 'uri' => 'graphql_test/class_based', 'middleware' => [ + GraphQLHttpMiddleware::class . ':class_based', ExampleMiddleware::class, ], ], @@ -98,6 +104,7 @@ public function testRoutes(): void ], 'uri' => 'graphql_test/class_based_with_methods', 'middleware' => [ + GraphQLHttpMiddleware::class . ':class_based_with_methods', ExampleMiddleware::class, ], ], From f0eecd2391526e518679b79152e12efe9c66cd5b Mon Sep 17 00:00:00 2001 From: Colant Alan <19172637+alancolant@users.noreply.github.com> Date: Sun, 9 Jul 2023 16:04:36 +0200 Subject: [PATCH 5/9] remove unused code --- src/routes.php | 65 -------------------------------------------------- 1 file changed, 65 deletions(-) diff --git a/src/routes.php b/src/routes.php index e4f8fdad..15c0b09a 100644 --- a/src/routes.php +++ b/src/routes.php @@ -6,7 +6,6 @@ use Rebing\GraphQL\GraphQL; use Rebing\GraphQL\GraphQLController; use Rebing\GraphQL\GraphQLHttpMiddleware; -use Rebing\GraphQL\Support\Contracts\ConfigConvertible; if (!\function_exists('registerGraphQLRoute')) { function registerGraphQLRoute($schemaName, $schemaConfig, $routeConfig, $alias = null): void @@ -44,73 +43,9 @@ function registerGraphQLRoute($schemaName, $schemaConfig, $routeConfig, $alias = ...$routeConfig['group_attributes'] ?? [], ], function () use (&$routeConfig, $defaultSchemaName): void { foreach (config('graphql.schemas', []) as $schemaName => $schemaConfig) { - if (\is_string($schemaConfig) && class_exists($schemaConfig)) { - $classSchema = (new $schemaConfig()); - - if ($classSchema instanceof ConfigConvertible) { - $schemaConfig = $classSchema->toConfig(); - } - } - if ($defaultSchemaName === $schemaName) { registerGraphQLRoute($schemaName, $schemaConfig, $routeConfig, ''); } registerGraphQLRoute($schemaName, $schemaConfig, $routeConfig); } }); - -// -///** @var Repository $config */ -//$config = Container::getInstance()->make(Repository::class); -// -//$routeConfig = $config->get('graphql.route'); -// -//if ($routeConfig) { -// /** @var Router $router */ -// $router = app('router'); -// -// $routeGroupAttributes = array_merge( -// [ -// 'prefix' => $routeConfig['prefix'] ?? 'graphql', -// 'middleware' => $routeConfig['middleware'] ?? [], -// ], -// $routeConfig['group_attributes'] ?? [] -// ); -// -// $router->group( -// $routeGroupAttributes, -// function (Router $router) use ($config, $routeConfig): void { -// $schemas = GraphQL::getNormalizedSchemasConfiguration(); -// $defaultSchema = $config->get('graphql.default_schema', 'default'); -// -// foreach ($schemas as $schemaName => $schemaConfig) { -// $method = $schemaConfig['method'] ?? ['GET', 'POST']; -// $actions = array_filter([ -// 'uses' => $schemaConfig['controller'] ?? $routeConfig['controller'] ?? GraphQLController::class . '@query', -// 'middleware' => $schemaConfig['middleware'] ?? $routeConfig['middleware'] ?? null, -// ]); -// -// // Support array syntax: `[Some::class, 'method']` -// if (\is_array($actions['uses']) && isset($actions['uses'][0], $actions['uses'][1])) { -// $actions['uses'] = $actions['uses'][0] . '@' . $actions['uses'][1]; -// } -// -// // Add route for each schema… -// $router->addRoute( -// $method, -// $schemaName, -// $actions + ['as' => "graphql.$schemaName"] -// ); -// -// // … and the default schema against the group itself -// if ($schemaName === $defaultSchema) { -// $router->addRoute( -// $method, -// '', -// $actions + ['as' => 'graphql'] -// ); -// } -// } -// } -// ); -//} From 8ea800b05f4aeed24a45a5d76812e946ae8a0549 Mon Sep 17 00:00:00 2001 From: Colant Alan <19172637+alancolant@users.noreply.github.com> Date: Sun, 9 Jul 2023 16:05:25 +0200 Subject: [PATCH 6/9] remove unused code --- src/routes.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/routes.php b/src/routes.php index 15c0b09a..46223ffe 100644 --- a/src/routes.php +++ b/src/routes.php @@ -1,6 +1,6 @@ $routeConfig['prefix'] ?? 'graphql', + 'prefix' => $routeConfig['prefix'] ?? 'graphql', 'middleware' => $routeConfig['middleware'] ?? [], - 'as' => 'graphql', + 'as' => 'graphql', ...$routeConfig['group_attributes'] ?? [], -], function () use (&$routeConfig, $defaultSchemaName): void { - foreach (config('graphql.schemas', []) as $schemaName => $schemaConfig) { +], function () use (&$routeConfig, &$defaultSchemaName, &$schemasConfig): void { + foreach ($schemasConfig as $schemaName => $schemaConfig) { if ($defaultSchemaName === $schemaName) { registerGraphQLRoute($schemaName, $schemaConfig, $routeConfig, ''); } From d2cb03396e8a78d19d6dc3c6d8ffce8619cd89d4 Mon Sep 17 00:00:00 2001 From: Colant Alan <19172637+alancolant@users.noreply.github.com> Date: Sun, 9 Jul 2023 16:07:06 +0200 Subject: [PATCH 7/9] run fix style --- src/routes.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/routes.php b/src/routes.php index 46223ffe..2201a4aa 100644 --- a/src/routes.php +++ b/src/routes.php @@ -1,6 +1,6 @@ $routeConfig['prefix'] ?? 'graphql', + 'prefix' => $routeConfig['prefix'] ?? 'graphql', 'middleware' => $routeConfig['middleware'] ?? [], - 'as' => 'graphql', + 'as' => 'graphql', ...$routeConfig['group_attributes'] ?? [], ], function () use (&$routeConfig, &$defaultSchemaName, &$schemasConfig): void { foreach ($schemasConfig as $schemaName => $schemaConfig) { From fe5fb8971ad289ebc2d98ccf8bcec72040ffee1a Mon Sep 17 00:00:00 2001 From: Colant Alan <19172637+alancolant@users.noreply.github.com> Date: Sun, 9 Jul 2023 16:32:06 +0200 Subject: [PATCH 8/9] externalise function getroute in graphql --- src/GraphQL.php | 21 ++++++++++++++++++++- src/routes.php | 34 ++++------------------------------ 2 files changed, 24 insertions(+), 31 deletions(-) diff --git a/src/GraphQL.php b/src/GraphQL.php index 9b04a93a..1dfdf764 100644 --- a/src/GraphQL.php +++ b/src/GraphQL.php @@ -19,6 +19,7 @@ use Illuminate\Contracts\Debug\ExceptionHandler; use Illuminate\Pipeline\Pipeline; use Illuminate\Support\Facades\Config; +use Illuminate\Support\Facades\Route; use Illuminate\Support\Traits\Macroable; use Illuminate\Validation\ValidationException; use Rebing\GraphQL\Error\AuthorizationError; @@ -508,8 +509,8 @@ public function wrapType(string $typeName, string $customTypeName, string $wrapp } /** - * @see \GraphQL\Executor\ExecutionResult::setErrorFormatter * @return array + * @see \GraphQL\Executor\ExecutionResult::setErrorFormatter */ public static function formatError(Error $e): array { @@ -635,6 +636,24 @@ public static function getNormalizedSchemaConfiguration(string $schemaName): arr return $schemaConfig; } + public static function parseRoute(string $schemaName, array $schemaConfig, array $routeConfig, ?string $alias = null): \Illuminate\Routing\Route + { + if (null !== $alias) { + $routeName = $alias ? ".$alias" : ''; + } else { + $routeName = $schemaName ? ".$schemaName" : ''; + } + + return Route::match( + $schemaConfig['method'] ?? ['GET', 'POST'], + $alias ?? $schemaName, + $schemaConfig['controller'] ?? $routeConfig['controller'] ?? [GraphQLController::class, 'query'], + )->middleware(array_merge( + [GraphQLHttpMiddleware::class . ":$schemaName"], + $schemaConfig['middleware'] ?? [] + ))->name($routeName); + } + public function decorateExecutionResult(ExecutionResult $executionResult): ExecutionResult { $errorFormatter = $this->config->get('graphql.error_formatter', [static::class, 'formatError']); diff --git a/src/routes.php b/src/routes.php index 2201a4aa..ff91fa55 100644 --- a/src/routes.php +++ b/src/routes.php @@ -4,28 +4,6 @@ use Illuminate\Support\Facades\Route; use Rebing\GraphQL\GraphQL; -use Rebing\GraphQL\GraphQLController; -use Rebing\GraphQL\GraphQLHttpMiddleware; - -if (!\function_exists('registerGraphQLRoute')) { - function registerGraphQLRoute($schemaName, $schemaConfig, $routeConfig, $alias = null): void - { - if (null !== $alias) { - $routeName = $alias ? ".$alias" : ''; - } else { - $routeName = $schemaName ? ".$schemaName" : ''; - } - - Route::match( - $schemaConfig['method'] ?? ['GET', 'POST'], - $alias ?? $schemaName, - $schemaConfig['controller'] ?? $routeConfig['controller'] ?? [GraphQLController::class, 'query'], - )->middleware(array_merge( - [GraphQLHttpMiddleware::class . ":$schemaName"], - $schemaConfig['middleware'] ?? [] - ))->name($routeName); - } -} $routeConfig = config('graphql.route'); @@ -41,11 +19,7 @@ function registerGraphQLRoute($schemaName, $schemaConfig, $routeConfig, $alias = 'middleware' => $routeConfig['middleware'] ?? [], 'as' => 'graphql', ...$routeConfig['group_attributes'] ?? [], -], function () use (&$routeConfig, &$defaultSchemaName, &$schemasConfig): void { - foreach ($schemasConfig as $schemaName => $schemaConfig) { - if ($defaultSchemaName === $schemaName) { - registerGraphQLRoute($schemaName, $schemaConfig, $routeConfig, ''); - } - registerGraphQLRoute($schemaName, $schemaConfig, $routeConfig); - } -}); +], fn () => collect($schemasConfig)->map(fn ($schemaConfig, $schemaName) => array_merge( + [GraphQL::parseRoute($schemaName, $schemaConfig, $routeConfig)], + $schemaName === $defaultSchemaName ? [GraphQL::parseRoute($schemaName, $schemaConfig, $routeConfig, '')] : [] +))->flatten(1)); From 373fe28a39f21dd831de625f6020c8fd0e5bd0cb Mon Sep 17 00:00:00 2001 From: Colant Alan <19172637+alancolant@users.noreply.github.com> Date: Sun, 9 Jul 2023 16:41:14 +0200 Subject: [PATCH 9/9] remove fn()=>callback instead of more simple function --- src/routes.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/routes.php b/src/routes.php index ff91fa55..228df39e 100644 --- a/src/routes.php +++ b/src/routes.php @@ -12,14 +12,17 @@ } $defaultSchemaName = config('graphql.default_schema', 'default'); -$schemasConfig = GraphQL::getNormalizedSchemasConfiguration(); - Route::group([ 'prefix' => $routeConfig['prefix'] ?? 'graphql', 'middleware' => $routeConfig['middleware'] ?? [], 'as' => 'graphql', ...$routeConfig['group_attributes'] ?? [], -], fn () => collect($schemasConfig)->map(fn ($schemaConfig, $schemaName) => array_merge( - [GraphQL::parseRoute($schemaName, $schemaConfig, $routeConfig)], - $schemaName === $defaultSchemaName ? [GraphQL::parseRoute($schemaName, $schemaConfig, $routeConfig, '')] : [] -))->flatten(1)); +], function () use (&$defaultSchemaName, &$routeConfig): void { + foreach (GraphQL::getNormalizedSchemasConfiguration() as $schemaName => $schemaConfig) { + GraphQL::parseRoute($schemaName, $schemaConfig, $routeConfig); + + if ($schemaName === $defaultSchemaName) { + GraphQL::parseRoute($schemaName, $schemaConfig, $routeConfig, ''); + } + } +});