Skip to content

Commit

Permalink
Fixed default values of enums (#4129)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Staib <michael@chillicream.com>
  • Loading branch information
PascalSenn and michaelstaib authored Aug 27, 2021
1 parent ebf2f1c commit 49bec1b
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 41 deletions.
34 changes: 33 additions & 1 deletion src/HotChocolate/Core/src/Types/Internal/TypeDependencyHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -159,6 +167,30 @@ public static void CollectDependencies(
CollectDirectiveDependencies(definition, dependencies);
}

public static void CollectDependencies(
DirectiveTypeDefinition definition,
ICollection<TypeDependency> 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<T>(
TypeDefinitionBase<T> definition,
ICollection<TypeDependency> dependencies)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using System;
using HotChocolate.Internal;
using HotChocolate.Language;
using HotChocolate.Properties;
using HotChocolate.Types.Descriptors.Definitions;
using HotChocolate.Types.Helpers;

namespace HotChocolate.Types.Descriptors
{
public class ArgumentDescriptorBase<T>
: DescriptorBase<T>
where T : ArgumentDefinition, new()
public class ArgumentDescriptorBase<T> : DescriptorBase<T> where T : ArgumentDefinition, new()
{
protected ArgumentDescriptorBase(IDescriptorContext context)
: base(context)
Expand All @@ -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;
}
Expand All @@ -29,20 +27,18 @@ protected void Description(string value)
Definition.Description = value;
}

public void Type<TInputType>()
where TInputType : IInputType
public void Type<TInputType>() 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);
Expand Down Expand Up @@ -110,23 +106,13 @@ public void DefaultValue(object value)
}
}

public void Directive<TDirective>(TDirective directiveInstance)
where TDirective : class
{
Definition.AddDirective(directiveInstance, Context.TypeInspector);
}
public void Directive<TDirective>(TDirective directiveInstance) where TDirective : class
=> Definition.AddDirective(directiveInstance, Context.TypeInspector);

public void Directive<TDirective>()
where TDirective : class, new()
{
Definition.AddDirective(new TDirective(), Context.TypeInspector);
}
public void Directive<TDirective>() 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);
}
}
16 changes: 2 additions & 14 deletions src/HotChocolate/Core/src/Types/Types/DirectiveType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 49bec1b

Please sign in to comment.