Skip to content

Commit 93e8232

Browse files
ritikadhawanCopilotaaronburtle
authored
fix: don't enforce many-to-one/one-to-many relationships for DWSQL (#3770)
## What's the problem? When Data API builder serves a Fabric Warehouse (DWSQL) database, a GraphQL query that follows a relationship between two tables could fail with this error: ``` "Cannot return null for non-nullable field." (HC0018) ``` ...even when the query was perfectly valid and the data was fine. Instead of returning the row, the whole query blew up. ## Why does it happen? Most databases guarantee that if a row points to a "parent" row (a foreign key), that parent actually exists. DAB relies on that guarantee: if a foreign-key column is marked "required" (NOT NULL), DAB assumes the related object is always there, and tells GraphQL "this related field will never be null." **Fabric Warehouse is different — it does not enforce foreign keys.** So a child row can happily point at a parent that doesn't exist (an "orphaned" row). When GraphQL asks for that missing parent, DAB has nothing to return, but it already promised the field would never be null. GraphQL sees a broken promise and throws HC0018. Example: an `Enrollment` row has a `studentId` that no `Student` matches. Asking for `enrollment.student` returns nothing, and the query fails. ## What does this change do? For Fabric Warehouse (DWSQL) only, DAB no longer assumes related rows always exist. It marks the related fields on many-to-one and one-to-many relationships as nullable. That way, when a related row is genuinely missing, the query simply returns `null` for that field instead of failing the entire request. Behavior for all other databases (SQL Server, PostgreSQL, MySQL) is unchanged, since they do enforce foreign keys. ## How was it verified? - Added unit tests covering both DWSQL (field is now nullable) and SQL Server (field stays non-nullable) for both relationship directions. - Manually validated end-to-end against a Fabric Warehouse: the query that previously failed now returns the row with `student: null`. #### Before <img width="854" height="730" alt="Screenshot 2026-08-12 143540" src="https://github.com/user-attachments/assets/9b4585e5-5cb8-456f-8efb-0eda22f68994" /> #### After <img width="1053" height="514" alt="Screenshot 2026-08-12 141456" src="https://github.com/user-attachments/assets/cbb42b63-c867-4804-bc10-6d7a4c1c643b" /> --------- Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Co-authored-by: aaronburtle <93220300+aaronburtle@users.noreply.github.com>
1 parent a795a1a commit 93e8232

3 files changed

Lines changed: 41 additions & 11 deletions

File tree

‎src/Core/Services/GraphQLSchemaCreator.cs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,8 @@ private DocumentNode GenerateSqlGraphQLObjects(RuntimeEntities entities, Diction
318318
configEntity: entity,
319319
entities: entities,
320320
rolesAllowedForEntity: rolesAllowedForEntity,
321-
rolesAllowedForFields: rolesAllowedForFields);
321+
rolesAllowedForFields: rolesAllowedForFields,
322+
databaseType: sqlMetadataProvider.GetDatabaseType());
322323

323324
if (databaseObject.SourceType is not EntitySourceType.StoredProcedure)
324325
{
@@ -560,7 +561,8 @@ private Dictionary<string, ObjectTypeDefinitionNode> GenerateObjectDefinitionsFo
560561
configEntity: linkingEntity,
561562
entities: new(new Dictionary<string, Entity>()),
562563
rolesAllowedForEntity: new List<string>(),
563-
rolesAllowedForFields: new Dictionary<string, IEnumerable<string>>()
564+
rolesAllowedForFields: new Dictionary<string, IEnumerable<string>>(),
565+
databaseType: sqlMetadataProvider.GetDatabaseType()
564566
);
565567

566568
linkingObjectTypes.Add(linkingEntityName, node);

