Skip to content

Commit

Permalink
Merge 362a77d into 8df55fd
Browse files Browse the repository at this point in the history
  • Loading branch information
ruudk committed Oct 29, 2021
2 parents 8df55fd + 362a77d commit 90217f8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,10 @@

#### Unreleased

#### 14.11.0

- Allow field definitions to be defined as any `iterable`, not just `array`

#### 14.10.0

Feat:
Expand Down
5 changes: 3 additions & 2 deletions src/Type/Definition/FieldDefinition.php
Expand Up @@ -12,6 +12,7 @@
use GraphQL\Utils\Utils;
use function is_array;
use function is_callable;
use function is_iterable;
use function is_string;
use function sprintf;

Expand Down Expand Up @@ -95,9 +96,9 @@ public static function defineFieldMap(Type $type, $fields) : array
if (is_callable($fields)) {
$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.', $type->name)
sprintf('%s fields must be an iterable or a callable which returns such an iterable.', $type->name)
);
}
$map = [];
Expand Down
5 changes: 3 additions & 2 deletions src/Type/Definition/InputObjectType.php
Expand Up @@ -11,6 +11,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 @@ -80,9 +81,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
Expand Up @@ -4,6 +4,7 @@

namespace GraphQL\Tests\Type;

use Generator;
use GraphQL\Error\InvariantViolation;
use GraphQL\Error\Warning;
use GraphQL\Tests\PHPUnit\ArraySubsetAsserts;
Expand All @@ -15,9 +16,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 @@ -2082,4 +2085,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());
}
}

0 comments on commit 90217f8

Please sign in to comment.