From ba1c1b800b44c849edc2916477d2a023d5cf0532 Mon Sep 17 00:00:00 2001 From: Glen Date: Wed, 25 Sep 2024 17:35:27 +0200 Subject: [PATCH] Added a generic version of the ID descriptor method (#7503) (cherry picked from commit 86a9c8144142412bbdbe2bd59d2b4696e0f34ed3) --- .../Extensions/RelayIdFieldExtensions.cs | 59 +++++++++++++++++-- .../Types/Relay/IdDescriptorTests.cs | 41 ++++++++++--- .../IdDescriptorTests.Id_On_Objects.snap | 7 ++- ...orTests.Id_Type_Is_Correctly_Inferred.snap | 5 +- 4 files changed, 97 insertions(+), 15 deletions(-) diff --git a/src/HotChocolate/Core/src/Types/Types/Relay/Extensions/RelayIdFieldExtensions.cs b/src/HotChocolate/Core/src/Types/Types/Relay/Extensions/RelayIdFieldExtensions.cs index 9bdba582f94..2c1152be3c6 100644 --- a/src/HotChocolate/Core/src/Types/Types/Relay/Extensions/RelayIdFieldExtensions.cs +++ b/src/HotChocolate/Core/src/Types/Types/Relay/Extensions/RelayIdFieldExtensions.cs @@ -61,7 +61,7 @@ namespace HotChocolate.Types; public static class RelayIdFieldExtensions { /// - /// the the descriptor + /// the descriptor /// /// Sets the type name of the relay id /// @@ -80,7 +80,24 @@ public static IInputFieldDescriptor ID( } /// - /// the the descriptor + /// the descriptor + /// + /// the type from which the type name is derived + /// + public static IInputFieldDescriptor ID(this IInputFieldDescriptor descriptor) + { + if (descriptor is null) + { + throw new ArgumentNullException(nameof(descriptor)); + } + + RelayIdFieldHelpers.ApplyIdToField(descriptor, typeof(T).Name); + + return descriptor; + } + + /// + /// the descriptor /// /// Sets the type name of the relay id /// @@ -99,7 +116,24 @@ public static IArgumentDescriptor ID( } /// - /// the the descriptor + /// the descriptor + /// + /// the type from which the type name is derived + /// + public static IArgumentDescriptor ID(this IArgumentDescriptor descriptor) + { + if (descriptor is null) + { + throw new ArgumentNullException(nameof(descriptor)); + } + + RelayIdFieldHelpers.ApplyIdToField(descriptor, typeof(T).Name); + + return descriptor; + } + + /// + /// the descriptor /// /// Sets the type name of the relay id /// @@ -118,7 +152,24 @@ public static IObjectFieldDescriptor ID( } /// - /// the the descriptor + /// the descriptor + /// + /// the type from which the type name is derived + /// + public static IObjectFieldDescriptor ID(this IObjectFieldDescriptor descriptor) + { + if (descriptor is null) + { + throw new ArgumentNullException(nameof(descriptor)); + } + + RelayIdFieldHelpers.ApplyIdToField(descriptor, typeof(T).Name); + + return descriptor; + } + + /// + /// the descriptor public static IInterfaceFieldDescriptor ID(this IInterfaceFieldDescriptor descriptor) { if (descriptor is null) diff --git a/src/HotChocolate/Core/test/Types.Tests/Types/Relay/IdDescriptorTests.cs b/src/HotChocolate/Core/test/Types.Tests/Types/Relay/IdDescriptorTests.cs index 35e14735155..c7dc943408a 100644 --- a/src/HotChocolate/Core/test/Types.Tests/Types/Relay/IdDescriptorTests.cs +++ b/src/HotChocolate/Core/test/Types.Tests/Types/Relay/IdDescriptorTests.cs @@ -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 = @@ -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 = @@ -58,12 +59,19 @@ 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 { { "someId", someId }, }) + } + """) + .SetVariableValues(new Dictionary + { + { "someId", someId }, + { "anotherId", anotherId } + }) .Build()); // assert @@ -71,6 +79,7 @@ public async Task Id_On_Objects() { result = result.ToJson(), someId, + anotherId }.MatchSnapshot(); } @@ -107,7 +116,7 @@ protected override void Configure(IObjectTypeDescriptor descriptor) descriptor .Field(t => t.GuidId(default)) - .Argument("id", a => a.ID()); + .Argument("id", a => a.ID()); descriptor .Field(t => t.Foo(default)) @@ -123,6 +132,10 @@ protected override void Configure(IInputObjectTypeDescriptor descripto descriptor .Field(t => t.SomeId) .ID("Some"); + + descriptor + .Field(t => t.AnotherId) + .ID(); } } @@ -135,6 +148,10 @@ protected override void Configure(IObjectTypeDescriptor descriptor) descriptor .Field(t => t.SomeId) .ID("Bar"); + + descriptor + .Field(t => t.AnotherId) + .ID(); } } @@ -145,6 +162,10 @@ protected override void Configure(IInterfaceTypeDescriptor descript descriptor .Field(t => t.SomeId) .ID(); + + descriptor + .Field(t => t.AnotherId) + .ID(); } } @@ -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; } diff --git a/src/HotChocolate/Core/test/Types.Tests/Types/Relay/__snapshots__/IdDescriptorTests.Id_On_Objects.snap b/src/HotChocolate/Core/test/Types.Tests/Types/Relay/__snapshots__/IdDescriptorTests.Id_On_Objects.snap index 1ef53d6fda2..a8023318c6c 100644 --- a/src/HotChocolate/Core/test/Types.Tests/Types/Relay/__snapshots__/IdDescriptorTests.Id_On_Objects.snap +++ b/src/HotChocolate/Core/test/Types.Tests/Types/Relay/__snapshots__/IdDescriptorTests.Id_On_Objects.snap @@ -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" } diff --git a/src/HotChocolate/Core/test/Types.Tests/Types/Relay/__snapshots__/IdDescriptorTests.Id_Type_Is_Correctly_Inferred.snap b/src/HotChocolate/Core/test/Types.Tests/Types/Relay/__snapshots__/IdDescriptorTests.Id_Type_Is_Correctly_Inferred.snap index b5be55322d7..20c520646a7 100644 --- a/src/HotChocolate/Core/test/Types.Tests/Types/Relay/__snapshots__/IdDescriptorTests.Id_Type_Is_Correctly_Inferred.snap +++ b/src/HotChocolate/Core/test/Types.Tests/Types/Relay/__snapshots__/IdDescriptorTests.Id_Type_Is_Correctly_Inferred.snap @@ -1,13 +1,15 @@ -schema { +schema { query: Query } interface IFooPayload { someId: ID + anotherId: ID } type FooPayload implements IFooPayload { someId: ID + anotherId: ID } type Query { @@ -19,4 +21,5 @@ type Query { input FooInput { someId: ID + anotherId: ID }