Skip to content
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ You can find and compare releases at the [GitHub release page](https://github.co
- Expose structured enumeration of directive locations
- Add `AST::concatAST()` utility
- Allow lazy input object fields
- Allow field definitions to be defined as any `iterable`, not just `array`

### Optimized

Expand Down
5 changes: 3 additions & 2 deletions src/Type/Definition/FieldDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use function is_array;
use function is_callable;
use function is_iterable;
use function is_string;
use function sprintf;

Expand Down Expand Up @@ -86,9 +87,9 @@ public static function defineFieldMap(Type $type, $fields): array
$fields = $fields();
}

if (! is_array($fields)) {
if (! is_iterable($fields)) {
throw new InvariantViolation(
"{$type->name} fields must be an array or a callable which returns such an array."
"{$type->name} fields must be an iterable or a callable which returns such an iterable."
);
}

Expand Down
5 changes: 3 additions & 2 deletions src/Type/Definition/InputObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use function count;
use function is_array;
use function is_callable;
use function is_iterable;
use function is_string;
use function sprintf;

Expand Down Expand Up @@ -96,9 +97,9 @@ protected function initializeFields(): void
$fields = $fields();
}

if (! is_array($fields)) {
if (! is_iterable($fields)) {
throw new InvariantViolation(
sprintf('%s fields must be an array or a callable which returns such an array.', $this->name)
sprintf('%s fields must be an iterable or a callable which returns such an iterable.', $this->name)
);
}

Expand Down
26 changes: 26 additions & 0 deletions tests/Type/DefinitionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace GraphQL\Tests\Type;

use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
use Generator;
use GraphQL\Error\InvariantViolation;
use GraphQL\Tests\Type\TestClasses\MyCustomType;
use GraphQL\Tests\Type\TestClasses\OtherCustom;
Expand All @@ -14,9 +15,11 @@
use GraphQL\Type\Definition\InputObjectField;
use GraphQL\Type\Definition\InputObjectType;
use GraphQL\Type\Definition\InterfaceType;
use GraphQL\Type\Definition\IntType;
use GraphQL\Type\Definition\ListOfType;
use GraphQL\Type\Definition\NonNull;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\StringType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\UnionType;
use GraphQL\Type\Schema;
Expand Down Expand Up @@ -2040,4 +2043,27 @@ public function testThrowsWhenLazyLoadedFieldHasInvalidArgs(): void

$objType->assertValid();
}

public function testReturningFieldsUsingYield()
{
$type = new ObjectType([
'name' => 'Query',
'fields' => static function (): Generator {
yield 'url' => ['type' => Type::string()];
yield 'width' => ['type' => Type::int()];
},
]);

$blogSchema = new Schema(['query' => $type]);

self::assertSame($blogSchema->getQueryType(), $type);

$field = $type->getField('url');
self::assertSame($field->name, 'url');
self::assertInstanceOf(StringType::class, $field->getType());

$field = $type->getField('width');
self::assertSame($field->name, 'width');
self::assertInstanceOf(IntType::class, $field->getType());
}
}