Skip to content

Commit

Permalink
Support @include/@Skip for non-nullable fields (#7918)
Browse files Browse the repository at this point in the history
  • Loading branch information
timward60 authored and michaelstaib committed Jan 22, 2025
1 parent 531ca73 commit a329840
Show file tree
Hide file tree
Showing 6 changed files with 2,137 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ private static IValueNode GetIfArgumentValue(DirectiveNode directive)
throw ThrowHelper.MissingIfArgument(directive);
}

private static DirectiveNode? GetSkipDirectiveNode(
internal static DirectiveNode? GetSkipDirectiveNode(
this IReadOnlyList<DirectiveNode> directives)
=> GetDirectiveNode(directives, WellKnownDirectives.Skip);

private static DirectiveNode? GetIncludeDirectiveNode(
internal static DirectiveNode? GetIncludeDirectiveNode(
this IReadOnlyList<DirectiveNode> directives)
=> GetDirectiveNode(directives, WellKnownDirectives.Include);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@ public enum PropertyKind
/// A non-null field that is deferred.
/// </summary>
DeferredField,

/// <summary>
/// A non-null field that is included or skipped conditionally.
/// </summary>
SkipOrIncludeField
}
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,12 @@ private static void CollectClassesThatImplementInterface(
}
}

private static bool IncludeOrSkipDirective(OutputFieldModel field)
{
return field.SyntaxNode.Directives.GetIncludeDirectiveNode() is not null ||
field.SyntaxNode.Directives.GetSkipDirectiveNode() is not null;
}

private static void AddProperties(
ClientModel model,
Dictionary<string, TypeDescriptorModel> typeDescriptors,
Expand All @@ -388,6 +394,7 @@ private static void AddProperties(
{
INamedTypeDescriptor? fieldType;
var namedType = field.Type.NamedType();
var includeOrSkipDirective = IncludeOrSkipDirective(field);

if (namedType.IsScalarType() || namedType.IsEnumType())
{
Expand All @@ -402,14 +409,19 @@ private static void AddProperties(
typeDescriptors);
}

var propertyKind = includeOrSkipDirective
? PropertyKind.SkipOrIncludeField
: PropertyKind.Field;
properties.Add(
new PropertyDescriptor(
field.Name,
field.ResponseName,
BuildFieldType(
field.Type,
fieldType),
field.Description));
fieldType,
propertyKind),
field.Description,
propertyKind));
}

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

private static ITypeDescriptor BuildFieldType(
this IType original,
INamedTypeDescriptor namedTypeDescriptor)
INamedTypeDescriptor namedTypeDescriptor,
PropertyKind kind = PropertyKind.Field)
{
if (original is NonNullType nnt)
{
if (kind == PropertyKind.SkipOrIncludeField)
{
return BuildFieldType(
nnt.Type,
namedTypeDescriptor);
}

return new NonNullTypeDescriptor(
BuildFieldType(
nnt.Type,
Expand Down
Loading

0 comments on commit a329840

Please sign in to comment.