Skip to content

Commit a329840

Browse files
timward60michaelstaib
authored andcommitted
Support @include/@Skip for non-nullable fields (#7918)
1 parent 531ca73 commit a329840

File tree

6 files changed

+2137
-5
lines changed

6 files changed

+2137
-5
lines changed

src/HotChocolate/Core/src/Types/Types/Extensions/DirectiveCollectionExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,11 @@ private static IValueNode GetIfArgumentValue(DirectiveNode directive)
105105
throw ThrowHelper.MissingIfArgument(directive);
106106
}
107107

108-
private static DirectiveNode? GetSkipDirectiveNode(
108+
internal static DirectiveNode? GetSkipDirectiveNode(
109109
this IReadOnlyList<DirectiveNode> directives)
110110
=> GetDirectiveNode(directives, WellKnownDirectives.Skip);
111111

112-
private static DirectiveNode? GetIncludeDirectiveNode(
112+
internal static DirectiveNode? GetIncludeDirectiveNode(
113113
this IReadOnlyList<DirectiveNode> directives)
114114
=> GetDirectiveNode(directives, WellKnownDirectives.Include);
115115

src/StrawberryShake/CodeGeneration/src/CodeGeneration/Descriptors/TypeDescriptors/PropertyKind.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,9 @@ public enum PropertyKind
1919
/// A non-null field that is deferred.
2020
/// </summary>
2121
DeferredField,
22+
23+
/// <summary>
24+
/// A non-null field that is included or skipped conditionally.
25+
/// </summary>
26+
SkipOrIncludeField
2227
}

src/StrawberryShake/CodeGeneration/src/CodeGeneration/Mappers/TypeDescriptorMapper.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,12 @@ private static void CollectClassesThatImplementInterface(
375375
}
376376
}
377377

378+
private static bool IncludeOrSkipDirective(OutputFieldModel field)
379+
{
380+
return field.SyntaxNode.Directives.GetIncludeDirectiveNode() is not null ||
381+
field.SyntaxNode.Directives.GetSkipDirectiveNode() is not null;
382+
}
383+
378384
private static void AddProperties(
379385
ClientModel model,
380386
Dictionary<string, TypeDescriptorModel> typeDescriptors,
@@ -388,6 +394,7 @@ private static void AddProperties(
388394
{
389395
INamedTypeDescriptor? fieldType;
390396
var namedType = field.Type.NamedType();
397+
var includeOrSkipDirective = IncludeOrSkipDirective(field);
391398

392399
if (namedType.IsScalarType() || namedType.IsEnumType())
393400
{
@@ -402,14 +409,19 @@ private static void AddProperties(
402409
typeDescriptors);
403410
}
404411

412+
var propertyKind = includeOrSkipDirective
413+
? PropertyKind.SkipOrIncludeField
414+
: PropertyKind.Field;
405415
properties.Add(
406416
new PropertyDescriptor(
407417
field.Name,
408418
field.ResponseName,
409419
BuildFieldType(
410420
field.Type,
411-
fieldType),
412-
field.Description));
421+
fieldType,
422+
propertyKind),
423+
field.Description,
424+
propertyKind));
413425
}
414426

415427
typeDescriptorModel.Descriptor.CompleteProperties(properties);
@@ -478,10 +490,18 @@ private static INamedTypeDescriptor GetFieldTypeDescriptor(
478490

479491
private static ITypeDescriptor BuildFieldType(
480492
this IType original,
481-
INamedTypeDescriptor namedTypeDescriptor)
493+
INamedTypeDescriptor namedTypeDescriptor,
494+
PropertyKind kind = PropertyKind.Field)
482495
{
483496
if (original is NonNullType nnt)
484497
{
498+
if (kind == PropertyKind.SkipOrIncludeField)
499+
{
500+
return BuildFieldType(
501+
nnt.Type,
502+
namedTypeDescriptor);
503+
}
504+
485505
return new NonNullTypeDescriptor(
486506
BuildFieldType(
487507
nnt.Type,

0 commit comments

Comments
 (0)