Skip to content

Commit

Permalink
Fixed issue with nullability inference of nested types. (#6230)
Browse files Browse the repository at this point in the history
Co-authored-by: Anton Tykhyy <atykhyy@gmail.com>
  • Loading branch information
michaelstaib and atykhyy committed Jun 2, 2023
1 parent 6283ee9 commit 679cc61
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ internal static bool IsNonGenericBaseType(Type type)
}

written = buffer.Length;
return false;
return true;
}
}
}
36 changes: 36 additions & 0 deletions src/HotChocolate/Core/test/Types.Tests/CodeFirstTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,18 @@ public async Task Allow_PascalCasedArguments()
""");
}

[Fact]
public async Task Infer_Nullability_From_Nested_Classes()
{
var schema =
await new ServiceCollection()
.AddGraphQLServer()
.AddQueryType<QueryNestedClassNullableString>()
.BuildSchemaAsync();

schema.MatchSnapshot();
}

public class Query
{
public string SayHello(string name) =>
Expand Down Expand Up @@ -324,4 +336,28 @@ public class PascalCaseQuery
{
public string TestResolver(string TestArgument) => "abc";
}

public sealed class QueryNestedClassNullableString
{
public class Outer
{
public string? shouldBeNullable { get; set; }
}

public class Example
{
public class Inner
{
public string? shouldAlsoBeNullable { get; set; }
}

public Inner? inner { get; set; } = new();
public Outer? outer { get; set; } = new();
}

public Example NestedClassNullableString()
{
return new Example();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,19 @@ public void From_IExecutableScalar()
Assert.True(dict.IsArrayOrList);
}

[Fact]
public void Nested_Nullability()
{
// arrange
// act
var extendedType = ExtendedType.FromMember(
typeof(Nullability).GetMember("NestedProp").Single(),
_cache);

// assert
Assert.True(extendedType.IsNullable);
}

private sealed class CustomStringList1
: List<string>
{
Expand Down Expand Up @@ -415,6 +428,13 @@ public class Nullability

public Optional<Nullable<Optional<string?>>> OptionalNullableOptionalNullableString() =>
throw new NotImplementedException();

public Nested? NestedProp { get; set; }

public class Nested
{
public string? Value { get; set; }
}
}

#nullable disable
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
schema {
query: QueryNestedClassNullableString
}

type Example {
inner: Inner!
outer: Outer!
}

type Inner {
shouldAlsoBeNullable: String!
}

type Outer {
shouldBeNullable: String!
}

type QueryNestedClassNullableString {
nestedClassNullableString: Example!
}

0 comments on commit 679cc61

Please sign in to comment.