Skip to content

Commit

Permalink
chore: consistently resolve ignore flags in derived types
Browse files Browse the repository at this point in the history
  • Loading branch information
tlil committed Jun 3, 2024
1 parent 257800e commit 3daa111
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/GraphQL.Conventions/Types/Resolution/ObjectReflector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,17 +250,27 @@ private IEnumerable<GraphFieldInfo> GetFields(TypeInfo typeInfo)
.ImplementedInterfaces
.SelectMany(iface => iface.GetMethods(DefaultBindingFlags));

return typeInfo
var fields = typeInfo
.GetMethods(DefaultBindingFlags)
.Union(implementedMethods)
.Union(GetExtensionMethods(typeInfo))
.Where(IsValidMember)
.Where(methodInfo => !methodInfo.IsSpecialName)
.Cast<MemberInfo>()
.Union(properties)
.Select(DeriveField)
.Union(properties);

var deduped = fields
.GroupBy(prop => prop.Name)
.Select(g => g.First());

var derivedFields = deduped
.Select(DeriveField);

var filteredFields = derivedFields
.Where(field => !field.IsIgnored)
.OrderBy(field => field.Name);

return filteredFields;
}

private IEnumerable<GraphArgumentInfo> GetArguments(MethodInfo methodInfo)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using System.Linq;
using GraphQL.Conventions;
using GraphQL.Conventions.Adapters;
using GraphQL.Conventions.Builders;
using GraphQL.Types;
using Tests.Templates;
using Tests.Templates.Extensions;
// ReSharper disable UnusedMember.Local
Expand All @@ -24,6 +28,44 @@ public void Enum_Members_Can_Be_Ignored()
type.ShouldHaveFieldWithName("DEPRECATED_MEMBER").AndWithDeprecationReason("Some enum member reason");
}

[Test]
public void Derived_Fields_Can_Be_Ignored()
{
var type = TypeInfo<Implementation>();
type.Fields.Count.ShouldEqual(2);
type.ShouldHaveFieldWithName("firstProperty");
type.ShouldHaveFieldWithName("someOtherProperty");
type.ShouldNotHaveFieldWithName("someProperty");

var iface = TypeInfo<ISomeInterface>();
iface.Fields.Count.ShouldEqual(1);
iface.ShouldHaveFieldWithName("firstProperty");
iface.ShouldNotHaveFieldWithName("someProperty");
}

[Test]
public void Can_Ignore_Unwanted_Fields_On_Derived_Types()
{
var schema = new SchemaConstructor<ISchema, IGraphType>(new GraphTypeAdapter())
.IgnoreTypes((t, m) => m?.Name == nameof(ISomeInterfaceExternal.SomeProperty))
.Build(typeof(SchemaType));

schema.Initialize();
var type = schema.AllTypes[nameof(ImplementationExternal)] as ObjectGraphType;
Assert.AreEqual(1, type.Fields.Count(f => f.Name == "someOtherProperty"));
Assert.AreEqual(0, type.Fields.Count(f => f.Name == "someProperty"));
}

private class SchemaType
{
public QueryType Query { get; }
}

private class QueryType
{
public ImplementationExternal Foo => new();
}

private class FieldData
{
public int NormalField { get; set; }
Expand All @@ -42,5 +84,39 @@ public enum Enum
DeprecatedMember,
}
}

private class Implementation : ISomeInterface
{
public string FirstProperty { get; set; }

[Ignore]
public string SomeProperty { get; set; }

public string SomeOtherProperty { get; set; }
}

private interface ISomeInterface
{
public string FirstProperty { get; set; }

[Ignore]
public string SomeProperty { get; set; }
}

private class ImplementationExternal : ISomeInterfaceExternal
{
public string FirstProperty { get; set; }

public string SomeProperty { get; set; }

public string SomeOtherProperty { get; set; }
}

private interface ISomeInterfaceExternal
{
public string FirstProperty { get; set; }

public string SomeProperty { get; set; }
}
}
}

0 comments on commit 3daa111

Please sign in to comment.