Skip to content

Commit 659e136

Browse files
committed
id() and uuid() columns now have GraphQL outputType ID by default
1 parent 6138919 commit 659e136

File tree

5 files changed

+51
-10
lines changed

5 files changed

+51
-10
lines changed

src/TdbmFluidColumnGraphqlOptions.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66

77
use function addslashes;
8+
use Doctrine\DBAL\Types\Type;
89
use function var_export;
910

1011
class TdbmFluidColumnGraphqlOptions
@@ -21,10 +22,25 @@ class TdbmFluidColumnGraphqlOptions
2122
* @var string
2223
*/
2324
private $outputType;
25+
/**
26+
* @var FluidColumn
27+
*/
28+
private $fluidColumn;
2429

25-
public function __construct(TdbmFluidColumnOptions $tdbmFluidColumnOptions)
30+
public function __construct(TdbmFluidColumnOptions $tdbmFluidColumnOptions, FluidColumn $fluidColumn)
2631
{
2732
$this->tdbmFluidColumnOptions = $tdbmFluidColumnOptions;
33+
$this->fluidColumn = $fluidColumn;
34+
if (!$this->getComment()->hasAnnotation('TheCodingMachine\\GraphQLite\\Annotations\\Field')) {
35+
$this->generateFieldAnnotation();
36+
}
37+
}
38+
39+
private function getComment(): Comment
40+
{
41+
$comment = $this->fluidColumn->getDbalColumn()->getComment();
42+
43+
return new Comment($comment ?? '');
2844
}
2945

3046
public function fieldName(string $name): self
@@ -43,9 +59,22 @@ public function outputType(string $outputType): self
4359

4460
private function generateFieldAnnotation(): void
4561
{
62+
$outputType = null;
63+
if ($this->outputType !== null) {
64+
$outputType = $this->outputType;
65+
} elseif ($this->fluidColumn->getDbalColumn()->getType() === Type::getType(Type::GUID)) {
66+
$outputType = 'ID';
67+
} else {
68+
// If the column is the primary key, let's add an ID type
69+
$pk = $this->tdbmFluidColumnOptions->then()->getDbalTable()->getPrimaryKey();
70+
if ($pk !== null && $pk->getColumns() === [$this->fluidColumn->getDbalColumn()->getName()]) {
71+
$outputType = 'ID';
72+
}
73+
}
74+
4675
$parameters = array_filter([
4776
'name' => $this->name,
48-
'outputType' => $this->outputType
77+
'outputType' => $outputType
4978
]);
5079
if (empty($parameters)) {
5180
$parameters = null;

src/TdbmFluidColumnOptions.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,8 @@ public function column(string $name): TdbmFluidColumn
104104

105105
public function graphql(): TdbmFluidColumnGraphqlOptions
106106
{
107-
if (!$this->getComment()->hasAnnotation('TheCodingMachine\\GraphQLite\\Annotations\\Field')) {
108-
$this->addAnnotation('TheCodingMachine\\GraphQLite\\Annotations\\Field');
109-
}
110107
$this->tdbmFluidTable->graphqlType();
111-
return new TdbmFluidColumnGraphqlOptions($this);
108+
return new TdbmFluidColumnGraphqlOptions($this, $this->fluidColumn);
112109
}
113110

114111
private function getComment(): Comment

src/TdbmFluidTable.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,21 @@ public function primaryKey(array $columnNames, ?string $indexName = null): TdbmF
6060
return $this;
6161
}
6262

63-
public function id(): TdbmFluidTable
63+
public function id(): TdbmFluidColumnOptions
6464
{
6565
$this->fluidTable->id();
66-
return $this;
66+
return $this->column('id')->integer();
6767
}
6868

69-
public function uuid(string $version = 'v4'): TdbmFluidTable
69+
public function uuid(string $version = 'v4'): TdbmFluidColumnOptions
7070
{
7171
if ($version !== 'v1' && $version !== 'v4') {
7272
throw new FluidSchemaException('UUID version must be one of "v1" or "v4"');
7373
}
7474
$this->fluidTable->uuid();
7575

7676
$this->column('uuid')->guid()->addAnnotation('UUID', $version);
77-
return $this;
77+
return $this->column('uuid')->guid();
7878
}
7979

8080
public function timestamps(): TdbmFluidTable

tests/DoctrineAnnotationDumperTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace TheCodingMachine\FluidSchema;
44

5+
use Exception;
56
use PHPUnit\Framework\TestCase;
67

78
class DoctrineAnnotationDumperTest extends TestCase
@@ -12,7 +13,13 @@ public function testExportValues()
1213
$this->assertSame('', DoctrineAnnotationDumper::exportValues(null));
1314
$this->assertSame('({})', DoctrineAnnotationDumper::exportValues([]));
1415
$this->assertSame('("foo")', DoctrineAnnotationDumper::exportValues("foo"));
16+
$this->assertSame('(foo = null)', DoctrineAnnotationDumper::exportValues(["foo"=>null]));
17+
$this->assertSame('(foo = 42)', DoctrineAnnotationDumper::exportValues(["foo"=>42]));
1518
$this->assertSame('(foo = "bar")', DoctrineAnnotationDumper::exportValues(["foo"=>"bar"]));
1619
$this->assertSame('(foo = {"bar":"baz", "baz":"bar"})', DoctrineAnnotationDumper::exportValues(["foo"=>["bar"=>"baz","baz"=>"bar"]]));
20+
$this->assertSame('(foo = {"baz", "bar"})', DoctrineAnnotationDumper::exportValues(["foo"=>["baz","bar"]]));
21+
22+
$this->expectException(\RuntimeException::class);
23+
DoctrineAnnotationDumper::exportValues(new Exception());
1724
}
1825
}

tests/TdbmFluidColumnGraphqlOptionsTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,13 @@ public function testGraphql()
4444
$this->assertSame($column2, $column);
4545

4646
$this->assertContains('@TheCodingMachine\GraphQLite\Annotations\Type', $schema->getTable('posts')->getOptions()['comment']);
47+
48+
$idColumn = $posts->id()->graphql();
49+
$this->assertContains('outputType = "ID"', $schema->getTable('posts')->getColumn('id')->getComment());
50+
51+
$users = $fluid->table('users');
52+
$uuidColumn = $users->uuid()->graphql();
53+
$this->assertContains('outputType = "ID"', $schema->getTable('users')->getColumn('uuid')->getComment());
54+
4755
}
4856
}

0 commit comments

Comments
 (0)