@@ -11,14 +11,12 @@ namespace HotChocolate.Execution.Projections;
11
11
12
12
internal sealed class SelectionExpressionBuilder
13
13
{
14
- private static readonly NullabilityInfoContext _nullabilityInfoContext = new ( ) ;
15
-
16
14
public Expression < Func < TRoot , TRoot > > BuildExpression < TRoot > ( ISelection selection )
17
15
{
18
16
var rootType = typeof ( TRoot ) ;
19
17
var parameter = Expression . Parameter ( rootType , "root" ) ;
20
18
var requirements = selection . DeclaringOperation . Schema . Features . GetRequired < FieldRequirementsMetadata > ( ) ;
21
- var context = new Context ( parameter , rootType , requirements ) ;
19
+ var context = new Context ( parameter , rootType , requirements , new NullabilityInfoContext ( ) ) ;
22
20
var root = new TypeContainer ( ) ;
23
21
24
22
CollectTypes ( context , selection , root ) ;
@@ -38,7 +36,7 @@ public Expression<Func<TRoot, TRoot>> BuildNodeExpression<TRoot>(ISelection sele
38
36
var rootType = typeof ( TRoot ) ;
39
37
var parameter = Expression . Parameter ( rootType , "root" ) ;
40
38
var requirements = selection . DeclaringOperation . Schema . Features . GetRequired < FieldRequirementsMetadata > ( ) ;
41
- var context = new Context ( parameter , rootType , requirements ) ;
39
+ var context = new Context ( parameter , rootType , requirements , new NullabilityInfoContext ( ) ) ;
42
40
var root = new TypeContainer ( ) ;
43
41
44
42
var entityType = selection . DeclaringOperation
@@ -252,7 +250,7 @@ private void CollectSelections(
252
250
253
251
if ( node . Nodes . Count == 0 )
254
252
{
255
- if ( IsNullableType ( node . Property ) )
253
+ if ( IsNullableType ( context , node . Property ) )
256
254
{
257
255
var nullCheck = Expression . Condition (
258
256
Expression . Equal ( propertyAccessor , Expression . Constant ( null ) ) ,
@@ -273,7 +271,7 @@ private void CollectSelections(
273
271
var newContext = context with { Parent = propertyAccessor , ParentType = node . Property . PropertyType } ;
274
272
var nestedExpression = BuildTypeSwitchExpression ( newContext , node ) ;
275
273
276
- if ( IsNullableType ( node . Property ) )
274
+ if ( IsNullableType ( context , node . Property ) )
277
275
{
278
276
var nullCheck = Expression . Condition (
279
277
Expression . Equal ( propertyAccessor , Expression . Constant ( null ) ) ,
@@ -286,22 +284,22 @@ private void CollectSelections(
286
284
return nestedExpression is null ? null : Expression . Bind ( node . Property , nestedExpression ) ;
287
285
}
288
286
289
- private static bool IsNullableType ( PropertyInfo propertyInfo )
287
+ private static bool IsNullableType ( Context context , PropertyInfo propertyInfo )
290
288
{
291
289
if ( propertyInfo . PropertyType . IsValueType )
292
290
{
293
291
return Nullable . GetUnderlyingType ( propertyInfo . PropertyType ) != null ;
294
292
}
295
293
296
- var nullabilityInfo = _nullabilityInfoContext . Create ( propertyInfo ) ;
297
-
294
+ var nullabilityInfo = context . NullabilityInfoContext . Create ( propertyInfo ) ;
298
295
return nullabilityInfo . WriteState == NullabilityState . Nullable ;
299
296
}
300
297
301
298
private readonly record struct Context (
302
299
Expression Parent ,
303
300
Type ParentType ,
304
- FieldRequirementsMetadata Requirements )
301
+ FieldRequirementsMetadata Requirements ,
302
+ NullabilityInfoContext NullabilityInfoContext )
305
303
{
306
304
public TypeNode ? GetRequirements ( ISelection selection )
307
305
{
0 commit comments