Skip to content

Commit

Permalink
Print @deprecated directive in HotChocolate.Skimmed
Browse files Browse the repository at this point in the history
  • Loading branch information
tobias-tengler committed Sep 29, 2023
1 parent b1dc205 commit 9da664a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ public static DirectiveNode CreateNode(string? reason = null)

var arguments = reason is null
? Array.Empty<ArgumentNode>()
: new[] { new ArgumentNode(DeprecatedDirectiveType.Names.Reason, reason) };
: new[] { new ArgumentNode(WellKnownDirectives.DeprecationReasonArgument, reason) };

return new DirectiveNode(
null,
new NameNode(DeprecatedDirectiveType.Names.Deprecated),
new NameNode(WellKnownDirectives.Deprecated),
arguments);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ protected override void Configure(
IDirectiveTypeDescriptor<DeprecatedDirective> descriptor)
{
descriptor
.Name(Names.Deprecated)
.Name(WellKnownDirectives.Deprecated)
.Description(TypeResources.DeprecatedDirectiveType_TypeDescription)
.Location(DirectiveLocation.FieldDefinition)
.Location(DirectiveLocation.ArgumentDefinition)
Expand All @@ -25,15 +25,9 @@ protected override void Configure(

descriptor
.Argument(t => t.Reason)
.Name(Names.Reason)
.Name(WellKnownDirectives.DeprecationReasonArgument)
.Description(TypeResources.DeprecatedDirectiveType_ReasonDescription)
.Type<StringType>()
.DefaultValue(WellKnownDirectives.DeprecationDefaultReason);
}

public static class Names
{
public const string Deprecated = "deprecated";
public const string Reason = "reason";
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using HotChocolate.Language;
using HotChocolate.Language.Utilities;
using HotChocolate.Utilities;

namespace HotChocolate.Skimmed.Serialization;

Expand All @@ -9,20 +10,20 @@ public static class SchemaFormatter
private static readonly SyntaxSerializerOptions _options =
new()
{
Indented = true,
Indented = true,
MaxDirectivesPerLine = 0
};

public static string FormatAsString(Schema schema, bool indented = true)
{
var context = new VisitorContext();
_visitor.VisitSchema(schema, context);

if (!indented)
{
((DocumentNode) context.Result!).ToString(false);
((DocumentNode)context.Result!).ToString(false);
}

return ((DocumentNode)context.Result!).ToString(_options);
}

Expand Down Expand Up @@ -119,7 +120,7 @@ public override void VisitTypes(TypeCollection types, VisitorContext context)

foreach (var type in types.OfType<ObjectType>().OrderBy(t => t.Name))
{
if(context.Schema?.QueryType == type ||
if (context.Schema?.QueryType == type ||
context.Schema?.MutationType == type ||
context.Schema?.SubscriptionType == type)
{
Expand Down Expand Up @@ -156,7 +157,7 @@ public override void VisitTypes(TypeCollection types, VisitorContext context)

foreach (var type in types.OfType<ScalarType>().OrderBy(t => t.Name))
{
if (type is { IsSpecScalar: true } || SpecScalarTypes.IsSpecScalar(type.Name))
if (type is { IsSpecScalar: true } || SpecScalarTypes.IsSpecScalar(type.Name))
{
type.IsSpecScalar = true;
continue;
Expand Down Expand Up @@ -326,6 +327,8 @@ public override void VisitEnumValue(EnumValue value, VisitorContext context)
VisitDirectives(value.Directives, context);
var directives = (List<DirectiveNode>)context.Result!;

directives = ApplyDeprecatedDirective(value, directives);

context.Result = new EnumValueDefinitionNode(
null,
new NameNode(value.Name),
Expand Down Expand Up @@ -399,6 +402,8 @@ public override void VisitOutputField(OutputField field, VisitorContext context)
VisitDirectives(field.Directives, context);
var directives = (List<DirectiveNode>)context.Result!;

directives = ApplyDeprecatedDirective(field, directives);

context.Result = new FieldDefinitionNode(
null,
new NameNode(field.Name),
Expand Down Expand Up @@ -428,6 +433,9 @@ public override void VisitInputFields(
public override void VisitInputField(InputField field, VisitorContext context)
{
VisitDirectives(field.Directives, context);
var directives = (List<DirectiveNode>)context.Result!;

directives = ApplyDeprecatedDirective(field, directives);

context.Result = new InputValueDefinitionNode(
null,
Expand All @@ -437,7 +445,7 @@ field.Description is not null
: null,
field.Type.ToTypeNode(),
field.DefaultValue,
(List<DirectiveNode>)context.Result!);
directives);
}

public override void VisitDirectives(DirectiveCollection directives, VisitorContext context)
Expand Down Expand Up @@ -481,6 +489,46 @@ public override void VisitArgument(Argument argument, VisitorContext context)
{
context.Result = new ArgumentNode(argument.Name, argument.Value);
}

private static List<DirectiveNode> ApplyDeprecatedDirective(
ICanBeDeprecated canBeDeprecated,
List<DirectiveNode> directives)
{
if (canBeDeprecated.IsDeprecated)
{
var deprecateDirective = CreateDeprecatedDirective(canBeDeprecated.DeprecationReason);

if (directives.Count == 0)
{
directives = new List<DirectiveNode> { deprecateDirective };
}
else
{
var temp = directives.ToList();
temp.Add(deprecateDirective);
directives = temp;
}
}

return directives;
}

private static DirectiveNode CreateDeprecatedDirective(string? reason = null)
{
if (WellKnownDirectives.DeprecationDefaultReason.EqualsOrdinal(reason))
{
reason = null;
}

var arguments = reason is null
? Array.Empty<ArgumentNode>()
: new[] { new ArgumentNode(WellKnownDirectives.DeprecationReasonArgument, reason) };

return new DirectiveNode(
null,
new NameNode(WellKnownDirectives.Deprecated),
arguments);
}
}

private sealed class VisitorContext
Expand Down

0 comments on commit 9da664a

Please sign in to comment.