Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
35 changes: 7 additions & 28 deletions src/Schema/SchemaBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<string, \GraphQL\Type\Definition\Type>
*/
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(
Expand Down