Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -167,18 +167,33 @@ public static bool HasIdAttribute(this InputFieldConfiguration? definition)
var attributes = member.GetCustomAttributesData();
foreach (var attribute in attributes)
{
if (attribute.AttributeType == typeof(IDAttribute))
if (IsIdAttribute(attribute.AttributeType))
{
return true;
}
}

return false;

if (attribute.AttributeType.IsGenericType
&& attribute.AttributeType.GetGenericTypeDefinition() == typeof(IDAttribute<>))
static bool IsIdAttribute(Type? type)
{
while (type is not null)
{
return true;
if (type == typeof(IDAttribute))
{
return true;
}

if (type.IsGenericType
&& type.GetGenericTypeDefinition() == typeof(IDAttribute<>))
{
return true;
}

type = type.BaseType;
}
}

return false;
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,38 @@ public async Task Filtering_Should_InferType_When_AnnotatedGeneric()
schema.MatchSnapshot();
}

[Fact]
public async Task Filtering_Should_InferType_When_AnnotatedWith_Derived_IDAttribute()
{
var schema = await new ServiceCollection()
.AddGraphQL()
.AddQueryType(x => x.Name("Query").Field("test").Resolve("a"))
.AddType(new FilterInputType<FooIdDerived>())
.AddFiltering()
.BuildSchemaAsync();

var filterType = Assert.IsAssignableFrom<InputObjectType>(schema.Types["FooIdDerivedFilterInput"]);
var fieldType = filterType.Fields["bar"].Type.NamedType();

Assert.Equal("IdOperationFilterInput", fieldType.Name);
}

[Fact]
public async Task Filtering_Should_InferType_When_AnnotatedWith_Derived_Generic_IDAttribute()
{
var schema = await new ServiceCollection()
.AddGraphQL()
.AddQueryType(x => x.Name("Query").Field("test").Resolve("a"))
.AddType(new FilterInputType<FooIdGenericDerived>())
.AddFiltering()
.BuildSchemaAsync();

var filterType = Assert.IsAssignableFrom<InputObjectType>(schema.Types["FooIdGenericDerivedFilterInput"]);
var fieldType = filterType.Fields["bar"].Type.NamedType();

Assert.Equal("IdOperationFilterInput", fieldType.Name);
}

public class Foo
{
public string? Bar { get; }
Expand All @@ -64,4 +96,20 @@ public class FooIdGeneric
[ID<Foo>]
public string? Bar { get; }
}

public class FooIdDerived
{
[InheritedId]
public string? Bar { get; }
}

public class FooIdGenericDerived
{
[InheritedId<Foo>]
public string? Bar { get; }
}

public sealed class InheritedId(string? typeName = null) : IDAttribute(typeName);

public sealed class InheritedId<T> : IDAttribute<T>;
}
Loading