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

Consistently resolve ignore flags in derived types #265

Merged
merged 1 commit into from
Jun 3, 2024
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
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; }
}
}
}
Loading