Skip to content

Commit

Permalink
feat: support creating field names from enums (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
simPod authored Aug 11, 2024
1 parent 42d5cd5 commit 6fde9c1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/Builder/FieldBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace SimPod\GraphQLUtils\Builder;

use BackedEnum;
use GraphQL\Executor\Executor;
use GraphQL\Type\Definition\Argument;
use GraphQL\Type\Definition\FieldDefinition;
Expand Down Expand Up @@ -35,7 +36,7 @@ class FieldBuilder
private array|null $args = null;

/** @phpstan-param FieldType $type */
final private function __construct(private string $name, $type)
final private function __construct(private BackedEnum|string $name, $type)
{
$this->type = $type;
}
Expand All @@ -45,7 +46,7 @@ final private function __construct(private string $name, $type)
*
* @return static
*/
public static function create(string $name, $type): self
public static function create(BackedEnum|string $name, $type): self
{
return new static($name, $type);
}
Expand Down Expand Up @@ -118,7 +119,7 @@ public function build(): array
{
return [
'args' => $this->args,
'name' => $this->name,
'name' => $this->name instanceof BackedEnum ? (string) $this->name->value : $this->name,

Check warning on line 122 in src/Builder/FieldBuilder.php

View workflow job for this annotation

GitHub Actions / Infection

Escaped Mutant for Mutator "CastString": @@ @@ /** @phpstan-return FieldDefinitionConfig */ public function build(): array { - return ['args' => $this->args, 'name' => $this->name instanceof BackedEnum ? (string) $this->name->value : $this->name, 'description' => $this->description, 'deprecationReason' => $this->deprecationReason, 'resolve' => $this->resolve, 'type' => $this->type]; + return ['args' => $this->args, 'name' => $this->name instanceof BackedEnum ? $this->name->value : $this->name, 'description' => $this->description, 'deprecationReason' => $this->deprecationReason, 'resolve' => $this->resolve, 'type' => $this->type]; } }
'description' => $this->description,
'deprecationReason' => $this->deprecationReason,
'resolve' => $this->resolve,
Expand Down
9 changes: 9 additions & 0 deletions tests/Builder/FieldBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use SimPod\GraphQLUtils\Builder\FieldBuilder;
use SimPod\GraphQLUtils\Tests\Builder\fixture\Fields;

final class FieldBuilderTest extends TestCase
{
Expand Down Expand Up @@ -46,4 +47,12 @@ public function testCreate(): void
self::assertSame('Reason', $args['arg1']['deprecationReason']);
self::assertSame(1, $args['arg1']['defaultValue']);
}

public function testCreateFromEnum(): void
{
$field = FieldBuilder::create(Fields::Field1, Type::string())
->build();

self::assertSame('Field1', $field['name']);
}
}
10 changes: 10 additions & 0 deletions tests/Builder/fixture/Fields.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace SimPod\GraphQLUtils\Tests\Builder\fixture;

enum Fields: string
{
case Field1 = 'Field1';
}

0 comments on commit 6fde9c1

Please sign in to comment.