Skip to content

Commit ded5356

Browse files
committed
Add a graphql() method in junctionTable options
1 parent 448dfee commit ded5356

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-3
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,6 @@ $posts = $db->table('posts')->string('title')->graphql() // The column is a Grap
4545
->right('CAN_EDIT') // The user must have the 'CAN_EDIT' right to view the field
4646
->failWith(null) // If the user is not logged or has no right, let's serve 'null'
4747
->endGraphql();
48+
49+
$db->junctionTable('posts', 'users')->graphql(); // Expose the many-to-many relationship as a GraphQL field.
4850
```
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
4+
namespace TheCodingMachine\FluidSchema;
5+
6+
7+
use function addslashes;
8+
use function var_export;
9+
10+
class TdbmFluidJunctionTableOptions
11+
{
12+
13+
/**
14+
* @var TdbmFluidTable
15+
*/
16+
private $tdbmFluidTable;
17+
18+
public function __construct(TdbmFluidTable $tdbmFluidTable)
19+
{
20+
$this->tdbmFluidTable = $tdbmFluidTable;
21+
}
22+
23+
public function graphql(): void
24+
{
25+
$this->tdbmFluidTable->addAnnotation('TheCodingMachine\\GraphQLite\\Annotations\\Field');
26+
}
27+
}

src/TdbmFluidSchema.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,14 @@ public function table(string $name): TdbmFluidTable
5151
*
5252
* @param string $table1
5353
* @param string $table2
54-
* @return FluidSchema
54+
* @return TdbmFluidJunctionTableOptions
5555
*/
56-
public function junctionTable(string $table1, string $table2): TdbmFluidSchema
56+
public function junctionTable(string $table1, string $table2): TdbmFluidJunctionTableOptions
5757
{
5858
$this->fluidSchema->junctionTable($table1, $table2);
59+
$tableName = $this->namingStrategy->getJointureTableName($table1, $table2);
5960

60-
return $this;
61+
return new TdbmFluidJunctionTableOptions($this->table($tableName));
6162
}
6263

6364
/**
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace TheCodingMachine\FluidSchema;
4+
5+
use Doctrine\DBAL\Schema\Schema;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class TdbmFluidJunctionTableOptionsTest extends TestCase
9+
{
10+
11+
public function testGraphql()
12+
{
13+
$schema = new Schema();
14+
$fluid = new TdbmFluidSchema($schema);
15+
16+
$fluid->table('posts')->uuid();
17+
$fluid->table('users')->uuid();
18+
19+
$fluid->junctionTable('posts', 'users')->graphql();
20+
21+
$this->assertSame("\n@TheCodingMachine\\GraphQLite\\Annotations\\Field", $schema->getTable('posts_users')->getOptions()['comment']);
22+
}
23+
}

0 commit comments

Comments
 (0)