Skip to content

Commit

Permalink
Fixed: Ignore doesn't work with InputObjectTyp (#373)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib authored Nov 29, 2018
1 parent 751c7d3 commit 3013699
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 74 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using HotChocolate.Configuration;
using HotChocolate.Language;
using Xunit;

Expand Down Expand Up @@ -160,7 +161,8 @@ public void OverwriteNativeDefaultValueWithDefaultValueLiteral()
// assert
InputFieldDescription description = descriptor.CreateDescription();
Assert.IsType<StringValueNode>(description.DefaultValue);
Assert.Equal("123", ((StringValueNode)description.DefaultValue).Value);
Assert.Equal("123",
((StringValueNode)description.DefaultValue).Value);
Assert.Null(description.NativeDefaultValue);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Xunit;

namespace HotChocolate.Types
{
public class InputObjectTypeDescriptorDescriptors
{
[Fact]
public void Field_Ignore_PropertyIsExcluded()
{
// arrange
var descriptor = new InputObjectTypeDescriptor<SimpleInput>();
IInputObjectTypeDescriptor<SimpleInput> descriptorItf = descriptor;

// act
descriptorItf.Field(t => t.Id).Ignore();

// assert
InputObjectTypeDescription description =
descriptor.CreateDescription();

Assert.Collection(description.Fields,
t => Assert.Equal("name", t.Name));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace HotChocolate.Types
{
public class FieldDescriptorTests
public class ObjectFieldDescriptorTests
{
[Fact]
public void DotNetTypesDoNotOverwriteSchemaTypes()
Expand Down
30 changes: 30 additions & 0 deletions src/Types.Tests/Types/InputObjectTypeTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using ChilliCream.Testing;
using HotChocolate.Configuration;
using HotChocolate.Language;
using HotChocolate.Utilities;
using Xunit;
Expand All @@ -8,6 +9,29 @@ namespace HotChocolate.Types
{
public class InputObjectTypeTests
{
[Fact]
public void Initialize_IgnoreProperty_PropertyIsNotInSchemaType()
{
// arrange
var errors = new List<SchemaError>();
var schemaContext = new SchemaContext();

// act
var fooType = new InputObjectType<SimpleInput>(
d => d.Field(f => f.Id).Ignore());
INeedsInitialization init = fooType;

// assert
var initializationContext = new TypeInitializationContext(
schemaContext, a => errors.Add(a), fooType, false);
init.RegisterDependencies(initializationContext);
schemaContext.CompleteTypes();

Assert.Empty(errors);
Assert.Collection(fooType.Fields,
t => Assert.Equal("name", t.Name));
}

[Fact]
public void ParseLiteral()
{
Expand Down Expand Up @@ -123,6 +147,12 @@ public Schema Create()
}
}

public class SimpleInput
{
public int Id { get; set; }
public string Name { get; set; }
}

public class SerializationInputObject1
{
public SerializationInputObject2 Foo { get; set; }
Expand Down
120 changes: 66 additions & 54 deletions src/Types/Types/Descriptors/InputObjectTypeDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,36 @@ public InputObjectTypeDescription CreateDescription()
return ObjectDescription;
}

protected virtual void CompleteFields()
private void CompleteFields()
{
var fields = new Dictionary<string, InputFieldDescription>();
var handledProperties = new HashSet<PropertyInfo>();

foreach (InputFieldDescriptor fieldDescriptor in Fields)
{
ObjectDescription.Fields.Add(
fieldDescriptor.CreateDescription());
InputFieldDescription fieldDescription = fieldDescriptor
.CreateDescription();

if (!fieldDescription.Ignored)
{
fields[fieldDescription.Name] = fieldDescription;
}

if (fieldDescription.Property != null)
{
handledProperties.Add(fieldDescription.Property);
}
}

OnCompleteFields(fields, handledProperties);

ObjectDescription.Fields.AddRange(fields.Values);
}

protected virtual void OnCompleteFields(
IDictionary<string, InputFieldDescription> fields,
ISet<PropertyInfo> handledProperties)
{
}

protected void SyntaxNode(InputObjectTypeDefinitionNode syntaxNode)
Expand Down Expand Up @@ -92,10 +115,10 @@ internal class InputObjectTypeDescriptor<T>
: InputObjectTypeDescriptor
, IInputObjectTypeDescriptor<T>
{
public InputObjectTypeDescriptor(Type clrType)
public InputObjectTypeDescriptor()
{
ObjectDescription.ClrType = clrType
?? throw new ArgumentNullException(nameof(clrType));
Type clrType = typeof(T);
ObjectDescription.ClrType = clrType;
ObjectDescription.Name = clrType.GetGraphQLName();
ObjectDescription.Description = clrType.GetGraphQLDescription();

Expand All @@ -109,24 +132,50 @@ public InputObjectTypeDescriptor(Type clrType)
}
}

protected override void CompleteFields()
protected override void OnCompleteFields(
IDictionary<string, InputFieldDescription> fields,
ISet<PropertyInfo> handledProperties)
{
base.CompleteFields();

var descriptions = new Dictionary<string, InputFieldDescription>();
if (ObjectDescription.FieldBindingBehavior ==
BindingBehavior.Implicit)
{
AddImplicitFields(fields, handledProperties);
}
}

foreach (InputFieldDescription description in
ObjectDescription.Fields)
private void AddImplicitFields(
IDictionary<string, InputFieldDescription> fields,
ISet<PropertyInfo> handledProperties)
{
foreach (KeyValuePair<PropertyInfo, string> property in
GetProperties(handledProperties))
{
descriptions[description.Name] = description;
if (!fields.ContainsKey(property.Value))
{
var fieldDescriptor =
new InputFieldDescriptor(property.Key);

fields[property.Value] = fieldDescriptor
.CreateDescription();
}
}
}

if (ObjectDescription.FieldBindingBehavior ==
BindingBehavior.Implicit)
private Dictionary<PropertyInfo, string> GetProperties(
ISet<PropertyInfo> handledProperties)
{
var properties = new Dictionary<PropertyInfo, string>();

foreach (KeyValuePair<string, PropertyInfo> property in
ReflectionUtils.GetProperties(ObjectDescription.ClrType))
{
DeriveFieldsFromType(descriptions);
ObjectDescription.Fields = descriptions.Values.ToList();
if (!handledProperties.Contains(property.Value))
{
properties[property.Value] = property.Key;
}
}

return properties;
}

protected void BindFields(BindingBehavior bindingBehavior)
Expand All @@ -149,43 +198,6 @@ protected InputFieldDescriptor Field<TValue>(
nameof(property));
}

private void DeriveFieldsFromType(
Dictionary<string, InputFieldDescription> descriptions)
{
Dictionary<PropertyInfo, string> properties =
GetProperties(ObjectDescription.ClrType);

foreach (InputFieldDescription description in descriptions.Values
.Where(t => t.Property != null))
{
properties.Remove(description.Property);
}

foreach (KeyValuePair<PropertyInfo, string> property in properties)
{
if (!descriptions.ContainsKey(property.Value))
{
var descriptor = new InputFieldDescriptor(property.Key);
descriptions[property.Value] = descriptor
.CreateDescription();
}
}
}

private static Dictionary<PropertyInfo, string> GetProperties(Type type)
{
var properties = new Dictionary<PropertyInfo, string>();

foreach (PropertyInfo property in type.GetProperties(
BindingFlags.Instance | BindingFlags.Public)
.Where(t => t.DeclaringType != typeof(object)))
{
properties[property] = property.GetGraphQLName();
}

return properties;
}

#region IInputObjectTypeDescriptor<T>

IInputObjectTypeDescriptor<T> IInputObjectTypeDescriptor<T>.SyntaxNode(
Expand Down
34 changes: 17 additions & 17 deletions src/Types/Types/Descriptors/ObjectTypeDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -378,23 +378,6 @@ protected override void OnCompleteFields(
AddResolverTypes(fields);
}

private Dictionary<MemberInfo, string> GetAllMembers(
ISet<MemberInfo> handledMembers)
{
var members = new Dictionary<MemberInfo, string>();

foreach (KeyValuePair<string, MemberInfo> member in
ReflectionUtils.GetMembers(ObjectDescription.ClrType))
{
if (!handledMembers.Contains(member.Value))
{
members[member.Value] = member.Key;
}
}

return members;
}

private void AddImplicitFields(
IDictionary<string, ObjectFieldDescription> fields,
ISet<MemberInfo> handledMembers)
Expand All @@ -414,6 +397,23 @@ private void AddImplicitFields(
}
}

private Dictionary<MemberInfo, string> GetAllMembers(
ISet<MemberInfo> handledMembers)
{
var members = new Dictionary<MemberInfo, string>();

foreach (KeyValuePair<string, MemberInfo> member in
ReflectionUtils.GetMembers(ObjectDescription.ClrType))
{
if (!handledMembers.Contains(member.Value))
{
members[member.Value] = member.Key;
}
}

return members;
}

#region IObjectTypeDescriptor<T>

IObjectTypeDescriptor<T> IObjectTypeDescriptor<T>.Name(NameString name)
Expand Down
2 changes: 1 addition & 1 deletion src/Types/Types/InputObjectType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public InputObjectType(Action<IInputObjectTypeDescriptor<T>> configure)
#region Configuration

internal sealed override InputObjectTypeDescriptor CreateDescriptor() =>
new InputObjectTypeDescriptor<T>(typeof(T));
new InputObjectTypeDescriptor<T>();

protected sealed override void Configure(IInputObjectTypeDescriptor descriptor)
{
Expand Down

0 comments on commit 3013699

Please sign in to comment.