From 49bec1b1bdaf76274499a8f13308aefbaae3f0f5 Mon Sep 17 00:00:00 2001 From: PascalSenn Date: Fri, 27 Aug 2021 16:41:08 +0200 Subject: [PATCH] Fixed default values of enums (#4129) Co-authored-by: Michael Staib --- .../Types/Internal/TypeDependencyHelper.cs | 34 ++++++++++++++++- .../Descriptors/ArgumentDescriptorBase~1.cs | 38 ++++++------------- .../Core/src/Types/Types/DirectiveType.cs | 16 +------- .../Types.Tests/Types/InputObjectTypeTests.cs | 8 ++++ ...tTypeTests.Input_Infer_Default_Values.snap | 6 +++ 5 files changed, 61 insertions(+), 41 deletions(-) diff --git a/src/HotChocolate/Core/src/Types/Internal/TypeDependencyHelper.cs b/src/HotChocolate/Core/src/Types/Internal/TypeDependencyHelper.cs index 3727e5cc2c7..bfc816623f5 100644 --- a/src/HotChocolate/Core/src/Types/Internal/TypeDependencyHelper.cs +++ b/src/HotChocolate/Core/src/Types/Internal/TypeDependencyHelper.cs @@ -112,7 +112,15 @@ public static void CollectDependencies( if (field.Type is not null) { - dependencies.Add(new(field.Type)); + var hasDefaultValue = + field.DefaultValue is not null || + field.RuntimeDefaultValue is not null; + + TypeDependencyKind kind = hasDefaultValue + ? TypeDependencyKind.Completed + : TypeDependencyKind.Default; + + dependencies.Add(new(field.Type, kind)); } CollectDirectiveDependencies(field, dependencies); @@ -159,6 +167,30 @@ public static void CollectDependencies( CollectDirectiveDependencies(definition, dependencies); } + public static void CollectDependencies( + DirectiveTypeDefinition definition, + ICollection dependencies) + { + if (definition.HasArguments) + { + foreach (DirectiveArgumentDefinition argument in definition.Arguments) + { + if (argument.Type is not null) + { + var hasDefaultValue = + argument.DefaultValue is not null || + argument.RuntimeDefaultValue is not null; + + TypeDependencyKind kind = hasDefaultValue + ? TypeDependencyKind.Completed + : TypeDependencyKind.Default; + + dependencies.Add(new(argument.Type, TypeDependencyKind.Completed)); + } + } + } + } + internal static void CollectDirectiveDependencies( TypeDefinitionBase definition, ICollection dependencies) diff --git a/src/HotChocolate/Core/src/Types/Types/Descriptors/ArgumentDescriptorBase~1.cs b/src/HotChocolate/Core/src/Types/Types/Descriptors/ArgumentDescriptorBase~1.cs index 6da0c093e3b..10482145d75 100644 --- a/src/HotChocolate/Core/src/Types/Types/Descriptors/ArgumentDescriptorBase~1.cs +++ b/src/HotChocolate/Core/src/Types/Types/Descriptors/ArgumentDescriptorBase~1.cs @@ -1,4 +1,5 @@ using System; +using HotChocolate.Internal; using HotChocolate.Language; using HotChocolate.Properties; using HotChocolate.Types.Descriptors.Definitions; @@ -6,9 +7,7 @@ namespace HotChocolate.Types.Descriptors { - public class ArgumentDescriptorBase - : DescriptorBase - where T : ArgumentDefinition, new() + public class ArgumentDescriptorBase : DescriptorBase where T : ArgumentDefinition, new() { protected ArgumentDescriptorBase(IDescriptorContext context) : base(context) @@ -18,8 +17,7 @@ protected ArgumentDescriptorBase(IDescriptorContext context) protected internal override T Definition { get; protected set; } - protected void SyntaxNode( - InputValueDefinitionNode inputValueDefinition) + protected void SyntaxNode(InputValueDefinitionNode inputValueDefinition) { Definition.SyntaxNode = inputValueDefinition; } @@ -29,20 +27,18 @@ protected void Description(string value) Definition.Description = value; } - public void Type() - where TInputType : IInputType + public void Type() where TInputType : IInputType { Type(typeof(TInputType)); } public void Type(Type type) { - var typeInfo = Context.TypeInspector.CreateTypeInfo(type); + ITypeInfo typeInfo = Context.TypeInspector.CreateTypeInfo(type); if (typeInfo.IsSchemaType && !typeInfo.IsInputType()) { - throw new ArgumentException( - TypeResources.ArgumentDescriptor_InputTypeViolation); + throw new ArgumentException(TypeResources.ArgumentDescriptor_InputTypeViolation); } Definition.SetMoreSpecificType(typeInfo.GetExtendedType(), TypeContext.Input); @@ -110,23 +106,13 @@ public void DefaultValue(object value) } } - public void Directive(TDirective directiveInstance) - where TDirective : class - { - Definition.AddDirective(directiveInstance, Context.TypeInspector); - } + public void Directive(TDirective directiveInstance) where TDirective : class + => Definition.AddDirective(directiveInstance, Context.TypeInspector); - public void Directive() - where TDirective : class, new() - { - Definition.AddDirective(new TDirective(), Context.TypeInspector); - } + public void Directive() where TDirective : class, new() + => Definition.AddDirective(new TDirective(), Context.TypeInspector); - public void Directive( - NameString name, - params ArgumentNode[] arguments) - { - Definition.AddDirective(name, arguments); - } + public void Directive(NameString name, params ArgumentNode[] arguments) + => Definition.AddDirective(name, arguments); } } diff --git a/src/HotChocolate/Core/src/Types/Types/DirectiveType.cs b/src/HotChocolate/Core/src/Types/Types/DirectiveType.cs index de7108eb585..de1e8e67419 100644 --- a/src/HotChocolate/Core/src/Types/Types/DirectiveType.cs +++ b/src/HotChocolate/Core/src/Types/Types/DirectiveType.cs @@ -3,6 +3,7 @@ using System.Globalization; using System.Linq; using HotChocolate.Configuration; +using HotChocolate.Internal; using HotChocolate.Language; using HotChocolate.Properties; using HotChocolate.Resolvers; @@ -196,20 +197,7 @@ protected override void OnRegisterDependencies( : typeof(object); IsRepeatable = definition.IsRepeatable; - RegisterDependencies(context, definition); - } - - private static void RegisterDependencies( - ITypeDiscoveryContext context, - DirectiveTypeDefinition definition) - { - if (definition.HasArguments) - { - foreach (var argument in definition.Arguments) - { - context.Dependencies.Add(new(argument.Type, TypeDependencyKind.Completed)); - } - } + TypeDependencyHelper.CollectDependencies(definition, context.Dependencies); } protected override void OnCompleteType( diff --git a/src/HotChocolate/Core/test/Types.Tests/Types/InputObjectTypeTests.cs b/src/HotChocolate/Core/test/Types.Tests/Types/InputObjectTypeTests.cs index f2a021f3f8f..6909e8977cd 100644 --- a/src/HotChocolate/Core/test/Types.Tests/Types/InputObjectTypeTests.cs +++ b/src/HotChocolate/Core/test/Types.Tests/Types/InputObjectTypeTests.cs @@ -772,7 +772,15 @@ public class InputWithDefault [DefaultValue(null)] public string WithNullDefault { get; set; } + [DefaultValue(FooEnum.Bar)] + public FooEnum Enum { get; set; } + public string WithoutDefault { get; set; } } + + public enum FooEnum + { + Bar, Baz + } } } diff --git a/src/HotChocolate/Core/test/Types.Tests/Types/__snapshots__/InputObjectTypeTests.Input_Infer_Default_Values.snap b/src/HotChocolate/Core/test/Types.Tests/Types/__snapshots__/InputObjectTypeTests.Input_Infer_Default_Values.snap index 0879b3afbec..6c4f0f64ff0 100644 --- a/src/HotChocolate/Core/test/Types.Tests/Types/__snapshots__/InputObjectTypeTests.Input_Infer_Default_Values.snap +++ b/src/HotChocolate/Core/test/Types.Tests/Types/__snapshots__/InputObjectTypeTests.Input_Infer_Default_Values.snap @@ -9,9 +9,15 @@ type Query { input InputWithDefaultInput { withStringDefault: String = "abc" withNullDefault: String + enum: FooEnum! = BAR withoutDefault: String } +enum FooEnum { + BAR + BAZ +} + "The `@defer` directive may be provided for fragment spreads and inline fragments to inform the executor to delay the execution of the current fragment to indicate deprioritization of the current fragment. A query with `@defer` directive will cause the request to potentially return multiple responses, where non-deferred data is delivered in the initial response and data deferred is delivered in a subsequent response. `@include` and `@skip` take precedence over `@defer`." directive @defer("If this argument label has a value other than null, it will be passed on to the result of this defer directive. This label is intended to give client applications a way to identify to which fragment a deferred result belongs to." label: String "Deferred when true." if: Boolean) on FRAGMENT_SPREAD | INLINE_FRAGMENT