Skip to content

Commit

Permalink
Refine Attribute Support (#1306)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib authored Dec 19, 2019
1 parent 3d8fa25 commit b4c04a5
Show file tree
Hide file tree
Showing 48 changed files with 926 additions and 105 deletions.
8 changes: 6 additions & 2 deletions src/Core/Types.Filters/UseFilteringAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Linq;
using System.Reflection;
using HotChocolate.Types.Descriptors;

namespace HotChocolate.Types
{
Expand All @@ -21,7 +22,10 @@ public sealed class UseFilteringAttribute : ObjectFieldDescriptorAttribute
/// <value>The filter type</value>
public Type FilterType { get; set; }

public override void OnConfigure(IObjectFieldDescriptor descriptor)
public override void OnConfigure(
IDescriptorContext context,
IObjectFieldDescriptor descriptor,
MemberInfo member)
{
if (FilterType is null)
{
Expand All @@ -33,4 +37,4 @@ public override void OnConfigure(IObjectFieldDescriptor descriptor)
}
}
}
}
}
8 changes: 6 additions & 2 deletions src/Core/Types.Sorting/UseSortingAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Linq;
using System.Reflection;
using HotChocolate.Types.Descriptors;

namespace HotChocolate.Types
{
Expand All @@ -21,7 +22,10 @@ public sealed class UseSortingAttribute : ObjectFieldDescriptorAttribute
/// <value>The sort type</value>
public Type SortType { get; set; }

public override void OnConfigure(IObjectFieldDescriptor descriptor)
public override void OnConfigure(
IDescriptorContext context,
IObjectFieldDescriptor descriptor,
MemberInfo member)
{
if (SortType is null)
{
Expand All @@ -33,4 +37,4 @@ public override void OnConfigure(IObjectFieldDescriptor descriptor)
}
}
}
}
}
34 changes: 32 additions & 2 deletions src/Core/Types.Tests/Types/EnumTypeDescriptorAttributeTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
using System.Linq;
using System.Reflection;
using HotChocolate.Types.Descriptors;
using Xunit;

namespace HotChocolate.Types
Expand Down Expand Up @@ -32,6 +35,19 @@ public void Change_Type_Name_With_Attribute()
Assert.NotNull(schema.GetType<EnumType>("Abc"));
}

[Fact]
public void Annotated_Enum3_With_EnumTypeAttribute()
{
// act
ISchema schema = SchemaBuilder.New()
.AddEnumType<Enum3>()
.ModifyOptions(o => o.StrictValidation = false)
.Create();

// assert
Assert.NotNull(schema.GetType<EnumType>("Foo"));
}

public enum Enum1
{

Expand All @@ -43,7 +59,10 @@ public enum Enum1
public class RenameValueAttribute
: EnumValueDescriptorAttribute
{
public override void OnConfigure(IEnumValueDescriptor descriptor)
public override void OnConfigure(
IDescriptorContext context,
IEnumValueDescriptor descriptor,
FieldInfo field)
{
descriptor.Name("ABC");
}
Expand All @@ -57,10 +76,21 @@ public enum Enum2
Value2
}

[EnumType(Name = "Foo")]
public enum Enum3
{

Value1,
Value2
}

public class RenameTypeAttribute
: EnumTypeDescriptorAttribute
{
public override void OnConfigure(IEnumTypeDescriptor descriptor)
public override void OnConfigure(
IDescriptorContext context,
IEnumTypeDescriptor descriptor,
Type type)
{
descriptor.Name("Abc");
}
Expand Down
35 changes: 33 additions & 2 deletions src/Core/Types.Tests/Types/InputObjectTypeAttributeTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using System;
using System.Reflection;
using HotChocolate.Types.Descriptors;
using Xunit;

namespace HotChocolate.Types
Expand Down Expand Up @@ -36,6 +39,22 @@ public void Change_InputObjectType_Name_With_Attribute()
.ContainsField("foo"));
}

[Fact]
public void Annotated_Struct1_With_InputObjectTypeAttribute()
{
// act
ISchema schema = SchemaBuilder.New()
.AddInputObjectType<Struct1>()
.ModifyOptions(o => o.StrictValidation = false)
.Create();

// assert
Assert.True(
schema.GetType<InputObjectType>("Foo")
.Fields
.ContainsField("foo"));
}

public class Object1
{
[RenameField]
Expand All @@ -45,7 +64,10 @@ public class Object1
public class RenameFieldAttribute
: InputFieldDescriptorAttribute
{
public override void OnConfigure(IInputFieldDescriptor descriptor)
public override void OnConfigure(
IDescriptorContext context,
IInputFieldDescriptor descriptor,
MemberInfo member)
{
descriptor.Name("bar");
}
Expand All @@ -57,10 +79,19 @@ public class Object2
public string Foo { get; set; }
}

[InputObjectType(Name = "Foo")]
public struct Struct1
{
public string Foo { get; set; }
}

public class RenameTypeAttribute
: InputObjectTypeDescriptorAttribute
{
public override void OnConfigure(IInputObjectTypeDescriptor descriptor)
public override void OnConfigure(
IDescriptorContext context,
IInputObjectTypeDescriptor descriptor,
Type type)
{
descriptor.Name("Bar");
}
Expand Down
38 changes: 35 additions & 3 deletions src/Core/Types.Tests/Types/InterfaceTypeAttributeTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Reflection;
using HotChocolate.Types.Descriptors;
using Xunit;

#nullable enable
Expand Down Expand Up @@ -77,6 +79,21 @@ public void InterfaceTypeDescriptorAttribute_Add_FieldDefinition()
.Fields.ContainsField("abc"));
}

