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 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..a27b0d19c1 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,17 @@ 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( + /** @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)); // 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(