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

Fixed default values of enums #4129

Merged
merged 5 commits into from
Aug 27, 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
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