Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public sealed class GraphQLDeprecatedAttribute : Attribute
/// <param name="deprecationReason">The deprecation reason.</param>
public GraphQLDeprecatedAttribute(string deprecationReason)
{
ArgumentException.ThrowIfNullOrEmpty(deprecationReason);
ArgumentException.ThrowIfNullOrWhiteSpace(deprecationReason);

DeprecationReason = deprecationReason;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static ObjectTypeDefinitionNode Format(IObjectTypeDefinition type)
DirectiveNames.Deprecated.Name,
new ArgumentNode(
DirectiveNames.Deprecated.Arguments.Reason,
type.DeprecationReason ?? DirectiveNames.Deprecated.Arguments.DefaultReason));
type.DeprecationReason));

directives.Insert(0, deprecatedDirective);
}
Expand Down Expand Up @@ -91,8 +91,7 @@ public static DirectiveDefinitionNode Format(IDirectiveDefinition directiveDefin
DirectiveNames.Deprecated.Name,
new ArgumentNode(
DirectiveNames.Deprecated.Arguments.Reason,
directiveDefinition.DeprecationReason
?? DirectiveNames.Deprecated.Arguments.DefaultReason));
directiveDefinition.DeprecationReason));

directives.Insert(0, deprecatedDirective);
}
Expand All @@ -119,7 +118,7 @@ public static FieldDefinitionNode Format(IOutputFieldDefinition field)
DirectiveNames.Deprecated.Name,
new ArgumentNode(
DirectiveNames.Deprecated.Arguments.Reason,
field.DeprecationReason ?? DirectiveNames.Deprecated.Arguments.DefaultReason));
field.DeprecationReason));

directives.Insert(0, deprecatedDirective);
}
Expand All @@ -145,7 +144,7 @@ public static InputValueDefinitionNode Format(IInputValueDefinition field)
DirectiveNames.Deprecated.Name,
new ArgumentNode(
DirectiveNames.Deprecated.Arguments.Reason,
field.DeprecationReason ?? DirectiveNames.Deprecated.Arguments.DefaultReason));
field.DeprecationReason));

directives.Insert(0, deprecatedDirective);
}
Expand All @@ -171,7 +170,7 @@ public static EnumValueDefinitionNode Format(IEnumValue value)
DirectiveNames.Deprecated.Name,
new ArgumentNode(
DirectiveNames.Deprecated.Arguments.Reason,
value.DeprecationReason ?? DirectiveNames.Deprecated.Arguments.DefaultReason));
value.DeprecationReason));

directives.Insert(0, deprecatedDirective);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -631,17 +631,10 @@ private static List<DirectiveNode> ApplyDeprecatedDirective(
return directives;
}

