Skip to content

Commit 9ffc8ed

Browse files
committed
Detect reverse nullable references in print-schema
1 parent b7b3670 commit 9ffc8ed

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

wundergraph_cli/src/print_schema/print_helper.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,8 @@ impl<'a> Display for GraphqlDefinition<'a> {
181181
"{}",
182182
GraphqlData {
183183
table: t,
184-
foreign_keys: &self.foreign_keys
184+
foreign_keys: &self.foreign_keys,
185+
table_data: self.tables
185186
}
186187
)?;
187188
}
@@ -216,6 +217,7 @@ impl<'a> Display for GraphqlDefinition<'a> {
216217
struct GraphqlData<'a> {
217218
table: &'a TableData,
218219
foreign_keys: &'a [ForeignKeyConstraint],
220+
table_data: &'a [TableData],
219221
}
220222

221223
fn uppercase_table_name(name: &str) -> String {
@@ -303,6 +305,9 @@ impl<'a> Display for GraphqlData<'a> {
303305
.filter(|f| f.parent_table == self.table.name)
304306
{
305307
writeln!(out, "#[diesel(default)]")?;
308+
if is_reverse_nullable_reference(f, self.table_data) {
309+
writeln!(out, "#[wundergraph(is_nullable_reference = \"true\")]")?;
310+
}
306311
writeln!(
307312
out,
308313
"{}: HasMany<{}>,",
@@ -316,6 +321,20 @@ impl<'a> Display for GraphqlData<'a> {
316321
}
317322
}
318323

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+
319338
struct GraphqlColumn<'a> {
320339
column: &'a ColumnDefinition,
321340
foreign_key: Option<&'a ForeignKeyConstraint>,
@@ -337,7 +356,6 @@ impl<'a> Display for GraphqlColumn<'a> {
337356
} else {
338357
fix_table_name(&foreign_key.parent_table.name)
339358
};
340-
341359
write!(f, "{}: HasOne<{}, {}>,", name, tpe, referenced)?;
342360
} else {
343361
write!(f, "{}: {},", name, tpe)?;
@@ -475,12 +493,7 @@ impl<'a> Display for GraphqlInsertable<'a> {
475493
{
476494
let mut out = PadAdapter::new(f);
477495
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) {
484497
let t = GraphqlType { sql_type: &c.ty };
485498
let name = c.rust_name.as_ref().unwrap_or(&c.sql_name);
486499
writeln!(out, "{}: {},", name, t)?;

0 commit comments

Comments
 (0)