Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to ignore enum values #4020

Merged
merged 5 commits into from
Aug 10, 2021
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -3,7 +3,7 @@
namespace HotChocolate
{
[AttributeUsage(AttributeTargets.Property
| AttributeTargets.Method)]
| AttributeTargets.Method | AttributeTargets.Field)]
public sealed class GraphQLIgnoreAttribute
: Attribute
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,31 @@ IEnumTypeDescriptor SyntaxNode(
/// <param name="value">
/// The name value.
/// </param>
IEnumTypeDescriptor Name(
NameString value);
IEnumTypeDescriptor Name(NameString value);

/// <summary>
/// Defines the description that the enum type shall have.
/// </summary>
/// <param name="value">
/// The description value.
/// </param>
IEnumTypeDescriptor Description(
string value);
IEnumTypeDescriptor Description(string value);

[Obsolete("Use `Value`.")]
IEnumValueDescriptor Item<T>(
T value);
IEnumValueDescriptor Item<T>(T value);

IEnumValueDescriptor Value<T>(
T value);
/// <summary>
/// Defines a value that should be included on the enum type.
/// </summary>
/// <param name="value">
/// The value to include.
/// </param>
IEnumValueDescriptor Value<T>(T value);

[Obsolete("Use `BindValues`.")]
IEnumTypeDescriptor BindItems(
BindingBehavior behavior);
IEnumTypeDescriptor BindItems(BindingBehavior behavior);

IEnumTypeDescriptor BindValues(
BindingBehavior behavior);
IEnumTypeDescriptor BindValues(BindingBehavior behavior);

/// <summary>
/// Defines that all enum values have to be specified explicitly.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ IEnumTypeDescriptor<T> SyntaxNode(
[Obsolete("Use `Value`.")]
IEnumValueDescriptor Item(T value);

/// <summary>
/// Defines a value that should be included on the enum type.
/// </summary>
/// <param name="value">
/// The value to include.
/// </param>
IEnumValueDescriptor Value(T value);

[Obsolete("Use `BindValues`.")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,51 @@ public interface IEnumValueDescriptor
IEnumValueDescriptor SyntaxNode(
EnumValueDefinitionNode enumValueDefinition);

/// <summary>
/// Defines the name of the <see cref="EnumValue"/>.
/// The name represents the public visible enum value name.
/// </summary>
/// <param name="value">The enum value name.</param>
/// <exception cref="ArgumentNullException">
/// <paramref name="value"/> is <c>null</c> or
/// <see cref="string.Empty"/>.
/// </exception>
IEnumValueDescriptor Name(NameString value);

/// <summary>
/// Adds explanatory text to the <see cref="EnumValue"/>
/// that can be accessed via introspection.
/// </summary>
/// <param name="value">The enum value description.</param>
IEnumValueDescriptor Description(string value);

/// <summary>
/// Specifies a deprecation reason for this enum value.
/// </summary>
/// <param name="reason">The reason why this enum value is deprecated.</param>
[Obsolete("Use `Deprecated`.")]
IEnumValueDescriptor DeprecationReason(
string reason);
IEnumValueDescriptor DeprecationReason(string reason);

/// <summary>
/// Deprecates the enum value.
/// </summary>
/// <param name="reason">The reason why this enum value is deprecated.</param>
IEnumValueDescriptor Deprecated(string reason);

/// <summary>
/// Deprecates the enum value.
/// </summary>
IEnumValueDescriptor Deprecated();

/// <summary>
/// Ignores the given enum value for the schema creation.
/// This enum will not be included into the GraphQL schema.
/// </summary>
/// <param name="ignore">
/// The value specifying if this enum value shall be ignored by the type initialization.
/// </param>
IEnumValueDescriptor Ignore(bool ignore = true);

IEnumValueDescriptor Directive<T>(
T directiveInstance)
where T : class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ IObjectFieldDescriptor Argument(
NameString argumentName,
Action<IArgumentDescriptor> argumentDescriptor);

/// <summary>
/// Ignores the given field for the schema creation.
/// This field will not be included into the GraphQL schema.
/// </summary>
/// <param name="ignore">
/// The value specifying if this field shall be ignored by the type initialization.
/// </param>
IObjectFieldDescriptor Ignore(bool ignore = true);

[Obsolete("Use Resolve(...)")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace HotChocolate.Types.Descriptors.Definitions
public class EnumValueDefinition
: TypeDefinitionBase<EnumValueDefinitionNode>
, ICanBeDeprecated
, IHasIgnore
{
/// <summary>
/// Initializes a new instance of <see cref="EnumValueDefinition"/>.
Expand Down Expand Up @@ -40,6 +41,12 @@ public EnumValueDefinition(
/// </summary>
public bool IsDeprecated => !string.IsNullOrEmpty(DeprecationReason);

/// <summary>
/// Defines if this enum value is ignored
/// and therefore excluded from the schema.
/// </summary>
public bool Ignore { get; set; }

/// <summary>
/// Gets the runtime value.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ protected override void OnCreateDefinition(EnumValueDefinition definition)
this,
Definition.Member);
Definition.AttributesAreApplied = true;

if (Context.TypeInspector.IsMemberIgnored(Definition.Member))
{
Ignore();
}
}

base.OnCreateDefinition(definition);
Expand Down Expand Up @@ -89,6 +94,12 @@ public IEnumValueDescriptor Deprecated()
return this;
}

public IEnumValueDescriptor Ignore(bool ignore = true)
{
Definition.Ignore = ignore;
return this;
}

public IEnumValueDescriptor Directive<T>(T directiveInstance)
where T : class
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using System;

namespace HotChocolate.Types
{
/// <summary>
/// Provides extensions to the <see cref="IEnumTypeDescriptor"/> and
/// <see cref="IEnumTypeDescriptor{T}"/>.
/// </summary>
public static class EnumTypeDescriptorExtensions
{
/// <summary>
/// Ignores the given enum value for the schema creation.
/// This enum will not be included into the GraphQL schema.
/// </summary>
/// <param name="descriptor">
/// The enum type descriptor.
/// </param>
/// <param name="value">
/// The enum value that shall be ignored.
/// </param>
/// <typeparam name="T">
/// The enum value type.
/// </typeparam>
/// <returns>
/// Returns the enum type descriptor for configuration chaining.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="descriptor"/> is <c>null</c> or
/// <paramref name="value"/> is <c>null</c>.
/// </exception>
public static IEnumTypeDescriptor<T> Ignore<T>(
this IEnumTypeDescriptor<T> descriptor,
T value)
{
if (descriptor is null)
{
throw new ArgumentNullException(nameof(descriptor));
}

if (value is null)
{
throw new ArgumentNullException(nameof(value));
}

descriptor.Value(value).Ignore();
return descriptor;
}

/// <summary>
/// Ignores the given enum value for the schema creation.
/// This enum will not be included into the GraphQL schema.
/// </summary>
/// <param name="descriptor">
/// The enum type descriptor.
/// </param>
/// <param name="value">
/// The enum value that shall be ignored.
/// </param>
/// <typeparam name="T">
/// The enum value type.
/// </typeparam>
/// <returns>
/// Returns the enum type descriptor for configuration chaining.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="descriptor"/> is <c>null</c> or
/// <paramref name="value"/> is <c>null</c>.
/// </exception>
public static IEnumTypeDescriptor Ignore<T>(
this IEnumTypeDescriptor descriptor,
T value)
{
if (descriptor is null)
{
throw new ArgumentNullException(nameof(descriptor));
}

if (value is null)
{
throw new ArgumentNullException(nameof(value));
}

descriptor.Value(value).Ignore();
return descriptor;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public EnumType(Action<IEnumTypeDescriptor> configure)
/// Returns the newly created enum type.
/// </returns>
public static EnumType CreateUnsafe(EnumTypeDefinition definition)
=> new() { Definition = definition};
=> new() { Definition = definition };

/// <summary>
/// Override this in order to specify the type configuration explicitly.
Expand Down Expand Up @@ -111,6 +111,9 @@ protected override void OnCompleteType(

foreach (EnumValueDefinition enumValueDefinition in definition.Values)
{
if (enumValueDefinition.Ignore)
continue;

if (TryCreateEnumValue(context, enumValueDefinition, out IEnumValue? enumValue))
{
_enumValues[enumValue.Name] = enumValue;
Expand All @@ -133,7 +136,7 @@ protected override void OnCompleteType(
protected virtual bool TryCreateEnumValue(
ITypeCompletionContext context,
EnumValueDefinition definition,
[NotNullWhen(true)]out IEnumValue? enumValue)
[NotNullWhen(true)] out IEnumValue? enumValue)
{
enumValue = new EnumValue(context, definition);
return true;
Expand Down
4 changes: 3 additions & 1 deletion src/HotChocolate/Core/src/Types/Types/EnumTypeExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public EnumTypeExtension(Action<IEnumTypeDescriptor> configure)
/// Returns the newly created enum type extension.
/// </returns>
public static EnumTypeExtension CreateUnsafe(EnumTypeDefinition definition)
=> new() { Definition = definition};
=> new() { Definition = definition };

/// <inheritdoc />
public override TypeKind Kind => TypeKind.Enum;
Expand Down Expand Up @@ -137,6 +137,8 @@ private void MergeValues(
}
else
{
existingValue.Ignore = enumValue.Ignore;

TypeExtensionHelper.MergeContextData(enumValue, existingValue);

TypeExtensionHelper.MergeDirectives(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Linq;
using HotChocolate.Language;
using Snapshooter.Xunit;
using Xunit;

namespace HotChocolate.Types
Expand Down Expand Up @@ -122,6 +123,20 @@ public void EnumTypeExtension_SetTypeContextData()
Assert.True(type.ContextData.ContainsKey("foo"));
}

[Fact]
public void EnumTypeExtension_Ignore_Fields()
{
// act
ISchema schema = SchemaBuilder.New()
.AddQueryType<DummyQuery>()
.AddType<FooType>()
.AddType<FooIgnoreTypeExtension>()
.Create();

// assert
schema.ToString().MatchSnapshot();
}

public class DummyQuery
{
public string Foo { get; set; }
Expand Down Expand Up @@ -149,6 +164,16 @@ protected override void Configure(IEnumTypeDescriptor descriptor)
}
}

public class FooIgnoreTypeExtension
: EnumTypeExtension
{
protected override void Configure(IEnumTypeDescriptor descriptor)
{
descriptor.Name("Foo");
descriptor.Value(Foo.Bar).Ignore();
}
}

public enum Foo
{
Bar,
Expand Down
Loading