private static DirectiveNode CreateDeprecatedDirective(string? reason = null)
{
if (string.IsNullOrEmpty(reason))
{
reason = DirectiveNames.Deprecated.Arguments.DefaultReason;
}

return new DirectiveNode(
private static DirectiveNode CreateDeprecatedDirective(string reason)
=> new(
new NameNode(DirectiveNames.Deprecated.Name),
[new ArgumentNode(DirectiveNames.Deprecated.Arguments.Reason, reason)]);
}

private static StringValueNode? CreateDescription(string? description)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ public interface IDirectiveDefinition
IReadOnlyDirectiveCollection IDirectivesProvider.Directives
=> EmptyCollections.Directives;

bool IDeprecationProvider.IsDeprecated => false;

string? IDeprecationProvider.DeprecationReason => null;

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,5 @@ public interface IObjectTypeDefinition
, IDeprecationProvider
, ISyntaxNodeProvider<ObjectTypeDefinitionNode>
{
bool IDeprecationProvider.IsDeprecated => false;

string? IDeprecationProvider.DeprecationReason => null;
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Diagnostics.CodeAnalysis;

#pragma warning disable IDE0130 // Namespace does not match folder structure
// ReSharper disable once CheckNamespace
namespace HotChocolate.Types;
Expand All @@ -10,12 +12,24 @@ public interface IDeprecationProvider : ITypeSystemMember
{
/// <summary>
/// Defines if this <see cref="ITypeSystemMember"/> is deprecated.
/// This is <c>true</c> if a <see cref="DeprecationReason"/> is present.
/// </summary>
[MemberNotNullWhen(true, nameof(DeprecationReason))]
bool IsDeprecated => DeprecationReason is not null;

/// <summary>
/// Defines if this <see cref="ITypeSystemMember"/> is deprecated without
/// a specific deprecation reason being provided.
/// </summary>
bool IsDeprecated { get; }
bool HasDefaultDeprecationReason
=> string.Equals(
DeprecationReason,
DirectiveNames.Deprecated.Arguments.DefaultReason,
StringComparison.Ordinal);

/// <summary>
/// Gets the deprecation reason of this <see cref="ITypeSystemMember"/>,
/// or <c>null</c> if no reason was provided.
/// or <c>null</c> if this member is not deprecated.
/// </summary>
string? DeprecationReason { get; }
}
8 changes: 6 additions & 2 deletions src/HotChocolate/Core/src/Types/Types/Contracts/EnumValue.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using HotChocolate.Configuration;
using HotChocolate.Features;
using HotChocolate.Language;
Expand Down Expand Up @@ -37,11 +38,14 @@ public abstract class EnumValue

/// <summary>
/// Defines if this enum value is deprecated.
/// This is <c>true</c> if a <see cref="DeprecationReason"/> is present.
/// </summary>
public abstract bool IsDeprecated { get; }
[MemberNotNullWhen(true, nameof(DeprecationReason))]
public bool IsDeprecated => DeprecationReason is not null;

/// <summary>
/// Gets the deprecation reason for this enum value.
/// Gets the deprecation reason for this enum value,
/// or <c>null</c> if this enum value is not deprecated.
/// </summary>
public abstract string? DeprecationReason { get; }

Expand Down
7 changes: 3 additions & 4 deletions src/HotChocolate/Core/src/Types/Types/DefaultEnumValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ public DefaultEnumValue(EnumValueConfiguration configuration)
? configuration.RuntimeValue.ToString()!
: configuration.Name;
Description = configuration.Description;
DeprecationReason = configuration.DeprecationReason;
IsDeprecated = !string.IsNullOrEmpty(configuration.DeprecationReason);
DeprecationReason = string.IsNullOrWhiteSpace(configuration.DeprecationReason)
? null
: configuration.DeprecationReason;
Value = configuration.RuntimeValue;
Features = configuration.GetFeatures();
}
Expand All @@ -37,8 +38,6 @@ public DefaultEnumValue(EnumValueConfiguration configuration)

public override string? Description { get; }

public override bool IsDeprecated { get; }

public override string? DeprecationReason { get; }

public override object Value { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ protected ArgumentDescriptorBase(IDescriptorContext context)
/// <inheritdoc cref="IArgumentDescriptor.Deprecated(string)"/>
protected void Deprecated(string? reason)
{
if (string.IsNullOrEmpty(reason))
if (string.IsNullOrWhiteSpace(reason))
{
Deprecated();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public DirectiveTypeConfiguration(
/// <summary>
/// Defines if this directive is deprecated.
/// </summary>
public bool IsDeprecated => !string.IsNullOrEmpty(DeprecationReason);
public bool IsDeprecated => !string.IsNullOrWhiteSpace(DeprecationReason);

/// <summary>
/// Gets the list of directives that are annotated to this directive.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public EnumValueConfiguration(
/// <summary>
/// Defines if this enum value is deprecated.
/// </summary>
public bool IsDeprecated => !string.IsNullOrEmpty(DeprecationReason);
public bool IsDeprecated => !string.IsNullOrWhiteSpace(DeprecationReason);

/// <summary>
/// Defines if this enum value is ignored
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public string? DeprecationReason
get => _deprecationReason;
set
{
if (string.IsNullOrEmpty(value))
if (string.IsNullOrWhiteSpace(value))
{
Flags &= ~CoreFieldFlags.Deprecated;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public IDirectiveTypeDescriptor Description(string value)

public IDirectiveTypeDescriptor Deprecated(string? reason)
{
if (string.IsNullOrEmpty(reason))
if (string.IsNullOrWhiteSpace(reason))
{
return Deprecated();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public IEnumValueDescriptor Description(string value)

public IEnumValueDescriptor Deprecated(string reason)
{
if (string.IsNullOrEmpty(reason))
if (string.IsNullOrWhiteSpace(reason))
{
return Deprecated();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public IObjectTypeDescriptor Deprecated(string? reason)
throw ThrowHelper.ObjectDeprecationNotEnabled(Configuration.Name);
}

Configuration.DeprecationReason = string.IsNullOrEmpty(reason)
Configuration.DeprecationReason = string.IsNullOrWhiteSpace(reason)
? DirectiveNames.Deprecated.Arguments.DefaultReason
: reason;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ protected void Argument(

public void Deprecated(string? reason)
{
if (string.IsNullOrEmpty(reason))
if (string.IsNullOrWhiteSpace(reason))
{
Deprecated();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ protected override void OnCompleteType(
_inputParser = context.DescriptorContext.InputParser;

Locations = configuration.Locations;
IsDeprecated = !string.IsNullOrEmpty(configuration.DeprecationReason);
DeprecationReason = configuration.DeprecationReason;
DeprecationReason = string.IsNullOrWhiteSpace(configuration.DeprecationReason)
? null
: configuration.DeprecationReason;
Arguments = OnCompleteFields(context, configuration);
IsPublic = configuration.IsPublic
|| context.DescriptorContext.Options.DisableInternalDirectives;
Expand Down
8 changes: 6 additions & 2 deletions src/HotChocolate/Core/src/Types/Types/DirectiveType.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using HotChocolate.Language;
using HotChocolate.Resolvers;
using HotChocolate.Types.Descriptors.Configurations;
Expand Down Expand Up @@ -91,11 +92,14 @@ IReadOnlyDirectiveCollection IDirectivesProvider.Directives

/// <summary>
/// Defines if this directive is deprecated.
/// This is <c>true</c> if a <see cref="DeprecationReason"/> is present.
/// </summary>
public bool IsDeprecated { get; private set; }
[MemberNotNullWhen(true, nameof(DeprecationReason))]
public bool IsDeprecated => DeprecationReason is not null;

/// <summary>
/// Gets the deprecation reason of this directive.
/// Gets the deprecation reason of this directive,
/// or <c>null</c> if this directive is not deprecated.
/// </summary>
public string? DeprecationReason { get; private set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static void AddDirectives<TOwner>(
if (directive.Arguments.Count != 0
&& directive.Arguments[0].Name.Value == DirectiveNames.Deprecated.Arguments.Reason
&& directive.Arguments[0].Value is StringValueNode s
&& !string.IsNullOrEmpty(s.Value))
&& !string.IsNullOrWhiteSpace(s.Value))
{
return s.Value;
}
Expand All @@ -57,7 +57,7 @@ public static string DeprecationReason(this DirectiveNode directive)
if (directive.Arguments.Count != 0
&& directive.Arguments[0].Name.Value == DirectiveNames.Deprecated.Arguments.Reason
&& directive.Arguments[0].Value is StringValueNode s
&& !string.IsNullOrEmpty(s.Value))
&& !string.IsNullOrWhiteSpace(s.Value))
{
return s.Value;
}
Expand Down
10 changes: 6 additions & 4 deletions src/HotChocolate/Core/src/Types/Types/FieldBase.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using HotChocolate.Configuration;
using HotChocolate.Features;
using HotChocolate.Language;
Expand Down Expand Up @@ -37,8 +38,9 @@ protected FieldBase(FieldConfiguration configuration, int index)

Name = configuration.Name.EnsureGraphQLName();
Description = configuration.Description;
IsDeprecated = !string.IsNullOrEmpty(configuration.DeprecationReason);
DeprecationReason = configuration.DeprecationReason;
DeprecationReason = string.IsNullOrWhiteSpace(configuration.DeprecationReason)
? null
: configuration.DeprecationReason;
Flags = configuration.Flags;
DeclaringType = null!;
DeclaringMember = null!;
Expand Down Expand Up @@ -66,7 +68,6 @@ protected FieldBase(FieldBase original, IType type)
Index = original.Index;
Name = original.Name;
Description = original.Description;
IsDeprecated = original.IsDeprecated;
DeprecationReason = original.DeprecationReason;
DeclaringType = original.DeclaringType;
DeclaringMember = original.DeclaringMember;
Expand Down Expand Up @@ -106,7 +107,8 @@ IReadOnlyDirectiveCollection IDirectivesProvider.Directives
=> Directives.AsReadOnlyDirectiveCollection();

/// <inheritdoc />
public bool IsDeprecated { get; }
[MemberNotNullWhen(true, nameof(DeprecationReason))]
public bool IsDeprecated => DeprecationReason is not null;

/// <inheritdoc />
public string? DeprecationReason { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ protected override void OnCompleteType(
{
base.OnCompleteType(context, configuration);

IsDeprecated = !string.IsNullOrEmpty(configuration.DeprecationReason);
DeprecationReason = configuration.DeprecationReason;
DeprecationReason = string.IsNullOrWhiteSpace(configuration.DeprecationReason)
? null
: configuration.DeprecationReason;

if (ValidateFields(context, configuration))
{
Expand Down
4 changes: 3 additions & 1 deletion src/HotChocolate/Core/src/Types/Types/ObjectType.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using HotChocolate.Language;
using HotChocolate.Resolvers;
using HotChocolate.Types.Descriptors.Configurations;
Expand Down Expand Up @@ -83,7 +84,8 @@ IReadOnlyFieldDefinitionCollection<IOutputFieldDefinition> IComplexTypeDefinitio
=> Fields.AsReadOnlyFieldDefinitionCollection();

/// <inheritdoc />
public bool IsDeprecated { get; private set; }
[MemberNotNullWhen(true, nameof(DeprecationReason))]
public bool IsDeprecated => DeprecationReason is not null;

/// <inheritdoc />
public string? DeprecationReason { get; private set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,4 @@ type Foo implements Bar @deprecated(reason: "Use Bar.") @example {
}
""");
}

[Fact]
public void Format_Should_UseDefaultReason_When_ObjectTypeHasNoDeprecationReason()
{
// arrange
// the deprecation state is set programmatically, so the formatter synthesizes @deprecated
const string sdl =
"""
type Foo {
id: ID
}
""";
var schema = SchemaParser.Parse(Encoding.UTF8.GetBytes(sdl));
var fooType = (MutableObjectTypeDefinition)schema.Types["Foo"];
fooType.IsDeprecated = true;

// act
var syntaxNode = SchemaDebugFormatter.Format(fooType);

// assert
syntaxNode.ToString().MatchInlineSnapshot(
"""
type Foo @deprecated(reason: "No longer supported.") {
id: ID
}
""");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ public void Deprecated_NoReason_SetsDefaultReason()
[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData(" ")]
public void Deprecated_EmptyReason_SetsDefaultReason(string? reason)
{
// arrange
Expand Down
Loading
Loading