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
15 changes: 12 additions & 3 deletions src/HotChocolate/Core/src/Types/Types/KeyValuePairObjectType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ private void ConfigureInternal(ObjectTypeDescriptor descriptor)
var descriptorExtension = descriptor.Extend();
var context = descriptorExtension.Context;
var configuration = descriptorExtension.Configuration;
var keyProperty = runtimeType.Type.GetProperty("Key")!;
var valueProperty = runtimeType.Type.GetProperty("Value")!;
var keyType = runtimeType.TypeArguments[0];
var valueType = runtimeType.TypeArguments[1];

Expand All @@ -37,8 +39,15 @@ private void ConfigureInternal(ObjectTypeDescriptor descriptor)
configuration.FieldBindingType = runtimeType.Type;
configuration.RuntimeType = runtimeType.Type;

descriptor.InferFieldsFromFieldBindingType();
descriptor.Field("key").Extend().Configuration.Type = TypeReference.Create(keyType, TypeContext.Output);
descriptor.Field("value").Extend().Configuration.Type = TypeReference.Create(valueType, TypeContext.Output);
descriptor
.Field(keyProperty)
.Name("key")
.Extend()
.Configuration.Type = TypeReference.Create(keyType, TypeContext.Output);
descriptor
.Field(valueProperty)
.Name("value")
.Extend()
.Configuration.Type = TypeReference.Create(valueType, TypeContext.Output);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using HotChocolate.Execution;
using Microsoft.Extensions.DependencyInjection;

namespace HotChocolate.Types;

public class InputObjectTypeDictionaryTests
public class DictionaryTypeTests
: TypeTestBase
{
[Fact]
Expand Down Expand Up @@ -202,6 +203,99 @@ type KeyValuePairOfStringAndIListOfString {
""");
}

[Fact]
public void Dictionary_Output_With_Object_Value_In_Oblivious_Context_Is_Supported()
{
// arrange
// act
var schema = SchemaBuilder.New()
.AddQueryType<DictionaryOutputQueryWithObliviousObjectValues>()
.Create();

// assert
var queryType = schema.Types.GetType<ObjectType>("DictionaryOutputQueryWithObliviousObjectValues");
var metadataType = schema.Types.GetType<ObjectType>(queryType.Fields["metadata"].Type.TypeName());
var attributesType = schema.Types.GetType<ObjectType>(queryType.Fields["attributes"].Type.TypeName());

Snapshot.Create()
.Add(metadataType.ToString(), "metadata")
.Add(attributesType.ToString(), "attributes")
.MatchInline(
"""
metadata
---------------
type KeyValuePairOfNullableStringAndNullableObject {
key: String
value: Any
}
---------------

attributes
---------------
type KeyValuePairOfNullableStringAndNullableObject {
key: String
value: Any
}
---------------

""");
}

[Fact]
public async Task Dictionary_Output_With_Object_Value_In_Oblivious_Context_Executes()
{
// arrange
var executor = await new ServiceCollection()
.AddGraphQLServer()
.AddQueryType<DictionaryOutputQueryWithObliviousObjectValuesExecution>()
.AddJsonTypeConverter()
.BuildRequestExecutorAsync();

// act
var result = await executor.ExecuteAsync(
"""
query {
metadata {
key
value
}
attributes {
key
value
}
}
""");

// assert
result.MatchInlineSnapshot(
"""
{
"data": {
"metadata": [
{
"key": "string",
"value": "abc"
},
{
"key": "int",
"value": 123
}
],
"attributes": [
{
"key": "bool",
"value": true
},
{
"key": "nullValue",
"value": null
}
]
}
}
""");
}

public class Query
{
public string GetFoo(FooInput input)
Expand Down Expand Up @@ -286,6 +380,34 @@ public class DictionaryWithListOutput
new Dictionary<string, IList<string>>();
}

#nullable disable

public class DictionaryOutputQueryWithObliviousObjectValues
{
public Dictionary<string, object> GetMetadata() => null;

public IReadOnlyDictionary<string, object> GetAttributes() => null;
}

public class DictionaryOutputQueryWithObliviousObjectValuesExecution
{
public Dictionary<string, object> GetMetadata() =>
new()
{
["string"] = "abc",
["int"] = 123
};

public IReadOnlyDictionary<string, object> GetAttributes() =>
new Dictionary<string, object>
{
["bool"] = true,
["nullValue"] = null
};
}

#nullable restore

public class CustomKeyValuePairType : ObjectType<KeyValuePair<string, string>>
{
protected override void Configure(IObjectTypeDescriptor<KeyValuePair<string, string>> descriptor)
Expand Down
Loading