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 @@ -102,7 +102,7 @@ public override bool TryHandleLeave(
var nullabilityInfo = context.NullabilityInfoContext.Create(propertyInfo);
var memberInit = queryableScope.CreateMemberInit();

if (context.InMemory && nullabilityInfo.ReadState == NullabilityState.Nullable)
if (context.InMemory && nullabilityInfo.ReadState != NullabilityState.NotNull)
{
parentScope.Level
.Peek()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Reflection;
using HotChocolate.Execution;
using HotChocolate.Types;
using Microsoft.EntityFrameworkCore;
using static CookieCrumble.TestEnvironment;

Expand Down Expand Up @@ -100,6 +102,12 @@ public class QueryableProjectionFilterTests
new()
];

private static readonly ParentWithObliviousNavigation[] s_parentsWithObliviousNavigation =
[
new() { Child = new OptionalChild() },
new()
];

private readonly SchemaCache _cache = new();

[Fact]
Expand Down Expand Up @@ -362,13 +370,75 @@ await Snapshot
.MatchAsync(TestContext.Current.CancellationToken);
}

[Fact]
public async Task Projection_Should_Preserve_Null_When_Optional_Navigation_Nullability_Is_Unknown()
{
// arrange
var childProperty = typeof(ParentWithObliviousNavigation)
.GetProperty(nameof(ParentWithObliviousNavigation.Child))!;
var nullabilityInfo = new NullabilityInfoContext().Create(childProperty);
Assert.Equal(NullabilityState.Unknown, nullabilityInfo.ReadState);

var tester = _cache.CreateSchema(
s_parentsWithObliviousNavigation,
ConfigureObliviousOptionalNavigation,
objectType: new ObjectType<ParentWithObliviousNavigation>(
descriptor => descriptor
.Field(x => x.Child)
.Type<ObjectType<OptionalChild>>()));

// act
var result = await tester.ExecuteAsync(
OperationRequestBuilder.New()
.SetDocument(
"""
{
root {
child {
id
}
}
}
""")
.Build(),
TestContext.Current.CancellationToken);

// assert
result.MatchInlineSnapshot(
"""
{
"data": {
"root": [
{
"child": {
"id": 1
}
},
{
"child": null
}
]
}
}
""");
}

private static void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Foo>().HasMany(x => x.ObjectArray);
modelBuilder.Entity<Foo>().HasOne(x => x.NestedObject);
modelBuilder.Entity<Bar>().HasOne(x => x.Foo);
}

private static void ConfigureObliviousOptionalNavigation(ModelBuilder modelBuilder)
{
modelBuilder
.Entity<ParentWithObliviousNavigation>()
.HasOne(x => x.Child)
.WithMany()
.IsRequired(false);
}

public class Foo
{
public int Id { get; set; }
Expand Down Expand Up @@ -443,6 +513,22 @@ public class BarNullable
public FooNullable? Foo { get; set; }
}

#nullable disable

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

public OptionalChild Child { get; set; }
}

#nullable restore

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

public enum BarEnum
{
FOO,
Expand Down
Loading