Skip to content

Commit 86d5a65

Browse files
authored
Allow lazy root type callables to return null
1 parent a92b074 commit 86d5a65

File tree

5 files changed

+29
-10
lines changed

5 files changed

+29
-10
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ You can find and compare releases at the [GitHub release page](https://github.co
99

1010
## Unreleased
1111

12+
## v15.6.1
13+
14+
### Fixed
15+
16+
- Allow lazy root type callables to return `null` https://github.com/webonyx/graphql-php/pull/1422
17+
1218
## v15.6.0
1319

1420
### Added

docs/class-reference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ Usage example:
535535

536536
@see Type, NamedType
537537

538-
@phpstan-type MaybeLazyObjectType ObjectType|(callable(): ObjectType)|null
538+
@phpstan-type MaybeLazyObjectType ObjectType|(callable(): (ObjectType|null))|null
539539
@phpstan-type TypeLoader callable(string $typeName): ((Type&NamedType)|null)
540540
@phpstan-type Types iterable<Type&NamedType>|(callable(): iterable<Type&NamedType>)
541541
@phpstan-type SchemaConfigOptions array{

docs/schema-definition.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,14 @@ with complex input values (see [Mutations and Input Types](type-definitions/inpu
7878
The schema constructor expects an instance of [`GraphQL\Type\SchemaConfig`](class-reference.md#graphqltypeschemaconfig)
7979
or an array with the following options:
8080

81-
| Option | Type | Notes |
82-
| ------------ | -------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
83-
| query | `ObjectType` or `callable(): ObjectType` or `null` | **Required.** Object type (usually named `Query`) containing root-level fields of your read API |
84-
| mutation | `ObjectType` or `callable(): ObjectType` or `null` | Object type (usually named `Mutation`) containing root-level fields of your write API |
85-
| subscription | `ObjectType` or `callable(): ObjectType` or `null` | Reserved for future subscriptions implementation. Currently presented for compatibility with introspection query of **graphql-js**, used by various clients (like Relay or GraphiQL) |
86-
| directives | `array<Directive>` | A full list of [directives](type-definitions/directives.md) supported by your schema. By default, contains built-in **@skip** and **@include** directives.<br><br> If you pass your own directives and still want to use built-in directives - add them explicitly. For example:<br><br> _array_merge(GraphQL::getStandardDirectives(), [$myCustomDirective]);_ |
87-
| types | `array<ObjectType>` | List of object types which cannot be detected by **graphql-php** during static schema analysis.<br><br>Most often this happens when the object type is never referenced in fields directly but is still a part of a schema because it implements an interface which resolves to this object type in its **resolveType** callable. <br><br> Note that you are not required to pass all of your types here - it is simply a workaround for a concrete use-case. |
88-
| typeLoader | `callable(string $name): Type` | Expected to return a type instance given the name. Must always return the same instance if called multiple times, see [lazy loading](#lazy-loading-of-types). See section below on lazy type loading. |
81+
| Option | Type | Notes |
82+
| ------------ | --------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
83+
| query | `ObjectType` or `callable(): ?ObjectType` or `null` | **Required.** Object type (usually named `Query`) containing root-level fields of your read API |
84+
| mutation | `ObjectType` or `callable(): ?ObjectType` or `null` | Object type (usually named `Mutation`) containing root-level fields of your write API |
85+
| subscription | `ObjectType` or `callable(): ?ObjectType` or `null` | Reserved for future subscriptions implementation. Currently presented for compatibility with introspection query of **graphql-js**, used by various clients (like Relay or GraphiQL) |
86+
| directives | `array<Directive>` | A full list of [directives](type-definitions/directives.md) supported by your schema. By default, contains built-in **@skip** and **@include** directives.<br><br> If you pass your own directives and still want to use built-in directives - add them explicitly. For example:<br><br> _array_merge(GraphQL::getStandardDirectives(), [$myCustomDirective]);_ |
87+
| types | `array<ObjectType>` | List of object types which cannot be detected by **graphql-php** during static schema analysis.<br><br>Most often this happens when the object type is never referenced in fields directly but is still a part of a schema because it implements an interface which resolves to this object type in its **resolveType** callable. <br><br> Note that you are not required to pass all of your types here - it is simply a workaround for a concrete use-case. |
88+
| typeLoader | `callable(string $name): Type` | Expected to return a type instance given the name. Must always return the same instance if called multiple times, see [lazy loading](#lazy-loading-of-types). See section below on lazy type loading. |
8989

9090
### Using config class
9191

src/Type/SchemaConfig.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
*
2727
* @see Type, NamedType
2828
*
29-
* @phpstan-type MaybeLazyObjectType ObjectType|(callable(): ObjectType)|null
29+
* @phpstan-type MaybeLazyObjectType ObjectType|(callable(): (ObjectType|null))|null
3030
* @phpstan-type TypeLoader callable(string $typeName): ((Type&NamedType)|null)
3131
* @phpstan-type Types iterable<Type&NamedType>|(callable(): iterable<Type&NamedType>)
3232
* @phpstan-type SchemaConfigOptions array{

tests/Type/LazyDefinitionTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,17 @@ public function testLazyRootTypes(): void
208208
self::assertSame($schema->getMutationType(), $mutation);
209209
self::assertSame($schema->getSubscriptionType(), $subscription);
210210
}
211+
212+
public function testLazyRootTypesNull(): void
213+
{
214+
$schema = new Schema([
215+
'query' => fn () => null,
216+
'mutation' => fn () => null,
217+
'subscription' => fn () => null,
218+
]);
219+
220+
self::assertNull($schema->getQueryType());
221+
self::assertNull($schema->getMutationType());
222+
self::assertNull($schema->getSubscriptionType());
223+
}
211224
}

0 commit comments

Comments
 (0)