55
66
77use function addslashes ;
8+ use Doctrine \DBAL \Types \Type ;
89use function var_export ;
910
1011class 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 ;
0 commit comments