Skip to content

Commit

Permalink
Register root types lazily
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia committed Aug 7, 2023
1 parent 834bc82 commit 655e692
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 30 deletions.
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
34 changes: 5 additions & 29 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,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<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

0 comments on commit 655e692

Please sign in to comment.