Skip to content

Commit b765e1f

Browse files
authored
Adds support for generic ID Attribute (#7086)
1 parent 4f56e59 commit b765e1f

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

Diff for: src/HotChocolate/Data/src/Data/Filters/Convention/FilterTypeInterceptor.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ public override void OnBeforeRegisterDependencies(
4040
foreach (var field in def.Fields)
4141
{
4242
if (field is FilterFieldDefinition filterField &&
43-
filterField.Member?.GetCustomAttribute(typeof(IDAttribute)) != null)
43+
filterField.Member is { } member &&
44+
(member.GetCustomAttribute(typeof(IDAttribute)) is { } ||
45+
member.GetCustomAttribute(typeof(IDAttribute<>)) is { }))
4446
{
4547
filterField.Type = discoveryContext.TypeInspector.GetTypeRef(
4648
typeof(IdOperationFilterInputType),

Diff for: src/HotChocolate/Data/test/Data.Filters.Tests/IdFilterTypeInterceptorTests.cs

+19
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,19 @@ public async Task Filtering_Should_InfereType_When_Annotated()
3636
schema.MatchSnapshot();
3737
}
3838

39+
[Fact]
40+
public async Task Filtering_Should_InferType_When_AnnotatedGeneric()
41+
{
42+
var schema = await new ServiceCollection()
43+
.AddGraphQL()
44+
.AddQueryType(x => x.Name("Query").Field("test").Resolve("a"))
45+
.AddType(new FilterInputType<FooIdGeneric>())
46+
.AddFiltering()
47+
.BuildSchemaAsync();
48+
49+
schema.MatchSnapshot();
50+
}
51+
3952
public class Foo
4053
{
4154
public string? Bar { get; }
@@ -46,4 +59,10 @@ public class FooId
4659
[ID]
4760
public string? Bar { get; }
4861
}
62+
63+
public class FooIdGeneric
64+
{
65+
[ID<Foo>]
66+
public string? Bar { get; }
67+
}
4968
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
schema {
2+
query: Query
3+
}
4+
5+
type Query {
6+
test: String
7+
}
8+
9+
input FooIdGenericFilterInput {
10+
and: [FooIdGenericFilterInput!]
11+
or: [FooIdGenericFilterInput!]
12+
bar: IdOperationFilterInput
13+
}
14+
15+
input IdOperationFilterInput {
16+
eq: ID
17+
neq: ID
18+
in: [ID]
19+
nin: [ID]
20+
}

0 commit comments

Comments
 (0)