Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support creating field names from enums #169

Merged
merged 1 commit into from
Aug 11, 2024
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
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 @@
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 @@
*
* @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 All @@ -70,7 +71,7 @@
mixed $defaultValue = null,
string|null $deprecationReason = null,
): self {
if ($this->args === null) {

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

View workflow job for this annotation

GitHub Actions / Infection

Escaped Mutant for Mutator "Identical": @@ @@ */ public function addArgument(string $name, $type, string|null $description = null, mixed $defaultValue = null, string|null $deprecationReason = null): self { - if ($this->args === null) { + if ($this->args !== null) { $this->args = []; } $value = ['type' => $type];
$this->args = [];
}

Expand Down Expand Up @@ -118,11 +119,11 @@
{
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,
'type' => $this->type,

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

View workflow job for this annotation

GitHub Actions / Infection

Escaped Mutant for Mutator "ArrayItem": @@ @@ /** @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 ? (string) $this->name->value : $this->name, 'description' => $this->description, 'deprecationReason' => $this->deprecationReason, 'resolve' => $this->resolve, 'type' > $this->type]; } }
];
}
}
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';
}
Loading