Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Fixing stitching functionality
Added helper extensions to find base generic type
Added some minor exception handling
  • Loading branch information
Michannne committed May 31, 2019
commit 49af677071cb148d2724eceed4446462ff8aa5eb
9 changes: 9 additions & 0 deletions GraphQL-Core/CSharpClassBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using GraphQLCore.Types;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Text;
Expand Down Expand Up @@ -64,6 +65,14 @@ public static Type GetDerivedGenericUserType(this Type T)
return null;
}

public static Type GetBaseGenericUserType(this Type T)
{
if (genericTypeParentClasses.ContainsValue(T))
return genericTypeParentClasses.Where(p => p.Value == T).FirstOrDefault().Key;

return null;
}

private static AssemblyName CreateDynamicAssemblyName()
{
try
Expand Down
45 changes: 40 additions & 5 deletions GraphQL-Core/Extensions/GraphQLExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,19 @@ namespace GraphQLCore.Extensions
/// </summary>
public static class GraphQLExtensions
{
public static (Type, bool) GetConvertibleBaseCSharpType(this Type cSharpType)
public static (Type, bool, Type) GetConvertibleBaseCSharpType(this Type cSharpType)
{
try
{
Type returnType = null;
bool nullable = false;
Type baseGraphQLType = null;


if (cSharpType.IsEnum)
{
returnType = typeof(EnumerationGraphType<>).MakeGenericType(cSharpType);
baseGraphQLType = returnType;
nullable = true;
}

Expand All @@ -50,6 +52,7 @@ public static (Type, bool) GetConvertibleBaseCSharpType(this Type cSharpType)
|| cSharpType == typeof(ushort):
{
returnType = typeof(int);
baseGraphQLType = returnType;
}
break;
case Type floaty when false
Expand All @@ -58,47 +61,54 @@ public static (Type, bool) GetConvertibleBaseCSharpType(this Type cSharpType)
|| cSharpType == typeof(double):
{
returnType = typeof(double);
baseGraphQLType = returnType;
}
break;
case Type alpha when false
|| cSharpType == typeof(char)
|| cSharpType == typeof(string):
{
returnType = typeof(string);
baseGraphQLType = returnType;
nullable = true;
}
break;
case Type boolean when false
|| cSharpType == typeof(bool):
{
returnType = typeof(bool);
baseGraphQLType = returnType;
}
break;
case Type date when false
|| cSharpType == typeof(DateTime):
{
returnType = typeof(DateTimeGraphType);
baseGraphQLType = returnType;
nullable = true;
}
break;
case Type time when false
|| cSharpType == typeof(TimeSpan):
{
returnType = typeof(TimeSpanSecondsGraphType);
baseGraphQLType = returnType;
nullable = true;
}
break;
case Type timeoffset when false
|| cSharpType == typeof(DateTimeOffset):
{
returnType = typeof(DateTimeOffsetGraphType);
baseGraphQLType = returnType;
nullable = true;
}
break;
case Type guid when false
|| cSharpType == typeof(Guid):
{
returnType = typeof(GuidGraphType);
baseGraphQLType = returnType;
nullable = true;
}
break;
Expand All @@ -108,6 +118,7 @@ public static (Type, bool) GetConvertibleBaseCSharpType(this Type cSharpType)
var genericType = typeof(GenericType<>).MakeGenericType(str);
var derived = genericType.GetDerivedGenericUserType();
returnType = derived;
baseGraphQLType = returnType;
nullable = true;
}
break;
Expand All @@ -129,24 +140,26 @@ public static (Type, bool) GetConvertibleBaseCSharpType(this Type cSharpType)
var listType = cSharpType.GenericTypeArguments[0];

if (listType is null)
return (null, false);
return (null, false, null);

(var underClass, _) = GetConvertibleBaseCSharpType(listType);
(var underClass, _, _) = GetConvertibleBaseCSharpType(listType);
if (!underClass.Implements(typeof(IGraphType)))
underClass = underClass.GetGraphTypeFromType(true);
returnType = typeof(ListGraphType<>).MakeGenericType(underClass);
baseGraphQLType = underClass;
nullable = true;
}

else
{
var genericType = typeof(GenericType<>).MakeGenericType(cSharpType);
var derived = genericType.GetDerivedGenericUserType();
baseGraphQLType = derived;
returnType = derived;
nullable = true;
}

return (returnType, nullable);
return (returnType, nullable, baseGraphQLType);
}
catch(Exception e)
{
Expand Down Expand Up @@ -174,7 +187,7 @@ public static Type TryConvertToGraphQLType(this Type cSharpType)
/// <returns>An appropriate GraphQL.NET Type</returns>
public static Type ConvertToGraphQLType(this Type cSharpType)
{
(Type graphQlType, var nullable) = GetConvertibleBaseCSharpType(cSharpType);
(Type graphQlType, var nullable, var underClass) = GetConvertibleBaseCSharpType(cSharpType);

if(graphQlType is null)
throw new GraphQLCoreConversionException(
Expand All @@ -188,6 +201,28 @@ public static Type ConvertToGraphQLType(this Type cSharpType)
return graphQlType.GetGraphTypeFromType(nullable);
}

/// <summary>
/// Given a C# Type instance that represents a value type, enumeration, or class, returns the appropriate base GraphQL.NET Type
/// For example, in cases such as Lists, returns the GraphQL.NET type of the generic argument
/// </summary>
/// <param name="cSharpType">The C# Type to convert</param>
/// <returns>An appropriate GraphQL.NET Type</returns>
public static Type GetBaseGraphQLType(this Type cSharpType)
{
(Type graphQlType, var nullable, var underClass) = GetConvertibleBaseCSharpType(cSharpType);

if (graphQlType is null)
throw new GraphQLCoreConversionException(
$"The C# type '{cSharpType.Name}' is not currently supported for auto-conversion into a GraphQL Node-type. " +
$"Consider registering it as a custom class in your GraphQL middleware");

if (underClass.Implements(typeof(IGraphType)))
return underClass;

else
return underClass.GetGraphTypeFromType(nullable);
}

/// <summary>
/// GraphQL.NET does not support a number of conversions
/// </summary>
Expand Down
8 changes: 5 additions & 3 deletions GraphQL-Core/GraphQLBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ IGraphQLBuilder IGraphQLBuilder.Stitch<A, BResult>(
)
{
var userTypeA = GraphQLCoreTypeWrapperGenerator.CreateGraphQLTypeWrapper<A>();
var userTypeB = typeof(BResult).ConvertToGraphQLType();
var userTypeB = typeof(BResult).GetBaseGraphQLType();
GenericType<A> graphQlTypeAInstance;
if (((IGraphQLBuilder)this).GraphQLTypes.ContainsKey(userTypeA))
graphQlTypeAInstance = (GenericType<A>)((IGraphQLBuilder)this).GraphQLTypes[userTypeA];
Expand All @@ -232,8 +232,10 @@ IGraphQLBuilder IGraphQLBuilder.Stitch<A, BResult>(
graphQlTypeAInstance.Stitch(expr, propertyName, joinTo());

//Remove references of A type from BResult
(((IGraphQLBuilder)this).GraphQLTypes[userTypeB] as GenericType<BResult>)
.RemoveFieldType(userTypeA);
var baseTypeOfBResult = GraphQLCoreTypeWrapperGenerator.GetBaseGenericUserType(userTypeB);
var storedObjectInstanceOfBase = ((IGraphQLBuilder)this).GraphQLTypes[userTypeB];
baseTypeOfBResult
.InvokeMember("RemoveFieldType", BindingFlags.InvokeMethod, null, storedObjectInstanceOfBase, new object[] { userTypeA });

return this;
}
Expand Down
13 changes: 10 additions & 3 deletions GraphQL-Core/Types/Types.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,16 @@ object resolver(ResolveFieldContext<T> context)

public void RemoveFieldType(Type ofType)
{
var currentFields = ((ObjectGraphType<T>)this).Fields;
FieldInfo fi = ((ObjectGraphType<T>)this).GetType().BaseType.BaseType.GetField("_fields", BindingFlags.NonPublic | BindingFlags.Instance);
fi.SetValue(this, currentFields.Where(field => field.Type != ofType).ToList());
try
{
var currentFields = ((ObjectGraphType<T>)this).Fields;
FieldInfo fi = ((ObjectGraphType<T>)this).GetType().BaseType.BaseType.GetField("_fields", BindingFlags.NonPublic | BindingFlags.Instance);
fi.SetValue(this, currentFields.Where(field => field.Type != ofType).ToList());
}
catch(Exception e)
{
throw new GraphQLCoreTypeException($"An error occurred while attempted to remove field of type {nameof(ofType)} from {nameof(T)}. Refer to inner exception for details", e);
}
}
}
}