Skip to content

Commit 86a9c81

Browse files
authored
Added a generic version of the ID descriptor method (#7503)
1 parent 114ad6f commit 86a9c81

File tree

4 files changed

+97
-15
lines changed

4 files changed

+97
-15
lines changed

src/HotChocolate/Core/src/Types/Types/Relay/Extensions/RelayIdFieldExtensions.cs

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ namespace HotChocolate.Types;
6161
public static class RelayIdFieldExtensions
6262
{
6363
/// <inheritdoc cref="RelayIdFieldExtensions"/>
64-
/// <param name="descriptor">the the descriptor</param>
64+
/// <param name="descriptor">the descriptor</param>
6565
/// <param name="typeName">
6666
/// Sets the <see cref="IDAttribute.TypeName">type name</see> of the relay id
6767
/// </param>
@@ -80,7 +80,24 @@ public static IInputFieldDescriptor ID(
8080
}
8181

8282
/// <inheritdoc cref="RelayIdFieldExtensions"/>
83-
/// <param name="descriptor">the the descriptor</param>
83+
/// <param name="descriptor">the descriptor</param>
84+
/// <typeparam name="T">
85+
/// the type from which the <see cref="IDAttribute.TypeName">type name</see> is derived
86+
/// </typeparam>
87+
public static IInputFieldDescriptor ID<T>(this IInputFieldDescriptor descriptor)
88+
{
89+
if (descriptor is null)
90+
{
91+
throw new ArgumentNullException(nameof(descriptor));
92+
}
93+
94+
RelayIdFieldHelpers.ApplyIdToField(descriptor, typeof(T).Name);
95+
96+
return descriptor;
97+
}
98+
99+
/// <inheritdoc cref="RelayIdFieldExtensions"/>
100+
/// <param name="descriptor">the descriptor</param>
84101
/// <param name="typeName">
85102
/// Sets the <see cref="IDAttribute.TypeName">type name</see> of the relay id
86103
/// </param>
@@ -99,7 +116,24 @@ public static IArgumentDescriptor ID(
99116
}
100117

101118
/// <inheritdoc cref="RelayIdFieldExtensions"/>
102-
/// <param name="descriptor">the the descriptor</param>
119+
/// <param name="descriptor">the descriptor</param>
120+
/// <typeparam name="T">
121+
/// the type from which the <see cref="IDAttribute.TypeName">type name</see> is derived
122+
/// </typeparam>
123+
public static IArgumentDescriptor ID<T>(this IArgumentDescriptor descriptor)
124+
{
125+
if (descriptor is null)
126+
{
127+
throw new ArgumentNullException(nameof(descriptor));
128+
}
129+
130+
RelayIdFieldHelpers.ApplyIdToField(descriptor, typeof(T).Name);
131+
132+
return descriptor;
133+
}
134+
135+
/// <inheritdoc cref="RelayIdFieldExtensions"/>
136+
/// <param name="descriptor">the descriptor</param>
103137
/// <param name="typeName">
104138
/// Sets the <see cref="IDAttribute.TypeName">type name</see> of the relay id
105139
/// </param>
@@ -118,7 +152,24 @@ public static IObjectFieldDescriptor ID(
118152
}
119153

120154
/// <inheritdoc cref="RelayIdFieldExtensions"/>
121-
/// <param name="descriptor">the the descriptor</param>
155+
/// <param name="descriptor">the descriptor</param>
156+
/// <typeparam name="T">
157+
/// the type from which the <see cref="IDAttribute.TypeName">type name</see> is derived
158+
/// </typeparam>
159+
public static IObjectFieldDescriptor ID<T>(this IObjectFieldDescriptor descriptor)
160+
{
161+
if (descriptor is null)
162+
{
163+
throw new ArgumentNullException(nameof(descriptor));
164+
}
165+
166+
RelayIdFieldHelpers.ApplyIdToField(descriptor, typeof(T).Name);
167+
168+
return descriptor;
169+
}
170+
171+
/// <inheritdoc cref="RelayIdFieldExtensions"/>
172+
/// <param name="descriptor">the descriptor</param>
122173
public static IInterfaceFieldDescriptor ID(this IInterfaceFieldDescriptor descriptor)
123174
{
124175
if (descriptor is null)

src/HotChocolate/Core/test/Types.Tests/Types/Relay/IdDescriptorTests.cs

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public async Task Id_On_Arguments()
1212
// arrange
1313
var intId = Convert.ToBase64String("Query:1"u8);
1414
var stringId = Convert.ToBase64String("Query:abc"u8);
15-
var guidId = Convert.ToBase64String(Combine("Query:"u8, Guid.Empty.ToByteArray()));
15+
var guidId = Convert.ToBase64String(Combine("Another:"u8, Guid.Empty.ToByteArray()));
1616

1717
// act
1818
var result =
@@ -47,6 +47,7 @@ public async Task Id_On_Objects()
4747
{
4848
// arrange
4949
var someId = Convert.ToBase64String("Some:1"u8);
50+
var anotherId = Convert.ToBase64String("Another:1"u8);
5051

5152
// act
5253
var result =
@@ -58,19 +59,27 @@ public async Task Id_On_Objects()
5859
.ExecuteRequestAsync(
5960
OperationRequestBuilder.New()
6061
.SetDocument(
61-
@"query foo ($someId: ID!) {
62-
foo(input: { someId: $someId }) {
62+
"""
63+
query foo ($someId: ID!, $anotherId: ID!) {
64+
foo(input: { someId: $someId, anotherId: $anotherId }) {
6365
someId
66+
anotherId
6467
}
65-
}")
66-
.SetVariableValues(new Dictionary<string, object> { { "someId", someId }, })
68+
}
69+
""")
70+
.SetVariableValues(new Dictionary<string, object>
71+
{
72+
{ "someId", someId },
73+
{ "anotherId", anotherId }
74+
})
6775
.Build());
6876

6977
// assert
7078
new
7179
{
7280
result = result.ToJson(),
7381
someId,
82+
anotherId
7483
}.MatchSnapshot();
7584
}
7685

@@ -107,7 +116,7 @@ protected override void Configure(IObjectTypeDescriptor<Query> descriptor)
107116

108117
descriptor
109118
.Field(t => t.GuidId(default))
110-
.Argument("id", a => a.ID());
119+
.Argument("id", a => a.ID<Another>());
111120

112121
descriptor
113122
.Field(t => t.Foo(default))
@@ -123,6 +132,10 @@ protected override void Configure(IInputObjectTypeDescriptor<FooInput> descripto
123132
descriptor
124133
.Field(t => t.SomeId)
125134
.ID("Some");
135+
136+
descriptor
137+
.Field(t => t.AnotherId)
138+
.ID<Another>();
126139
}
127140
}
128141

@@ -135,6 +148,10 @@ protected override void Configure(IObjectTypeDescriptor<FooPayload> descriptor)
135148
descriptor
136149
.Field(t => t.SomeId)
137150
.ID("Bar");
151+
152+
descriptor
153+
.Field(t => t.AnotherId)
154+
.ID<Another>();
138155
}
139156
}
140157

@@ -145,6 +162,10 @@ protected override void Configure(IInterfaceTypeDescriptor<IFooPayload> descript
145162
descriptor
146163
.Field(t => t.SomeId)
147164
.ID();
165+
166+
descriptor
167+
.Field(t => t.AnotherId)
168+
.ID();
148169
}
149170
}
150171

