Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,28 @@ private void CollectTypes(Context context, Selection selection, TypeContainer pa
return switchExpression;
}

return BuildSelectionSetExpression(context, parent.Nodes[0]);
var singleTypeNode = parent.Nodes[0];

if (context.ParentType != singleTypeNode.Type)
{
var newParent = Expression.Convert(context.Parent, singleTypeNode.Type);
var newContext = context with { Parent = newParent, ParentType = singleTypeNode.Type };
var selectionSet = BuildSelectionSetExpression(newContext, singleTypeNode);

if (selectionSet is null)
{
return null;
}

var castedSelectionSet = Expression.Convert(selectionSet, context.ParentType);

return Expression.Condition(
Expression.TypeIs(context.Parent, singleTypeNode.Type),
castedSelectionSet,
Expression.Constant(null, context.ParentType));
}

return BuildSelectionSetExpression(context, singleTypeNode);
}

private static MemberInitExpression? BuildSelectionSetExpression(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,28 @@ public class QueryableProjectionUnionTypeTests
}
];

private static readonly InspectionDefinition[] s_inspectionDefinitions =
[
new()
{
Id = 1,
Trigger = new FieldDateTimeInspectionTrigger
{
Id = 11,
FieldModelKey = "field-1"
}
},
new()
{
Id = 2,
Trigger = new FieldDateTimeInspectionTrigger
{
Id = 12,
FieldModelKey = "field-2"
}
}
];

private readonly SchemaCache _cache = new SchemaCache();

[Fact]
Expand Down Expand Up @@ -214,6 +236,75 @@ await Snapshot
.MatchAsync();
}

[Fact]
public async Task Create_Union_Single_Property()
{
// arrange
var tester = _cache.CreateSchema(
s_inspectionDefinitions,
OnModelCreatingInspection,
configure: ConfigureInspectionSchema);

// act
var result = await tester.ExecuteAsync(
OperationRequestBuilder.New()
.SetDocument(
"""
{
root {
trigger {
... on FieldDateTimeInspectionTrigger {
fieldModelKey
}
}
}
}
""")
.Build());

// assert
await Snapshot
.Create()
.AddResult(result)
.MatchAsync();
}

[Fact]
public async Task Create_Union_Single_Property_Pagination()
{
// arrange
var tester = _cache.CreateSchema(
s_inspectionDefinitions,
OnModelCreatingInspection,
usePaging: true,
configure: ConfigureInspectionSchema);

// act
var result = await tester.ExecuteAsync(
OperationRequestBuilder.New()
.SetDocument(
"""
{
root {
nodes {
trigger {
... on FieldDateTimeInspectionTrigger {
fieldModelKey
}
}
}
}
}
""")
.Build());

// assert
await Snapshot
.Create()
.AddResult(result)
.MatchAsync();
}

private static void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<AbstractType>()
Expand All @@ -229,6 +320,21 @@ private static void ConfigureSchema(ISchemaBuilder schemaBuilder)
.AddType(new ObjectType<Bar>());
}

private static void OnModelCreatingInspection(ModelBuilder modelBuilder)
{
modelBuilder.Entity<InspectionDefinition>()
.HasOne(x => x.Trigger)
.WithOne()
.HasForeignKey<InspectionDefinition>(x => x.TriggerId);

modelBuilder.Entity<InspectionTrigger>()
.HasDiscriminator<string>("d")
.HasValue<FieldDateTimeInspectionTrigger>("fieldDateTime");
}

private static void ConfigureInspectionSchema(ISchemaBuilder schemaBuilder)
=> schemaBuilder.AddType(new ObjectType<FieldDateTimeInspectionTrigger>());

public class NestedList
{
public int Id { get; set; }
Expand Down Expand Up @@ -260,4 +366,24 @@ public class Bar : AbstractType
{
public string BarProp { get; set; } = null!;
}

public class InspectionDefinition
{
public int Id { get; set; }

public int TriggerId { get; set; }

public InspectionTrigger Trigger { get; set; } = null!;
}

[UnionType]
public abstract class InspectionTrigger
{
public int Id { get; set; }
}

public class FieldDateTimeInspectionTrigger : InspectionTrigger
{
public string FieldModelKey { get; set; } = null!;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Result:
---------------
{
"data": {
"root": [
{
"trigger": {
"fieldModelKey": "field-1"
}
},
{
"trigger": {
"fieldModelKey": "field-2"
}
}
]
}
}
---------------

SQL:
---------------
SELECT 1, 1, "i"."FieldModelKey"
FROM "Data" AS "d"
INNER JOIN "InspectionTrigger" AS "i" ON "d"."TriggerId" = "i"."Id"
---------------
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Result:
---------------
{
"data": {
"root": {
"nodes": [
{
"trigger": {
"fieldModelKey": "field-1"
}
},
{
"trigger": {
"fieldModelKey": "field-2"
}
}
]
}
}
}
---------------

SQL:
---------------
SELECT 1, 1, "i"."FieldModelKey"
FROM "Data" AS "d"
INNER JOIN "InspectionTrigger" AS "i" ON "d"."TriggerId" = "i"."Id"
---------------
Loading
Loading