Skip to content

Commit 8dfba6d

Browse files
committed
feat: add ExtendableInputObjectGraphType
1 parent f7af5d7 commit 8dfba6d

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

src/VirtoCommerce.Xapi.Core/Helpers/GraphTypeExtensionHelper.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ public static Type GetActualComplexType<TGraphType>() where TGraphType : IGraphT
4444
return GetActualComplexTypeRecursive(outerGraphType);
4545
}
4646

47+
public static Type GetActualComplexType(Type graphType)
48+
{
49+
return GetActualComplexTypeRecursive(graphType);
50+
}
51+
4752
private static Type GetActualComplexTypeRecursive(Type outerGraphType)
4853
{
4954
if (outerGraphType.IsGenericType && outerGraphType.GenericTypeArguments.Length > 0)
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using GraphQL;
4+
using GraphQL.Types;
5+
using VirtoCommerce.Xapi.Core.Helpers;
6+
7+
namespace VirtoCommerce.Xapi.Core.Schemas;
8+
9+
public class ExtendableInputObjectGraphType : ExtendableInputObjectGraphType<object>
10+
{
11+
}
12+
13+
public class ExtendableInputObjectGraphType<TSourceType> : InputObjectGraphType<TSourceType>
14+
{
15+
private Func<IDictionary<string, object>, object> _parseDictionary;
16+
private bool _initialized;
17+
18+
public ExtendableInputObjectGraphType()
19+
{
20+
if (typeof(TSourceType) == typeof(object))
21+
{
22+
// when typeof(TSourceType) != typeof(object)
23+
_parseDictionary = static x =>
24+
{
25+
return x;
26+
};
27+
}
28+
}
29+
30+
public override FieldType AddField(FieldType fieldType)
31+
{
32+
fieldType.Type = GraphTypeExtensionHelper.GetActualComplexType(fieldType.Type);
33+
34+
return base.AddField(fieldType);
35+
}
36+
37+
38+
public override void Initialize(ISchema schema)
39+
{
40+
if (_initialized)
41+
{
42+
throw new InvalidOperationException($"The graph type with name '{Name}' has already been initialized. Make sure that you do not use the same instance of a graph type in multiple schemas. It may be so if you registered this graph type as singleton; see https://graphql-dotnet.github.io/docs/getting-started/dependency-injection/ for more info.");
43+
}
44+
45+
_initialized = true;
46+
47+
// when typeof(TSourceType) != typeof(object)
48+
if (_parseDictionary != null)
49+
{
50+
return;
51+
}
52+
53+
var actualType = GenericTypeHelper.GetActualType<TSourceType>();
54+
55+
var conversion = ValueConverter.GetConversion(typeof(IDictionary<string, object>), actualType);
56+
if (conversion != null)
57+
{
58+
_parseDictionary = conversion;
59+
}
60+
else if (GlobalSwitches.DynamicallyCompileToObject)
61+
{
62+
if (GetType().GetMethod(nameof(ParseDictionary), [typeof(IDictionary<string, object>)])!.DeclaringType == typeof(InputObjectGraphType<TSourceType>))
63+
{
64+
_parseDictionary = ObjectExtensions.CompileToObject(actualType, this);
65+
}
66+
else
67+
{
68+
_parseDictionary = data =>
69+
{
70+
_parseDictionary = ObjectExtensions.CompileToObject(actualType, this);
71+
var result = _parseDictionary(data);
72+
return result;
73+
};
74+
}
75+
}
76+
else
77+
{
78+
_parseDictionary = data => data.ToObject(actualType, this);
79+
}
80+
}
81+
82+
//public override object ParseDictionary(IDictionary<string, object> value)
83+
//{
84+
// if (value == null)
85+
// {
86+
// return null;
87+
// }
88+
89+
// object result = null;
90+
// if (_parseDictionary != null)
91+
// {
92+
// result = _parseDictionary(value);
93+
// return result;
94+
// }
95+
96+
// var actualType = GenericTypeHelper.GetActualType<TSourceType>();
97+
// result = value.ToObject(actualType, this);
98+
// return result;
99+
//}
100+
}

0 commit comments

Comments
 (0)