@@ -181,7 +181,8 @@ impl<'a> Display for GraphqlDefinition<'a> {
181
181
"{}" ,
182
182
GraphqlData {
183
183
table: t,
184
- foreign_keys: & self . foreign_keys
184
+ foreign_keys: & self . foreign_keys,
185
+ table_data: self . tables
185
186
}
186
187
) ?;
187
188
}
@@ -216,6 +217,7 @@ impl<'a> Display for GraphqlDefinition<'a> {
216
217
struct GraphqlData < ' a > {
217
218
table : & ' a TableData ,
218
219
foreign_keys : & ' a [ ForeignKeyConstraint ] ,
220
+ table_data : & ' a [ TableData ] ,
219
221
}
220
222
221
223
fn uppercase_table_name ( name : & str ) -> String {
@@ -303,6 +305,9 @@ impl<'a> Display for GraphqlData<'a> {
303
305
. filter ( |f| f. parent_table == self . table . name )
304
306
{
305
307
writeln ! ( out, "#[diesel(default)]" ) ?;
308
+ if is_reverse_nullable_reference ( f, self . table_data ) {
309
+ writeln ! ( out, "#[wundergraph(is_nullable_reference = \" true\" )]" ) ?;
310
+ }
306
311
writeln ! (
307
312
out,
308
313
"{}: HasMany<{}>," ,
@@ -316,6 +321,20 @@ impl<'a> Display for GraphqlData<'a> {
316
321
}
317
322
}
318
323
324
+ fn is_reverse_nullable_reference ( fk : & ForeignKeyConstraint , tables : & [ TableData ] ) -> bool {
325
+ if let Some ( t) = tables. iter ( ) . find ( |t| {
326
+ t. name == fk. child_table && t. column_data . iter ( ) . any ( |c| c. sql_name == fk. foreign_key )
327
+ } ) {
328
+ if let Some ( c) = t. column_data . iter ( ) . find ( |c| c. sql_name == fk. foreign_key ) {
329
+ c. ty . is_nullable
330
+ } else {
331
+ false
332
+ }
333
+ } else {
334
+ false
335
+ }
336
+ }
337
+
319
338
struct GraphqlColumn < ' a > {
320
339
column : & ' a ColumnDefinition ,
321
340
foreign_key : Option < & ' a ForeignKeyConstraint > ,
@@ -337,7 +356,6 @@ impl<'a> Display for GraphqlColumn<'a> {
337
356
} else {
338
357
fix_table_name ( & foreign_key. parent_table . name )
339
358
} ;
340
-
341
359
write ! ( f, "{}: HasOne<{}, {}>," , name, tpe, referenced) ?;
342
360
} else {
343
361
write ! ( f, "{}: {}," , name, tpe) ?;
@@ -475,12 +493,7 @@ impl<'a> Display for GraphqlInsertable<'a> {
475
493
{
476
494
let mut out = PadAdapter :: new ( f) ;
477
495
writeln ! ( out) ?;
478
- for c in self
479
- . table
480
- . column_data
481
- . iter ( )
482
- . filter ( |c| !c. has_default )
483
- {
496
+ for c in self . table . column_data . iter ( ) . filter ( |c| !c. has_default ) {
484
497
let t = GraphqlType { sql_type : & c. ty } ;
485
498
let name = c. rust_name . as_ref ( ) . unwrap_or ( & c. sql_name ) ;
486
499
writeln ! ( out, "{}: {}," , name, t) ?;
0 commit comments