[Fact]
public void Annotated_Class_With_InterfaceTypeAttribute()
{
// act
ISchema schema = SchemaBuilder.New()
.AddInterfaceType<Object1>()
.ModifyOptions(o => o.StrictValidation = false)
.Create();

// assert
Assert.True(
schema.GetType<InterfaceType>("Foo")
.Fields.ContainsField("bar"));
}

public interface Interface1
{
string GetField([ArgumentDefaultValue("abc")]string argument);
Expand All @@ -92,7 +109,10 @@ public ArgumentDefaultValueAttribute(object defaultValue)

public object DefaultValue { get; }

public override void OnConfigure(IArgumentDescriptor descriptor)
public override void OnConfigure(
IDescriptorContext context,
IArgumentDescriptor descriptor,
ParameterInfo parameter)
{
descriptor.DefaultValue(DefaultValue);
}
Expand All @@ -107,7 +127,10 @@ public interface Interface2
public class PropertyAddContextDataAttribute
: InterfaceFieldDescriptorAttribute
{
public override void OnConfigure(IInterfaceFieldDescriptor descriptor)
public override void OnConfigure(
IDescriptorContext context,
IInterfaceFieldDescriptor descriptor,
MemberInfo member)
{
descriptor.Extend().OnBeforeCompletion(
(c, d) => d.ContextData.Add("abc", "def"));
Expand All @@ -123,10 +146,19 @@ public interface Interface3
public class InterfaceAddFieldAttribute
: InterfaceTypeDescriptorAttribute
{
public override void OnConfigure(IInterfaceTypeDescriptor descriptor)
public override void OnConfigure(
IDescriptorContext context,
IInterfaceTypeDescriptor descriptor,
Type type)
{
descriptor.Field("abc").Type<StringType>();
}
}

[InterfaceType(Name = "Foo")]
public class Object1
{
public string? Bar { get; set; }
}
}
}
57 changes: 54 additions & 3 deletions src/Core/Types.Tests/Types/ObjectTypeAttributeTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
using System.Reflection;
using HotChocolate.Types.Descriptors;
using Snapshooter.Xunit;
using Xunit;

#nullable enable
Expand Down Expand Up @@ -76,6 +79,33 @@ public void ObjectTypeDescriptorAttribute_Add_FieldDefinition_2()
Assert.True(schema.GetType<ObjectType>("Object3").Fields.ContainsField("abc"));
}

[Fact]
public void ObjectTypeAttribute_Mark_Struct_As_ObjectType()
{
// act
ISchema schema = SchemaBuilder.New()
.AddType<StructQuery>()
.ModifyOptions(o => o.RemoveUnreachableTypes = true)
.Create();

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

[Fact]
public void ExtendObjectTypeAttribute_Extend_Query_Type()
{
// act
ISchema schema = SchemaBuilder.New()
.AddType<StructQuery>()
.AddType<StructQueryExtension>()
.ModifyOptions(o => o.RemoveUnreachableTypes = true)
.Create();

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

public class Object1
{
public string GetField([ArgumentDefaultValue("abc")]string argument)
Expand All @@ -94,7 +124,10 @@ public ArgumentDefaultValueAttribute(object defaultValue)

public object DefaultValue { get; }

public override void OnConfigure(IArgumentDescriptor descriptor)
public override void OnConfigure(
IDescriptorContext context,
IArgumentDescriptor descriptor,
ParameterInfo parameterInfo)
{
descriptor.DefaultValue(DefaultValue);
}
Expand All @@ -112,7 +145,10 @@ public string GetField()
public class PropertyAddContextDataAttribute
: ObjectFieldDescriptorAttribute
{
public override void OnConfigure(IObjectFieldDescriptor descriptor)
public override void OnConfigure(
IDescriptorContext context,
IObjectFieldDescriptor descriptor,
MemberInfo member)
{
descriptor.Extend().OnBeforeCompletion(
(c, d) => d.ContextData.Add("abc", "def"));
Expand All @@ -131,10 +167,25 @@ public string GetField()
public class ObjectAddFieldAttribute
: ObjectTypeDescriptorAttribute
{
public override void OnConfigure(IObjectTypeDescriptor descriptor)
public override void OnConfigure(
IDescriptorContext context,
IObjectTypeDescriptor descriptor,
Type type)
{
descriptor.Field("abc").Resolver<string>("def");
}
}

[ObjectType(Name = "Query")]
public struct StructQuery
{
public string? Foo { get; }
}

[ExtendObjectType(Name = "Query")]
public class StructQueryExtension
{
public string? Bar { get; }
}
}
}
Loading

0 comments on commit b4c04a5

Please sign in to comment.