From 655e6920469aa0ba7aa32011fbfcbe09862d07ae Mon Sep 17 00:00:00 2001 From: Benedikt Franke Date: Mon, 7 Aug 2023 15:36:21 +0200 Subject: [PATCH 1/6] Register root types lazily See https://github.com/webonyx/graphql-php/pull/1418 --- composer.json | 2 +- src/Schema/SchemaBuilder.php | 34 +++++----------------------------- 2 files changed, 6 insertions(+), 30 deletions(-) diff --git a/composer.json b/composer.json index 3f0d6ce2c3..8e0fe31f1b 100644 --- a/composer.json +++ b/composer.json @@ -40,7 +40,7 @@ "illuminate/validation": "^9 || ^10", "laragraph/utils": "^1.5 || ^2", "thecodingmachine/safe": "^1 || ^2", - "webonyx/graphql-php": "^15" + "webonyx/graphql-php": "^15.6.1" }, "require-dev": { "algolia/algoliasearch-client-php": "^3 || ^4", diff --git a/src/Schema/SchemaBuilder.php b/src/Schema/SchemaBuilder.php index f10c1c83a1..87d4dfbd96 100644 --- a/src/Schema/SchemaBuilder.php +++ b/src/Schema/SchemaBuilder.php @@ -3,7 +3,6 @@ namespace Nuwave\Lighthouse\Schema; use GraphQL\GraphQL; -use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\Type; use GraphQL\Type\Schema; use GraphQL\Type\SchemaConfig; @@ -35,37 +34,14 @@ protected function build(DocumentAST $documentAST): Schema $this->typeRegistry->setDocumentAST($documentAST); - // Always set Query since it is required - $query = $this->typeRegistry->get(RootType::QUERY); - assert($query instanceof ObjectType); - $config->setQuery($query); - - // Mutation and Subscription are optional, so only add them - // if they are present in the schema - if (isset($documentAST->types[RootType::MUTATION])) { - $mutation = $this->typeRegistry->get(RootType::MUTATION); - assert($mutation instanceof ObjectType); - $config->setMutation($mutation); - } - - if (isset($documentAST->types[RootType::SUBSCRIPTION])) { - $subscription = $this->typeRegistry->get(RootType::SUBSCRIPTION); - assert($subscription instanceof ObjectType); - $config->setSubscription($subscription); - } - // Use lazy type loading to prevent unnecessary work - $config->setTypeLoader( - fn (string $name): ?Type => $this->typeRegistry->search($name), - ); + $config->setQuery(fn (): Type => $this->typeRegistry->get(RootType::QUERY)); + $config->setMutation(fn (): ?Type => $this->typeRegistry->search(RootType::MUTATION)); + $config->setMutation(fn (): ?Type => $this->typeRegistry->search(RootType::SUBSCRIPTION)); + $config->setTypeLoader(fn (string $name): ?Type => $this->typeRegistry->search($name),); // Enables introspection to list all types in the schema - $config->setTypes( - /** - * @return array - */ - fn (): array => $this->typeRegistry->possibleTypes(), - ); + $config->setTypes(fn (): array => $this->typeRegistry->possibleTypes()); // There is no way to resolve directives lazily, so we convert them eagerly $directiveFactory = new DirectiveFactory( From 3ea0a837a1378530366ec9c71f7be7bbc341c922 Mon Sep 17 00:00:00 2001 From: Benedikt Franke Date: Mon, 7 Aug 2023 15:53:59 +0200 Subject: [PATCH 2/6] cl --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2baf1d2f34..8bae8425f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,10 @@ You can find and compare releases at the [GitHub release page](https://github.co ## Unreleased +### Changed + +- Register root types lazily https://github.com/nuwave/lighthouse/pull/2433 + ## v6.16.2 ### Fixed From c6e66c32ea025c5fba6be58c2a4486f3f63025bc Mon Sep 17 00:00:00 2001 From: spawnia Date: Mon, 7 Aug 2023 13:55:18 +0000 Subject: [PATCH 3/6] Apply php-cs-fixer changes --- src/Schema/SchemaBuilder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Schema/SchemaBuilder.php b/src/Schema/SchemaBuilder.php index 87d4dfbd96..0839572e58 100644 --- a/src/Schema/SchemaBuilder.php +++ b/src/Schema/SchemaBuilder.php @@ -38,7 +38,7 @@ protected function build(DocumentAST $documentAST): Schema $config->setQuery(fn (): Type => $this->typeRegistry->get(RootType::QUERY)); $config->setMutation(fn (): ?Type => $this->typeRegistry->search(RootType::MUTATION)); $config->setMutation(fn (): ?Type => $this->typeRegistry->search(RootType::SUBSCRIPTION)); - $config->setTypeLoader(fn (string $name): ?Type => $this->typeRegistry->search($name),); + $config->setTypeLoader(fn (string $name): ?Type => $this->typeRegistry->search($name)); // Enables introspection to list all types in the schema $config->setTypes(fn (): array => $this->typeRegistry->possibleTypes()); From 87f8003f7fe229436a8f3d5e7b633186f6a7c85a Mon Sep 17 00:00:00 2001 From: Benedikt Franke Date: Mon, 7 Aug 2023 16:12:56 +0200 Subject: [PATCH 4/6] subscription --- src/Schema/SchemaBuilder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Schema/SchemaBuilder.php b/src/Schema/SchemaBuilder.php index 0839572e58..5a7b26c9ff 100644 --- a/src/Schema/SchemaBuilder.php +++ b/src/Schema/SchemaBuilder.php @@ -37,7 +37,7 @@ protected function build(DocumentAST $documentAST): Schema // Use lazy type loading to prevent unnecessary work $config->setQuery(fn (): Type => $this->typeRegistry->get(RootType::QUERY)); $config->setMutation(fn (): ?Type => $this->typeRegistry->search(RootType::MUTATION)); - $config->setMutation(fn (): ?Type => $this->typeRegistry->search(RootType::SUBSCRIPTION)); + $config->setSubscription(fn (): ?Type => $this->typeRegistry->search(RootType::SUBSCRIPTION)); $config->setTypeLoader(fn (string $name): ?Type => $this->typeRegistry->search($name)); // Enables introspection to list all types in the schema From 6b4920021f03f1481aa4bf44ecb2364f62f5ab32 Mon Sep 17 00:00:00 2001 From: Benedikt Franke Date: Mon, 7 Aug 2023 16:13:51 +0200 Subject: [PATCH 5/6] stan --- src/Schema/SchemaBuilder.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Schema/SchemaBuilder.php b/src/Schema/SchemaBuilder.php index 5a7b26c9ff..1a4441f398 100644 --- a/src/Schema/SchemaBuilder.php +++ b/src/Schema/SchemaBuilder.php @@ -35,7 +35,10 @@ protected function build(DocumentAST $documentAST): Schema $this->typeRegistry->setDocumentAST($documentAST); // Use lazy type loading to prevent unnecessary work - $config->setQuery(fn (): Type => $this->typeRegistry->get(RootType::QUERY)); + $config->setQuery( + /** @return \GraphQL\Type\Definition\ObjectType */ + fn (): Type => $this->typeRegistry->get(RootType::QUERY) + ); $config->setMutation(fn (): ?Type => $this->typeRegistry->search(RootType::MUTATION)); $config->setSubscription(fn (): ?Type => $this->typeRegistry->search(RootType::SUBSCRIPTION)); $config->setTypeLoader(fn (string $name): ?Type => $this->typeRegistry->search($name)); From 4318afe963ae4f7090c6f0172191e22755fa9a00 Mon Sep 17 00:00:00 2001 From: spawnia Date: Mon, 7 Aug 2023 14:14:40 +0000 Subject: [PATCH 6/6] Apply php-cs-fixer changes --- src/Schema/SchemaBuilder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Schema/SchemaBuilder.php b/src/Schema/SchemaBuilder.php index 1a4441f398..a27b0d19c1 100644 --- a/src/Schema/SchemaBuilder.php +++ b/src/Schema/SchemaBuilder.php @@ -37,7 +37,7 @@ protected function build(DocumentAST $documentAST): Schema // Use lazy type loading to prevent unnecessary work $config->setQuery( /** @return \GraphQL\Type\Definition\ObjectType */ - fn (): Type => $this->typeRegistry->get(RootType::QUERY) + fn (): Type => $this->typeRegistry->get(RootType::QUERY), ); $config->setMutation(fn (): ?Type => $this->typeRegistry->search(RootType::MUTATION)); $config->setSubscription(fn (): ?Type => $this->typeRegistry->search(RootType::SUBSCRIPTION));