@@ -153,21 +174,27 @@ public class Query
153174
public string IntId(int id) => id.ToString();
154175
public string StringId(string id) => id;
155176
public string GuidId(Guid id) => id.ToString();
156-
public IFooPayload Foo(FooInput input) => new FooPayload { SomeId = input.SomeId, };
177+
public IFooPayload Foo(FooInput input)
178+
=> new FooPayload { SomeId = input.SomeId, AnotherId = input.AnotherId };
157179
}
158180

159181
public class FooInput
160182
{
161183
public string SomeId { get; set; }
184+
public string AnotherId { get; set; }
162185
}
163186

164187
public class FooPayload : IFooPayload
165188
{
166189
public string SomeId { get; set; }
190+
public string AnotherId { get; set; }
167191
}
168192

169193
public interface IFooPayload
170194
{
171195
string SomeId { get; set; }
196+
string AnotherId { get; set; }
172197
}
198+
199+
private class Another;
173200
}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
{
2-
"result": "{\n \"data\": {\n \"foo\": {\n \"someId\": \"QmFyOjE=\"\n }\n }\n}",
3-
"someId": "U29tZTox"
1+
{
2+
"result": "{\n \"data\": {\n \"foo\": {\n \"someId\": \"QmFyOjE=\",\n \"anotherId\": \"QW5vdGhlcjox\"\n }\n }\n}",
3+
"someId": "U29tZTox",
4+
"anotherId": "QW5vdGhlcjox"
45
}

src/HotChocolate/Core/test/Types.Tests/Types/Relay/__snapshots__/IdDescriptorTests.Id_Type_Is_Correctly_Inferred.snap

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
schema {
1+
schema {
22
query: Query
33
}
44

55
interface IFooPayload {
66
someId: ID
7+
anotherId: ID
78
}
89

910
type FooPayload implements IFooPayload {
1011
someId: ID
12+
anotherId: ID
1113
}
1214

1315
type Query {
@@ -19,4 +21,5 @@ type Query {
1921

2022
input FooInput {
2123
someId: ID
24+
anotherId: ID
2225
}

0 commit comments

Comments
 (0)