Skip to content

Commit

Permalink
Added AstManipulator::findField().
Browse files Browse the repository at this point in the history
  • Loading branch information
LastDragon-ru committed Oct 3, 2023
1 parent 3e69b99 commit c83c1ee
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 12 deletions.
45 changes: 33 additions & 12 deletions packages/graphql/src/Utils/AstManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,28 @@ public function getInterfaces(
return $interfaces;
}

/**
* @param Closure(FieldDefinitionNode|FieldDefinition): bool $closure
*/
public function findField(
InterfaceTypeDefinitionNode|ObjectTypeDefinitionNode|HasFieldsType $node,
Closure $closure,
): FieldDefinitionNode|FieldDefinition|null {
$found = null;
$fields = $node instanceof HasFieldsType
? $node->getFields()
: $node->fields;

foreach ($fields as $field) {
if ($closure($field)) {
$found = $field;
break;
}
}

return $found;
}

/**
* @return ($node is HasFieldsType ? FieldDefinition : FieldDefinitionNode)|null
*/
Expand All @@ -502,12 +524,9 @@ public function getField(
if ($node instanceof HasFieldsType) {
$field = $node->hasField($name) ? $node->getField($name) : null;
} else {
foreach ($node->fields as $nodeField) {
if ($this->getName($nodeField) === $name) {
$field = $nodeField;
break;
}
}
$field = $this->findField($node, function (mixed $field) use ($name): bool {
return $this->getName($field) === $name;
});
}

return $field;
Expand Down Expand Up @@ -576,12 +595,14 @@ public function addArgument(
): InputValueDefinitionNode|Argument {
// Added?
if ($this->getArgument($field, $name)) {
throw new ArgumentAlreadyDefined(sprintf(
'%s { %s(%s) }',
$this->getTypeFullName($definition),
$this->getName($field),
$name,
));
throw new ArgumentAlreadyDefined(
sprintf(
'%s { %s(%s) }',
$this->getTypeFullName($definition),
$this->getName($field),
$name,
),
);
}

// Add
Expand Down
54 changes: 54 additions & 0 deletions packages/graphql/src/Utils/AstManipulatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,60 @@ public function testSetArgumentType(
$this->assertGraphQLExportableEquals($expected, $definition);
}
}

public function testFindField(): void {
// Prepare
$manipulator = $this->getManipulator();
$node = Parser::objectTypeDefinition(
<<<'GRAPHQL'
type Test {
a: Int
b: String
c: Boolean
}
GRAPHQL,
);
$type = new ObjectType([
'name' => 'Test',
'fields' => [
new FieldDefinition([
'name' => 'a',
'type' => Type::int(),
]),
new FieldDefinition([
'name' => 'b',
'type' => Type::string(),
]),
new FieldDefinition([
'name' => 'c',
'type' => Type::boolean(),
]),
],
'astNode' => $node,
]);

// Test
$nodeField = $manipulator->findField(
$node,
static function (mixed $field) use ($manipulator): bool {
return $manipulator->getTypeName($field) === Type::INT;
},
);

self::assertInstanceOf(FieldDefinitionNode::class, $nodeField);
self::assertEquals('a', $nodeField->name->value);

$typeField = $manipulator->findField(
$type,
static function (mixed $field) use ($manipulator): bool {
return $manipulator->getTypeName($field) === Type::BOOLEAN;
},
);

self::assertInstanceOf(FieldDefinition::class, $typeField);
self::assertEquals('c', $typeField->name);
}

//</editor-fold>

// <editor-fold desc="Helpers">
Expand Down

0 comments on commit c83c1ee

Please sign in to comment.