Skip to content
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

Avoid checking for null on non-nullable property for projection #6706

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public override bool CanHandle(ISelection selection) =>
selection.Field.Member is { } &&
selection.SelectionSet is not null;

private readonly NullabilityInfoContext _nullabilityInfoContext = new();

public override bool TryHandleEnter(
QueryableProjectionContext context,
ISelection selection,
Expand Down Expand Up @@ -89,7 +91,10 @@ public override bool TryHandleLeave(
return true;
}

if (context.InMemory)
// TODO: Use existing utility function/extension method? Cache?
var nullabilityInfo = _nullabilityInfoContext.Create(propertyInfo);

if (context.InMemory && nullabilityInfo.ReadState == NullabilityState.Nullable)
{
parentScope.Level
.Peek()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using CookieCrumble;
using HotChocolate.Execution;
using Microsoft.EntityFrameworkCore;

namespace HotChocolate.Data.Projections;

public class QueryableProjectionComplexTypeTests
{
private static readonly Foo[] _fooEntities =
[
new() { Bar = new Bar { Baz = "testatest", } },
new() { Bar = new Bar { Baz = "testbtest", } },
];

private readonly SchemaCache _cache = new SchemaCache();

[Fact]
public async Task Create_Complex_Type()
{
// arrange
var tester = _cache.CreateSchema(_fooEntities, OnModelCreating);

// act
var res1 = await tester.ExecuteAsync(
OperationRequestBuilder.New()
.SetDocument(
@"
{
root {
bar {
baz
}
}
}")
.Build());

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

private static void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Foo>().ComplexProperty(f => f.Bar);
}

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

public Bar Bar { get; set; } = null!;
}

public record Bar
{
public string Baz { get; set; } = null!;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Result:
---------------
{
"data": {
"root": [
{
"bar": {
"baz": "testatest"
}
},
{
"bar": {
"baz": "testbtest"
}
}
]
}
}
---------------

SQL:
---------------
SELECT "d"."Bar_Baz" AS "Baz"
FROM "Data" AS "d"
---------------
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ SQL:
---------------
.param set @__p_0 'a'

SELECT 1, "d"."Id", "f"."Id", "t"."c", "t"."BarString", "t"."BarShort", "t"."Id", "t"."Id0"
SELECT "d"."Id", "f"."Id", "t"."BarString", "t"."BarShort", "t"."Id", "t"."Id0"
FROM "Data" AS "d"
INNER JOIN "Foo" AS "f" ON "d"."FooId" = "f"."Id"
LEFT JOIN (
SELECT 1 AS "c", "f0"."BarString", "f0"."BarShort", "b"."Id", "f0"."Id" AS "Id0", "b"."FooId"
SELECT "f0"."BarString", "f0"."BarShort", "b"."Id", "f0"."Id" AS "Id0", "b"."FooId"
FROM "BarDeep" AS "b"
INNER JOIN "FooDeep" AS "f0" ON "b"."FooId1" = "f0"."Id"
WHERE "f0"."BarString" = @__p_0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ SQL:
---------------
.param set @__p_0 'a'

SELECT 1, "d"."Id", "f"."Id", "s"."c", "s"."BarString", "s"."BarShort", "s"."Id", "s"."Id0"
SELECT "d"."Id", "f"."Id", "s"."BarString", "s"."BarShort", "s"."Id", "s"."Id0"
FROM "Data" AS "d"
INNER JOIN "Foo" AS "f" ON "d"."FooId" = "f"."Id"
LEFT JOIN (
SELECT 1 AS "c", "f0"."BarString", "f0"."BarShort", "b"."Id", "f0"."Id" AS "Id0", "b"."FooId"
SELECT "f0"."BarString", "f0"."BarShort", "b"."Id", "f0"."Id" AS "Id0", "b"."FooId"
FROM "BarDeep" AS "b"
INNER JOIN "FooDeep" AS "f0" ON "b"."FooId1" = "f0"."Id"
WHERE "f0"."BarString" = @__p_0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ SQL:
---------------
.param set @__p_0 'a'

SELECT 1, "f"."BarString", "d"."Id", "f"."Id", "t"."c", "t"."BarString", "t"."BarShort", "t"."Id", "t"."Id0"
SELECT "f"."BarString", "d"."Id", "f"."Id", "t"."BarString", "t"."BarShort", "t"."Id", "t"."Id0"
FROM "Data" AS "d"
INNER JOIN "Foo" AS "f" ON "d"."FooId" = "f"."Id"
LEFT JOIN (
SELECT 1 AS "c", "f0"."BarString", "f0"."BarShort", "b"."Id", "f0"."Id" AS "Id0", "b"."FooId"
SELECT "f0"."BarString", "f0"."BarShort", "b"."Id", "f0"."Id" AS "Id0", "b"."FooId"
FROM "BarDeep" AS "b"
INNER JOIN "FooDeep" AS "f0" ON "b"."FooId1" = "f0"."Id"
WHERE "f0"."BarString" = @__p_0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ SQL:
---------------
.param set @__p_0 'a'

SELECT 1, "f"."BarString", "d"."Id", "f"."Id", "s"."c", "s"."BarString", "s"."BarShort", "s"."Id", "s"."Id0"
SELECT "f"."BarString", "d"."Id", "f"."Id", "s"."BarString", "s"."BarShort", "s"."Id", "s"."Id0"
FROM "Data" AS "d"
INNER JOIN "Foo" AS "f" ON "d"."FooId" = "f"."Id"
LEFT JOIN (
SELECT 1 AS "c", "f0"."BarString", "f0"."BarShort", "b"."Id", "f0"."Id" AS "Id0", "b"."FooId"
SELECT "f0"."BarString", "f0"."BarShort", "b"."Id", "f0"."Id" AS "Id0", "b"."FooId"
FROM "BarDeep" AS "b"
INNER JOIN "FooDeep" AS "f0" ON "b"."FooId1" = "f0"."Id"
WHERE "f0"."BarString" = @__p_0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ Result:

SQL:
---------------
SELECT 1, "d"."Id", "f"."Id", "t"."c", "t"."BarString", "t"."BarShort", "t"."Id", "t"."Id0"
SELECT "d"."Id", "f"."Id", "t"."BarString", "t"."BarShort", "t"."Id", "t"."Id0"
FROM "Data" AS "d"
INNER JOIN "Foo" AS "f" ON "d"."FooId" = "f"."Id"
LEFT JOIN (
SELECT 1 AS "c", "f0"."BarString", "f0"."BarShort", "b"."Id", "f0"."Id" AS "Id0", "b"."FooId"
SELECT "f0"."BarString", "f0"."BarShort", "b"."Id", "f0"."Id" AS "Id0", "b"."FooId"
FROM "BarDeep" AS "b"
INNER JOIN "FooDeep" AS "f0" ON "b"."FooId1" = "f0"."Id"
) AS "t" ON "f"."Id" = "t"."FooId"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ Result:

SQL:
---------------
SELECT 1, "d"."Id", "f"."Id", "s"."c", "s"."BarString", "s"."BarShort", "s"."Id", "s"."Id0"
SELECT "d"."Id", "f"."Id", "s"."BarString", "s"."BarShort", "s"."Id", "s"."Id0"
FROM "Data" AS "d"
INNER JOIN "Foo" AS "f" ON "d"."FooId" = "f"."Id"
LEFT JOIN (
SELECT 1 AS "c", "f0"."BarString", "f0"."BarShort", "b"."Id", "f0"."Id" AS "Id0", "b"."FooId"
SELECT "f0"."BarString", "f0"."BarShort", "b"."Id", "f0"."Id" AS "Id0", "b"."FooId"
FROM "BarDeep" AS "b"
INNER JOIN "FooDeep" AS "f0" ON "b"."FooId1" = "f0"."Id"
) AS "s" ON "f"."Id" = "s"."FooId"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ Result:

SQL:
---------------
SELECT 1, "f"."BarString", "d"."Id", "f"."Id", "t"."c", "t"."BarString", "t"."BarShort", "t"."Id", "t"."Id0"
SELECT "f"."BarString", "d"."Id", "f"."Id", "t"."BarString", "t"."BarShort", "t"."Id", "t"."Id0"
FROM "Data" AS "d"
INNER JOIN "Foo" AS "f" ON "d"."FooId" = "f"."Id"
LEFT JOIN (
SELECT 1 AS "c", "f0"."BarString", "f0"."BarShort", "b"."Id", "f0"."Id" AS "Id0", "b"."FooId"
SELECT "f0"."BarString", "f0"."BarShort", "b"."Id", "f0"."Id" AS "Id0", "b"."FooId"
FROM "BarDeep" AS "b"
INNER JOIN "FooDeep" AS "f0" ON "b"."FooId1" = "f0"."Id"
) AS "t" ON "f"."Id" = "t"."FooId"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ Result:

SQL:
---------------
SELECT 1, "f"."BarString", "d"."Id", "f"."Id", "s"."c", "s"."BarString", "s"."BarShort", "s"."Id", "s"."Id0"
SELECT "f"."BarString", "d"."Id", "f"."Id", "s"."BarString", "s"."BarShort", "s"."Id", "s"."Id0"
FROM "Data" AS "d"
INNER JOIN "Foo" AS "f" ON "d"."FooId" = "f"."Id"
LEFT JOIN (
SELECT 1 AS "c", "f0"."BarString", "f0"."BarShort", "b"."Id", "f0"."Id" AS "Id0", "b"."FooId"
SELECT "f0"."BarString", "f0"."BarShort", "b"."Id", "f0"."Id" AS "Id0", "b"."FooId"
FROM "BarDeep" AS "b"
INNER JOIN "FooDeep" AS "f0" ON "b"."FooId1" = "f0"."Id"
) AS "s" ON "f"."Id" = "s"."FooId"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ Result:

SQL:
---------------
SELECT 1, "d"."Id", "f"."Id", "t"."c", "t"."BarString", "t"."BarShort", "t"."Id", "t"."Id0"
SELECT "d"."Id", "f"."Id", "t"."BarString", "t"."BarShort", "t"."Id", "t"."Id0"
FROM "Data" AS "d"
INNER JOIN "Foo" AS "f" ON "d"."FooId" = "f"."Id"
LEFT JOIN (
SELECT 1 AS "c", "f0"."BarString", "f0"."BarShort", "b"."Id", "f0"."Id" AS "Id0", "b"."FooId"
SELECT "f0"."BarString", "f0"."BarShort", "b"."Id", "f0"."Id" AS "Id0", "b"."FooId"
FROM "BarDeep" AS "b"
INNER JOIN "FooDeep" AS "f0" ON "b"."FooId1" = "f0"."Id"
) AS "t" ON "f"."Id" = "t"."FooId"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ Result:

SQL:
---------------
SELECT 1, "d"."Id", "f"."Id", "s"."c", "s"."BarString", "s"."BarShort", "s"."Id", "s"."Id0"
SELECT "d"."Id", "f"."Id", "s"."BarString", "s"."BarShort", "s"."Id", "s"."Id0"
FROM "Data" AS "d"
INNER JOIN "Foo" AS "f" ON "d"."FooId" = "f"."Id"
LEFT JOIN (
SELECT 1 AS "c", "f0"."BarString", "f0"."BarShort", "b"."Id", "f0"."Id" AS "Id0", "b"."FooId"
SELECT "f0"."BarString", "f0"."BarShort", "b"."Id", "f0"."Id" AS "Id0", "b"."FooId"
FROM "BarDeep" AS "b"
INNER JOIN "FooDeep" AS "f0" ON "b"."FooId1" = "f0"."Id"
) AS "s" ON "f"."Id" = "s"."FooId"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ Result:

SQL:
---------------
SELECT 1, "f"."BarString", "d"."Id", "f"."Id", "t"."c", "t"."BarString", "t"."BarShort", "t"."Id", "t"."Id0"
SELECT "f"."BarString", "d"."Id", "f"."Id", "t"."BarString", "t"."BarShort", "t"."Id", "t"."Id0"
FROM "Data" AS "d"
INNER JOIN "Foo" AS "f" ON "d"."FooId" = "f"."Id"
LEFT JOIN (
SELECT 1 AS "c", "f0"."BarString", "f0"."BarShort", "b"."Id", "f0"."Id" AS "Id0", "b"."FooId"
SELECT "f0"."BarString", "f0"."BarShort", "b"."Id", "f0"."Id" AS "Id0", "b"."FooId"
FROM "BarDeep" AS "b"
INNER JOIN "FooDeep" AS "f0" ON "b"."FooId1" = "f0"."Id"
) AS "t" ON "f"."Id" = "t"."FooId"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ Result:

SQL:
---------------
SELECT 1, "f"."BarString", "d"."Id", "f"."Id", "s"."c", "s"."BarString", "s"."BarShort", "s"."Id", "s"."Id0"
SELECT "f"."BarString", "d"."Id", "f"."Id", "s"."BarString", "s"."BarShort", "s"."Id", "s"."Id0"
FROM "Data" AS "d"
INNER JOIN "Foo" AS "f" ON "d"."FooId" = "f"."Id"
LEFT JOIN (
SELECT 1 AS "c", "f0"."BarString", "f0"."BarShort", "b"."Id", "f0"."Id" AS "Id0", "b"."FooId"
SELECT "f0"."BarString", "f0"."BarShort", "b"."Id", "f0"."Id" AS "Id0", "b"."FooId"
FROM "BarDeep" AS "b"
INNER JOIN "FooDeep" AS "f0" ON "b"."FooId1" = "f0"."Id"
) AS "s" ON "f"."Id" = "s"."FooId"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Result:

SQL:
---------------
SELECT 1, "a"."d" = 'bar', "a"."Name", "a"."BarProp", "a"."d" = 'foo', "a"."FooProp"
SELECT "a"."d" = 'bar', "a"."Name", "a"."BarProp", "a"."d" = 'foo', "a"."FooProp"
FROM "Data" AS "d"
INNER JOIN "AbstractType" AS "a" ON "d"."NestedId" = "a"."Id"
---------------
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Result:

SQL:
---------------
SELECT 1, "f"."BarString"
SELECT "f"."BarString"
FROM "Data" AS "d"
INNER JOIN "Foo" AS "f" ON "d"."FooId" = "f"."Id"
---------------
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ Result:

SQL:
---------------
SELECT 1, "d"."Id", "f"."Id", "t"."c", "t"."BarString", "t"."BarShort", "t"."Id", "t"."Id0"
SELECT "d"."Id", "f"."Id", "t"."BarString", "t"."BarShort", "t"."Id", "t"."Id0"
FROM "Data" AS "d"
INNER JOIN "Foo" AS "f" ON "d"."FooId" = "f"."Id"
LEFT JOIN (
SELECT 1 AS "c", "f0"."BarString", "f0"."BarShort", "b"."Id", "f0"."Id" AS "Id0", "b"."FooId"
SELECT "f0"."BarString", "f0"."BarShort", "b"."Id", "f0"."Id" AS "Id0", "b"."FooId"
FROM "BarDeep" AS "b"
INNER JOIN "FooDeep" AS "f0" ON "b"."FooId1" = "f0"."Id"
) AS "t" ON "f"."Id" = "t"."FooId"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ Result:

SQL:
---------------
SELECT 1, "d"."Id", "f"."Id", "s"."c", "s"."BarString", "s"."BarShort", "s"."Id", "s"."Id0"
SELECT "d"."Id", "f"."Id", "s"."BarString", "s"."BarShort", "s"."Id", "s"."Id0"
FROM "Data" AS "d"
INNER JOIN "Foo" AS "f" ON "d"."FooId" = "f"."Id"
LEFT JOIN (
SELECT 1 AS "c", "f0"."BarString", "f0"."BarShort", "b"."Id", "f0"."Id" AS "Id0", "b"."FooId"
SELECT "f0"."BarString", "f0"."BarShort", "b"."Id", "f0"."Id" AS "Id0", "b"."FooId"
FROM "BarDeep" AS "b"
INNER JOIN "FooDeep" AS "f0" ON "b"."FooId1" = "f0"."Id"
) AS "s" ON "f"."Id" = "s"."FooId"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ Result:

SQL:
---------------
SELECT 1, "f"."BarString", "d"."Id", "f"."Id", "t"."c", "t"."BarString", "t"."BarShort", "t"."Id", "t"."Id0"
SELECT "f"."BarString", "d"."Id", "f"."Id", "t"."BarString", "t"."BarShort", "t"."Id", "t"."Id0"
FROM "Data" AS "d"
INNER JOIN "Foo" AS "f" ON "d"."FooId" = "f"."Id"
LEFT JOIN (
SELECT 1 AS "c", "f0"."BarString", "f0"."BarShort", "b"."Id", "f0"."Id" AS "Id0", "b"."FooId"
SELECT "f0"."BarString", "f0"."BarShort", "b"."Id", "f0"."Id" AS "Id0", "b"."FooId"
FROM "BarDeep" AS "b"
INNER JOIN "FooDeep" AS "f0" ON "b"."FooId1" = "f0"."Id"
) AS "t" ON "f"."Id" = "t"."FooId"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ Result:

SQL:
---------------
SELECT 1, "f"."BarString", "d"."Id", "f"."Id", "s"."c", "s"."BarString", "s"."BarShort", "s"."Id", "s"."Id0"
SELECT "f"."BarString", "d"."Id", "f"."Id", "s"."BarString", "s"."BarShort", "s"."Id", "s"."Id0"
FROM "Data" AS "d"
INNER JOIN "Foo" AS "f" ON "d"."FooId" = "f"."Id"
LEFT JOIN (
SELECT 1 AS "c", "f0"."BarString", "f0"."BarShort", "b"."Id", "f0"."Id" AS "Id0", "b"."FooId"
SELECT "f0"."BarString", "f0"."BarShort", "b"."Id", "f0"."Id" AS "Id0", "b"."FooId"
FROM "BarDeep" AS "b"
INNER JOIN "FooDeep" AS "f0" ON "b"."FooId1" = "f0"."Id"
) AS "s" ON "f"."Id" = "s"."FooId"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ Result:

SQL:
---------------
SELECT 1, "d"."Id", "f"."Id", "t"."c", "t"."BarString", "t"."BarShort", "t"."Id", "t"."Id0"
SELECT "d"."Id", "f"."Id", "t"."BarString", "t"."BarShort", "t"."Id", "t"."Id0"
FROM "Data" AS "d"
INNER JOIN "Foo" AS "f" ON "d"."FooId" = "f"."Id"
LEFT JOIN (
SELECT 1 AS "c", "f0"."BarString", "f0"."BarShort", "b"."Id", "f0"."Id" AS "Id0", "b"."FooId"
SELECT "f0"."BarString", "f0"."BarShort", "b"."Id", "f0"."Id" AS "Id0", "b"."FooId"
FROM "BarDeep" AS "b"
INNER JOIN "FooDeep" AS "f0" ON "b"."FooId1" = "f0"."Id"
) AS "t" ON "f"."Id" = "t"."FooId"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ Result:

SQL:
---------------
SELECT 1, "d"."Id", "f"."Id", "s"."c", "s"."BarString", "s"."BarShort", "s"."Id", "s"."Id0"
SELECT "d"."Id", "f"."Id", "s"."BarString", "s"."BarShort", "s"."Id", "s"."Id0"
FROM "Data" AS "d"
INNER JOIN "Foo" AS "f" ON "d"."FooId" = "f"."Id"
LEFT JOIN (
SELECT 1 AS "c", "f0"."BarString", "f0"."BarShort", "b"."Id", "f0"."Id" AS "Id0", "b"."FooId"
SELECT "f0"."BarString", "f0"."BarShort", "b"."Id", "f0"."Id" AS "Id0", "b"."FooId"
FROM "BarDeep" AS "b"
INNER JOIN "FooDeep" AS "f0" ON "b"."FooId1" = "f0"."Id"
) AS "s" ON "f"."Id" = "s"."FooId"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ Result:

SQL:
---------------
SELECT 1, "f"."BarString", "d"."Id", "f"."Id", "t"."c", "t"."BarString", "t"."BarShort", "t"."Id", "t"."Id0"
SELECT "f"."BarString", "d"."Id", "f"."Id", "t"."BarString", "t"."BarShort", "t"."Id", "t"."Id0"
FROM "Data" AS "d"
INNER JOIN "Foo" AS "f" ON "d"."FooId" = "f"."Id"
LEFT JOIN (
SELECT 1 AS "c", "f0"."BarString", "f0"."BarShort", "b"."Id", "f0"."Id" AS "Id0", "b"."FooId"
SELECT "f0"."BarString", "f0"."BarShort", "b"."Id", "f0"."Id" AS "Id0", "b"."FooId"
FROM "BarDeep" AS "b"
INNER JOIN "FooDeep" AS "f0" ON "b"."FooId1" = "f0"."Id"
) AS "t" ON "f"."Id" = "t"."FooId"
Expand Down
Loading
Loading