Skip to content

Commit

Permalink
Added a generic version of the ID descriptor method (#7503)
Browse files Browse the repository at this point in the history
(cherry picked from commit 86a9c81)
  • Loading branch information
glen-84 authored and michaelstaib committed Sep 27, 2024
1 parent 05ebd88 commit ba1c1b8
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ namespace HotChocolate.Types;
public static class RelayIdFieldExtensions
{
/// <inheritdoc cref="RelayIdFieldExtensions"/>
/// <param name="descriptor">the the descriptor</param>
/// <param name="descriptor">the descriptor</param>
/// <param name="typeName">
/// Sets the <see cref="IDAttribute.TypeName">type name</see> of the relay id
/// </param>
Expand All @@ -80,7 +80,24 @@ public static IInputFieldDescriptor ID(
}

/// <inheritdoc cref="RelayIdFieldExtensions"/>
/// <param name="descriptor">the the descriptor</param>
/// <param name="descriptor">the descriptor</param>
/// <typeparam name="T">
/// the type from which the <see cref="IDAttribute.TypeName">type name</see> is derived
/// </typeparam>
public static IInputFieldDescriptor ID<T>(this IInputFieldDescriptor descriptor)
{
if (descriptor is null)
{
throw new ArgumentNullException(nameof(descriptor));
}

RelayIdFieldHelpers.ApplyIdToField(descriptor, typeof(T).Name);

return descriptor;
}

/// <inheritdoc cref="RelayIdFieldExtensions"/>
/// <param name="descriptor">the descriptor</param>
/// <param name="typeName">
/// Sets the <see cref="IDAttribute.TypeName">type name</see> of the relay id
/// </param>
Expand All @@ -99,7 +116,24 @@ public static IArgumentDescriptor ID(
}

/// <inheritdoc cref="RelayIdFieldExtensions"/>
/// <param name="descriptor">the the descriptor</param>
/// <param name="descriptor">the descriptor</param>
/// <typeparam name="T">
/// the type from which the <see cref="IDAttribute.TypeName">type name</see> is derived
/// </typeparam>
public static IArgumentDescriptor ID<T>(this IArgumentDescriptor descriptor)
{
if (descriptor is null)
{
throw new ArgumentNullException(nameof(descriptor));
}

RelayIdFieldHelpers.ApplyIdToField(descriptor, typeof(T).Name);

return descriptor;
}

/// <inheritdoc cref="RelayIdFieldExtensions"/>
/// <param name="descriptor">the descriptor</param>
/// <param name="typeName">
/// Sets the <see cref="IDAttribute.TypeName">type name</see> of the relay id
/// </param>
Expand All @@ -118,7 +152,24 @@ public static IObjectFieldDescriptor ID(
}

/// <inheritdoc cref="RelayIdFieldExtensions"/>
/// <param name="descriptor">the the descriptor</param>
/// <param name="descriptor">the descriptor</param>
/// <typeparam name="T">
/// the type from which the <see cref="IDAttribute.TypeName">type name</see> is derived
/// </typeparam>
public static IObjectFieldDescriptor ID<T>(this IObjectFieldDescriptor descriptor)
{
if (descriptor is null)
{
throw new ArgumentNullException(nameof(descriptor));
}

RelayIdFieldHelpers.ApplyIdToField(descriptor, typeof(T).Name);

return descriptor;
}

/// <inheritdoc cref="RelayIdFieldExtensions"/>
/// <param name="descriptor">the descriptor</param>
public static IInterfaceFieldDescriptor ID(this IInterfaceFieldDescriptor descriptor)
{
if (descriptor is null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public async Task Id_On_Arguments()
// arrange
var intId = Convert.ToBase64String("Query:1"u8);
var stringId = Convert.ToBase64String("Query:abc"u8);
var guidId = Convert.ToBase64String(Combine("Query:"u8, Guid.Empty.ToByteArray()));
var guidId = Convert.ToBase64String(Combine("Another:"u8, Guid.Empty.ToByteArray()));

// act
var result =
Expand Down Expand Up @@ -47,6 +47,7 @@ public async Task Id_On_Objects()
{
// arrange
var someId = Convert.ToBase64String("Some:1"u8);
var anotherId = Convert.ToBase64String("Another:1"u8);

// act
var result =
Expand All @@ -58,19 +59,27 @@ public async Task Id_On_Objects()
.ExecuteRequestAsync(
OperationRequestBuilder.New()
.SetDocument(
@"query foo ($someId: ID!) {
foo(input: { someId: $someId }) {
"""
query foo ($someId: ID!, $anotherId: ID!) {
foo(input: { someId: $someId, anotherId: $anotherId }) {
someId
anotherId
}
}")
.SetVariableValues(new Dictionary<string, object> { { "someId", someId }, })
}
""")
.SetVariableValues(new Dictionary<string, object>
{
{ "someId", someId },
{ "anotherId", anotherId }
})
.Build());

// assert
new
{
result = result.ToJson(),
someId,
anotherId
}.MatchSnapshot();
}

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

descriptor
.Field(t => t.GuidId(default))
.Argument("id", a => a.ID());
.Argument("id", a => a.ID<Another>());

descriptor
.Field(t => t.Foo(default))
Expand All @@ -123,6 +132,10 @@ protected override void Configure(IInputObjectTypeDescriptor<FooInput> descripto
descriptor
.Field(t => t.SomeId)
.ID("Some");

descriptor
.Field(t => t.AnotherId)
.ID<Another>();
}
}

Expand All @@ -135,6 +148,10 @@ protected override void Configure(IObjectTypeDescriptor<FooPayload> descriptor)
descriptor
.Field(t => t.SomeId)
.ID("Bar");

descriptor
.Field(t => t.AnotherId)
.ID<Another>();
}
}

Expand All @@ -145,6 +162,10 @@ protected override void Configure(IInterfaceTypeDescriptor<IFooPayload> descript
descriptor
.Field(t => t.SomeId)
.ID();

descriptor
.Field(t => t.AnotherId)
.ID();
}
}

Expand All @@ -153,21 +174,27 @@ public class Query
public string IntId(int id) => id.ToString();
public string StringId(string id) => id;
public string GuidId(Guid id) => id.ToString();
public IFooPayload Foo(FooInput input) => new FooPayload { SomeId = input.SomeId, };
public IFooPayload Foo(FooInput input)
=> new FooPayload { SomeId = input.SomeId, AnotherId = input.AnotherId };
}

public class FooInput
{
public string SomeId { get; set; }
public string AnotherId { get; set; }
}

public class FooPayload : IFooPayload
{
public string SomeId { get; set; }
public string AnotherId { get; set; }
}

public interface IFooPayload
{
string SomeId { get; set; }
string AnotherId { get; set; }
}

private class Another;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"result": "{\n \"data\": {\n \"foo\": {\n \"someId\": \"QmFyOjE=\"\n }\n }\n}",
"someId": "U29tZTox"
{
"result": "{\n \"data\": {\n \"foo\": {\n \"someId\": \"QmFyOjE=\",\n \"anotherId\": \"QW5vdGhlcjox\"\n }\n }\n}",
"someId": "U29tZTox",
"anotherId": "QW5vdGhlcjox"
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
schema {
schema {
query: Query
}

interface IFooPayload {
someId: ID
anotherId: ID
}

type FooPayload implements IFooPayload {
someId: ID
anotherId: ID
}

type Query {
Expand All @@ -19,4 +21,5 @@ type Query {

input FooInput {
someId: ID
anotherId: ID
}

0 comments on commit ba1c1b8

Please sign in to comment.