‎src/Service.GraphQLBuilder/Sql/SchemaConverter.cs‎

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ public static ObjectTypeDefinitionNode GenerateObjectTypeDefinitionForDatabaseOb
4848
Entity configEntity,
4949
RuntimeEntities entities,
5050
IEnumerable<string> rolesAllowedForEntity,
51-
IDictionary<string, IEnumerable<string>> rolesAllowedForFields)
51+
IDictionary<string, IEnumerable<string>> rolesAllowedForFields,
52+
DatabaseType databaseType = DatabaseType.MSSQL)
5253
{
5354
ObjectTypeDefinitionNode objectDefinitionNode;
5455
switch (databaseObject.SourceType)
@@ -69,7 +70,8 @@ public static ObjectTypeDefinitionNode GenerateObjectTypeDefinitionForDatabaseOb
6970
configEntity: configEntity,
7071
entities: entities,
7172
rolesAllowedForEntity: rolesAllowedForEntity,
72-
rolesAllowedForFields: rolesAllowedForFields);
73+
rolesAllowedForFields: rolesAllowedForFields,
74+
databaseType: databaseType);
7375
break;
7476
default:
7577
throw new DataApiBuilderException(
@@ -170,7 +172,8 @@ private static ObjectTypeDefinitionNode CreateObjectTypeDefinitionForTableOrView
170172
Entity configEntity,
171173
RuntimeEntities entities,
172174
IEnumerable<string> rolesAllowedForEntity,
173-
IDictionary<string, IEnumerable<string>> rolesAllowedForFields)
175+
IDictionary<string, IEnumerable<string>> rolesAllowedForFields,
176+
DatabaseType databaseType = DatabaseType.MSSQL)
174177
{
175178
Dictionary<string, FieldDefinitionNode> fieldDefinitionNodes = new();
176179
SourceDefinition sourceDefinition = databaseObject.SourceDefinition;
@@ -225,7 +228,8 @@ private static ObjectTypeDefinitionNode CreateObjectTypeDefinitionForTableOrView
225228
databaseObject,
226229
entities,
227230
relationshipName,
228-
relationship);
231+
relationship,
232+
databaseType);
229233
fieldDefinitionNodes.Add(relationshipField.Name.Value, relationshipField);
230234
}
231235
}
@@ -474,13 +478,14 @@ private static FieldDefinitionNode GenerateFieldForRelationship(
474478
DatabaseObject databaseObject,
475479
RuntimeEntities entities,
476480
string relationshipName,
477-
EntityRelationship relationship)
481+
EntityRelationship relationship,
482+
DatabaseType databaseType = DatabaseType.MSSQL)
478483
{
479484
// Generate the field that represents the relationship to ObjectType, so you can navigate through it
480485
// and walk the graph.
481486
string targetEntityName = relationship.TargetEntity.Split('.').Last();
482487
Entity referencedEntity = entities[targetEntityName];
483-
bool isNullableRelationship = FindNullabilityOfRelationship(entityName, databaseObject, targetEntityName);
488+
bool isNullableRelationship = FindNullabilityOfRelationship(entityName, databaseObject, targetEntityName, databaseType);
484489

485490
INullableTypeNode targetField = relationship.Cardinality switch
486491
{
@@ -631,8 +636,15 @@ public static IValueNode CreateValueNodeFromDbObjectMetadata(object metadataValu
631636
private static bool FindNullabilityOfRelationship(
632637
string entityName,
633638
DatabaseObject databaseObject,
634-
string targetEntityName)
639+
string targetEntityName,
640+
DatabaseType databaseType = DatabaseType.MSSQL)
635641
{
642+
// DWSQL does not enforce foreign key constraints, so relationship fields are always nullable.
643+
if (databaseType == DatabaseType.DWSQL)
644+
{
645+
return true;
646+
}
647+
636648
bool isNullableRelationship = false;
637649
SourceDefinition sourceDefinition = databaseObject.SourceDefinition;
638650
if (// Retrieve all the relationship information for the source entity which is backed by this table definition

‎src/Service.Tests/GraphQLBuilder/Sql/SchemaConverterTests.cs‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,21 @@ public void ForeignKeyFieldHasCorrectNullability(bool isNullable)
361361
Assert.AreEqual(expected: isNullable, actual: field.Type is INullableTypeNode);
362362
}
363363

364+
// A NOT NULL foreign-key column yields a non-nullable relationship field for MSSQL, but a
365+
// nullable one for DWSQL since DWSQL does not enforce foreign key constraints.
366+
[DataRow(DatabaseType.DWSQL, Cardinality.One, true, DisplayName = "Many-to-one relationship field is nullable for DWSQL despite NOT NULL FK.")]
367+
[DataRow(DatabaseType.DWSQL, Cardinality.Many, true, DisplayName = "Cardinality.Many relationship field is nullable for DWSQL despite NOT NULL FK metadata.")]
368+
[DataRow(DatabaseType.MSSQL, Cardinality.One, false, DisplayName = "Many-to-one relationship field is non-nullable for MSSQL with NOT NULL FK.")]
369+
[DataRow(DatabaseType.MSSQL, Cardinality.Many, false, DisplayName = "Cardinality.Many relationship field is non-nullable for MSSQL with NOT NULL FK metadata.")]
370+
[TestMethod]
371+
public void RelationshipFieldIsNullableForDwSqlDespiteNonNullableForeignKey(DatabaseType databaseType, Cardinality cardinality, bool expectedNullable)
372+
{
373+
ObjectTypeDefinitionNode od =
374+
GenerateObjectWithRelationship(cardinality, isNullableRelationship: false, databaseType: databaseType);
375+
FieldDefinitionNode field = od.Fields.First(f => f.Name.Value == FIELD_NAME_FOR_TARGET);
376+
Assert.AreEqual(expectedNullable, field.Type is INullableTypeNode);
377+
}
378+
364379
[TestMethod]
365380
public void WhenForeignKeyDefinedButNoRelationship_GraphQLWontModelIt()
366381
{
@@ -756,7 +771,7 @@ public static Entity GenerateEmptyEntity(string entityName)
756771
);
757772
}
758773

759-
private static ObjectTypeDefinitionNode GenerateObjectWithRelationship(Cardinality cardinality, bool isNullableRelationship = false)
774+
private static ObjectTypeDefinitionNode GenerateObjectWithRelationship(Cardinality cardinality, bool isNullableRelationship = false, DatabaseType databaseType = DatabaseType.MSSQL)
760775
{
761776
SourceDefinition table = GenerateTableWithForeignKeyDefinition(isNullableRelationship);
762777

@@ -786,7 +801,8 @@ private static ObjectTypeDefinitionNode GenerateObjectWithRelationship(Cardinali
786801
dbObject,
787802
configEntity, new(new Dictionary<string, Entity>() { { TARGET_ENTITY, relationshipEntity } }),
788803
rolesAllowedForEntity: GetRolesAllowedForEntity(),
789-
rolesAllowedForFields: GetFieldToRolesMap()
804+
rolesAllowedForFields: GetFieldToRolesMap(),
805+
databaseType: databaseType
790806
);
791807
}
792808

0 commit comments

Comments
 (0)