-
Notifications
You must be signed in to change notification settings - Fork 126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug]: Cannot return null for non-nullable field. #1747
Comments
1 task
Aniruddh25
added a commit
that referenced
this issue
Jan 31, 2024
## 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: ![image](https://github.com/Azure/data-api-builder/assets/3513779/17602247-db9b-4b61-9e42-cccf527306b5) 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. ![image](https://github.com/Azure/data-api-builder/assets/3513779/eec87cc0-8341-44bd-98d9-3bcf7fd4bc40) ## 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 ![image](https://github.com/Azure/data-api-builder/assets/3513779/556cee8f-80d4-4fd3-b365-9046ec31326e) 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 <sean.leonard@microsoft.com>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What happened?
If, in a One-to-Many or Many-to-One relationship, there is no value for the "one" side of the relationship, an error is returned, instead than returning a null value. As a result, this make impossible to represent correctly One-to-One relationship. Take this example, using the "School Database" available here: https://learn.microsoft.com/en-us/ef/ef6/resources/school-database
There is a one-to-one relationship between
Course
andOnlineCourse
asOnlineCourse
is a "specialization" ofCourse
. Same goes betweenCourse
andOnsiteCourse
and This is the following DAB config:if I run the following GraphQL query:
I get the following error:
As for not all courses the field returned by the referenced entities will be available.
Version
Microsoft.DataApiBuilder 0.8.52+c69925060e1942d28515b9c4b89ea24832da0c7c
What database are you using?
Azure SQL
What hosting model are you using?
Local (including CLI)
Which API approach are you accessing DAB through?
GraphQL
Relevant log output
Code of Conduct
The text was updated successfully, but these errors were encountered: