Commit 5fe65f8
Make relationship fields in referenced entities nullable (#1958)
## Why make this change?
- Closes #1747
- Records in referenced entities may not always be related to the
referencing entity. E.g. in a `book-websiteplacement` relationship, not
all `books` (referenced entity) may have a `websiteplacement`
(referencing) yet. This change is to ensure we get all records of the
referenced entity including the ones that DON'T have any relationships.
## What is this change?
- Previously, we used to rely on the nullability of the referenced
fields to determine whether the relationship field in the referenced
entity should be nullable or not. While this is applicable when we are
considering the relationship fields of a referencing entity, it is
actually restricting when used for a referenced entity.
- For example, the referencing entity (BookWebsitePlacement) should have
a nullable `books` (relationship field) based on whether the foreign key
`book_id` in `book_website_placements` is nullable or not -> indicating
whether a website placement MUST have a book or not.
- On the other hand, the referenced entity `books` should always have a
NULLABLE `websiteplacement` relationship field in order to include those
books that don't have any `websiteplacement` published yet. Relying on
the nullability of the `id` - the referenced field in the
book->book_website_placement foreign key would make the relationship
NON-NULLABLE but this restricts inclusion of those books that don't have
any website placements hence the `error: "Cannot return null for
non-nullable field"`. We need to make the relationship field nullable in
such cases.
- The source entity could be both the referencing and referenced entity
in case of missing foreign keys in the db or self referencing
relationships.
- Use the nullability of referencing columns to determine the
nullability of the relationship field only if
1. there is exactly one relationship where source is the referencing
entity.
DAB doesn't support multiple relationships at the moment.
and
2. when the source is not a referenced entity in any of the
relationships.
- Identifies the scenario where the relationship field is from the
referenced entity and always sets its nullability to `true`.
## How was this tested?
- [X] Integration Tests - existing data already had this bug, needed to
modify the query to expose it.
- Ran the query in #1747 to verify this fix solved that issue.
## Sample Request(s)
```GraphQL
{
books {
items {
id
title
websiteplacement {
price
}
}
}
}
```
BEFORE:

AFTER FIX:
There are no more errors and note that we now actually return `null` as
the relationship field value(here, `websiteplacement`) when querying the
referenced entity (here, `book`) which doesn't have any relationship
with the referencing entity.

## NOTE
The issue is only exposed in a 1:1 relationship. This is because the
only other scenario for querying a referenced entity is a 1:many
relationship. And for records in the referenced entity(e.g. publisher)
which are NOT related to the referencing entity(e.g. book), we
explicitly check for nulls and return an empty array. See here:
https://github.com/Azure/data-api-builder/blob/dacf3bccbe0a36da51776bf4afd1b4a0d4348ff4/src/Core/Resolvers/SqlQueryEngine.cs#L123
E.g.
```GraphQL Query for 1:many relationship
{ publishers {
items {
id
name
books {
items {
title
}
}
}
}
}
```
Expected Response

NOTE:
When DAB supports multiple relationships, nullability of each
relationship field should be determined based on foreign keys associated
with each relationship.
---------
Co-authored-by: Sean Leonard <[email protected]>1 parent e681aa5 commit 5fe65f8
File tree
7 files changed
+160
-190
lines changed- src
- Service.GraphQLBuilder/Sql
- Service.Tests
- SqlTests/GraphQLQueryTests
7 files changed
+160
-190
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
121 | 121 | | |
122 | 122 | | |
123 | 123 | | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | | - | |
140 | | - | |
141 | | - | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
147 | | - | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
158 | | - | |
159 | | - | |
| 124 | + | |
160 | 125 | | |
161 | 126 | | |
162 | 127 | | |
| |||
276 | 241 | | |
277 | 242 | | |
278 | 243 | | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
279 | 325 | | |
280 | 326 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
| |||
67 | 68 | | |
68 | 69 | | |
69 | 70 | | |
| 71 | + | |
70 | 72 | | |
71 | 73 | | |
72 | 74 | | |
| |||
Lines changed: 25 additions & 39 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
68 | 68 | | |
69 | 69 | | |
70 | 70 | | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
109 | 95 | | |
110 | | - | |
| 96 | + | |
111 | 97 | | |
112 | 98 | | |
113 | 99 | | |
| |||
Lines changed: 7 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
556 | 556 | | |
557 | 557 | | |
558 | 558 | | |
559 | | - | |
| 559 | + | |
560 | 560 | | |
561 | | - | |
562 | | - | |
563 | | - | |
| 561 | + | |
| 562 | + | |
564 | 563 | | |
| 564 | + | |
| 565 | + | |
565 | 566 | | |
566 | | - | |
567 | | - | |
568 | | - | |
569 | 567 | | |
570 | 568 | | |
| 569 | + | |
571 | 570 | | |
572 | 571 | | |
573 | 572 | | |
574 | 573 | | |
575 | 574 | | |
576 | | - | |
| 575 | + | |
577 | 576 | | |
578 | 577 | | |
579 | 578 | | |
| |||
Lines changed: 17 additions & 37 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
83 | 83 | | |
84 | 84 | | |
85 | 85 | | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | | - | |
119 | | - | |
120 | | - | |
121 | | - | |
122 | | - | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
123 | 103 | | |
124 | 104 | | |
125 | 105 | | |
| |||
Lines changed: 11 additions & 19 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
107 | 107 | | |
108 | 108 | | |
109 | 109 | | |
110 | | - | |
111 | | - | |
| 110 | + | |
| 111 | + | |
112 | 112 | | |
113 | 113 | | |
| 114 | + | |
114 | 115 | | |
115 | 116 | | |
116 | | - | |
117 | | - | |
118 | | - | |
119 | | - | |
120 | | - | |
| 117 | + | |
| 118 | + | |
121 | 119 | | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | | - | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
134 | 126 | | |
135 | 127 | | |
136 | 128 | | |
| |||
0 commit comments