Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed dictionary middleware. (#2492)
- Loading branch information
1 parent
ab0edc3
commit f491d2f
Showing
8 changed files
with
284 additions
and
349 deletions.
There are no files selected for viewing
153 changes: 0 additions & 153 deletions
153
src/HotChocolate/Core/src/Abstractions/Execution/FieldData.cs
This file was deleted.
Oops, something went wrong.
62 changes: 0 additions & 62 deletions
62
src/HotChocolate/Core/src/Abstractions/Execution/FieldValue.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
src/HotChocolate/Stitching/src/Stitching/Delegation/DictionaryDeserializer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using HotChocolate.Language; | ||
using HotChocolate.Types; | ||
|
||
namespace HotChocolate.Stitching.Delegation | ||
{ | ||
internal static class DictionaryDeserializer | ||
{ | ||
public static object? DeserializeResult( | ||
IType fieldType, | ||
object? obj) | ||
{ | ||
INamedType namedType = fieldType.NamedType(); | ||
|
||
if (namedType is IInputType && fieldType is IInputType inputType) | ||
{ | ||
if (namedType.Kind == TypeKind.Enum) | ||
{ | ||
return DeserializeEnumResult(inputType, obj); | ||
} | ||
|
||
if (namedType.Kind == TypeKind.Scalar) | ||
{ | ||
return DeserializeScalarResult(inputType, obj); | ||
} | ||
} | ||
|
||
return obj is NullValueNode ? null : obj; | ||
} | ||
|
||
private static object? DeserializeEnumResult(IInputType inputType, object? value) | ||
{ | ||
switch (value) | ||
{ | ||
case IReadOnlyList<object> list: | ||
{ | ||
var elementType = (IInputType)inputType.ElementType(); | ||
var deserializedList = (IList)Activator.CreateInstance(inputType.RuntimeType)!; | ||
|
||
foreach (object? item in list) | ||
{ | ||
deserializedList.Add(DeserializeEnumResult(elementType, item)); | ||
} | ||
|
||
return deserializedList; | ||
} | ||
|
||
case ListValueNode listLiteral: | ||
{ | ||
var elementType = (IInputType)inputType.ElementType(); | ||
var list = new List<object?>(); | ||
|
||
foreach (IValueNode item in listLiteral.Items) | ||
{ | ||
list.Add(DeserializeEnumResult(elementType, item)); | ||
} | ||
|
||
return list; | ||
} | ||
|
||
case StringValueNode stringLiteral: | ||
return inputType.Deserialize(stringLiteral.Value); | ||
|
||
case IValueNode literal: | ||
return inputType.ParseLiteral(literal); | ||
|
||
default: | ||
return inputType.Deserialize(value); | ||
} | ||
} | ||
|
||
private static object? DeserializeScalarResult(IInputType inputType, object? value) | ||
{ | ||
switch (value) | ||
{ | ||
case IReadOnlyList<object> list: | ||
{ | ||
var elementType = (IInputType)inputType.ElementType(); | ||
var deserializedList = (IList)Activator.CreateInstance(inputType.RuntimeType)!; | ||
|
||
foreach (object? item in list) | ||
{ | ||
deserializedList.Add(DeserializeEnumResult(elementType, item)); | ||
} | ||
|
||
return deserializedList; | ||
} | ||
|
||
case ListValueNode listLiteral: | ||
{ | ||
var elementType = (IInputType)inputType.ElementType(); | ||
var list = new List<object?>(); | ||
|
||
foreach (IValueNode item in listLiteral.Items) | ||
{ | ||
list.Add(DeserializeEnumResult(elementType, item)); | ||
} | ||
|
||
return list; | ||
} | ||
|
||
case IValueNode literal: | ||
return inputType.ParseLiteral(literal); | ||
|
||
default: | ||
return inputType.Deserialize(value); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.