Skip to content

Commit

Permalink
Allow callable in args
Browse files Browse the repository at this point in the history
  • Loading branch information
ruudk committed Dec 5, 2023
1 parent c6e174a commit b954661
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/Type/Definition/Argument.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,18 @@ public function __construct(array $config)
}

/**
* @phpstan-param ArgumentListConfig $config
* @phpstan-param ArgumentListConfig|callable(): ArgumentListConfig $config
*
* @return array<int, self>
*/
public static function listFromConfig(iterable $config): array
public static function listFromConfig($config): array
{
$list = [];

if (\is_callable($config)) {
$config = $config();
}

foreach ($config as $name => $argConfig) {
if (! \is_array($argConfig)) {
$argConfig = ['type' => $argConfig];
Expand Down
4 changes: 2 additions & 2 deletions src/Type/Definition/FieldDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* name: string,
* type: FieldType,
* resolve?: FieldResolver|null,
* args?: ArgumentListConfig|null,
* args?: ArgumentListConfig|callable(): ArgumentListConfig|null,
* description?: string|null,
* visible?: VisibilityFn|bool,
* deprecationReason?: string|null,
Expand All @@ -31,7 +31,7 @@
* @phpstan-type UnnamedFieldDefinitionConfig array{
* type: FieldType,
* resolve?: FieldResolver|null,
* args?: ArgumentListConfig|null,
* args?: ArgumentListConfig|callable(): ArgumentListConfig|null,
* description?: string|null,
* visible?: VisibilityFn|bool,
* deprecationReason?: string|null,
Expand Down
20 changes: 20 additions & 0 deletions tests/Type/LazyDefinitionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,24 @@ public function testLazyRootTypesNull(): void
self::assertNull($schema->getMutationType());
self::assertNull($schema->getSubscriptionType());
}

public function testLazyArgs(): void
{
$objType = new ObjectType([
'name' => 'SomeObject',
'fields' => [
'f' => [
'type' => Type::string(),
'args' => static fn (): array => [
'id' => ['type' => Type::int()],
],
],
],
]);

$objType->assertValid();

self::assertNotNull($objType->getField('f')->getArg('id'));
self::assertSame(Type::int(), $objType->getField('f')->getArg('id')->getType());
}
}

0 comments on commit b954661

Please sign in to comment.