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
Next Next commit
Fixed issue with enumerations not being stored as singletons
  • Loading branch information
Michannne committed May 3, 2019
commit 8d3c72672f252422487779c51ed3cc57e14e64a9
32 changes: 32 additions & 0 deletions GraphQL-Core.Tests/TestExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using GraphQL_Core.Tests.Models.Enum;
using System;
using System.Collections.Generic;
using System.Text;

namespace GraphQL_Core.Tests
{
public static class TestExtensions
{
public static dynamic DefaultUnknownProperty(this Type t)
{
dynamic defaultValue = null;

switch (t)
{
case Type u when t == typeof(string):
defaultValue = "";
break;
case Type u when t.IsEnum:
defaultValue = 11;
break;
case Type u when t.IsInterface:
break;
default:
defaultValue = Activator.CreateInstance(t);
break;
}

return defaultValue;
}
}
}
7 changes: 6 additions & 1 deletion GraphQL-Core.Tests/TypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System;
using GraphQLCore.Resolvers;
using GraphQLCore.GraphQL;
using GraphQL_Core.Tests.Models.Enum;
using GraphQL.Types;

namespace GraphQL_Core.Tests
{
Expand Down Expand Up @@ -200,14 +202,16 @@ public void Generic_Test_AddType_IsQueryable<T>()
{
initializer.Init();
var builder = initializer.services.AddGraphQL()
.Type<EnumerationGraphType<BookType>>()
.Type<T>();

foreach (var field in typeof(T).GetProperties())
{
GraphQLQuery defaultQuery =
() => new Query() {
Expression = $"get_{field.Name}",
Resolver = (context) => Activator.CreateInstance(field.PropertyType)
Resolver = (context) =>
field.PropertyType.DefaultUnknownProperty()
};

var methodInfo = builder
Expand All @@ -234,6 +238,7 @@ public void Generic_Test_AddType_IsQueryable<T>()
{
Assert.IsTrue(userModelInstance.HasField(field.Name));

//add sub-selection is field is class, use all props
(var hasError, var result) = initializer.Ask($@"
{{
get_{field.Name}
Expand Down
3 changes: 3 additions & 0 deletions GraphQL-Core/Extensions/GraphQLExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ public static (Type, bool) GetConvertibleBaseCSharpType(this Type cSharpType)


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

else if (false
|| cSharpType.IsValueType
Expand Down
28 changes: 21 additions & 7 deletions GraphQL-Core/GraphQLBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,30 @@ public GraphQLBuilder(IServiceCollection services)

IGraphQLBuilder IGraphQLBuilder.Type<T>()
{
var userType = GraphQLCoreTypeWrapperGenerator.CreateGraphQLTypeWrapper<T>();
if(typeof(T).IsGenericType && typeof(T).GenericTypeArguments[0].IsEnum)
{
var enumType = typeof(T).GenericTypeArguments[0];

if (((IGraphQLBuilder)this).GraphQLTypes.ContainsKey(userType))
return this;
if (((IGraphQLBuilder)this).GraphQLTypes.ContainsKey(typeof(T)))
return this;

var graphQlTypeInstance = new GenericType<T>(this)
var graphQlTypeInstance = Activator.CreateInstance<T>();
((IGraphQLBuilder)this).GraphQLTypes.Add(typeof(T), graphQlTypeInstance);
}
else
{
Name = typeof(T).Name
};
((IGraphQLBuilder)this).GraphQLTypes.Add(userType, graphQlTypeInstance);
var userType = GraphQLCoreTypeWrapperGenerator.CreateGraphQLTypeWrapper<T>();

if (((IGraphQLBuilder)this).GraphQLTypes.ContainsKey(userType))
return this;

var graphQlTypeInstance = new GenericType<T>(this)
{
Name = typeof(T).Name
};
((IGraphQLBuilder)this).GraphQLTypes.Add(userType, graphQlTypeInstance);
}

return this;
}

Expand Down
8 changes: 7 additions & 1 deletion GraphQL-Core/Types/Types.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ public class GenericType<T> : ObjectGraphType<T>, IGraphQLGenericType
/// <summary>
/// Initializes the GraphQL Type, using the provided user-defined model
/// </summary>

public GenericType()
: this(null)
{
}

public GenericType(IGraphQLBuilder builder = null)
{
var typedClass = typeof(T);
Expand All @@ -29,7 +35,7 @@ public GenericType(IGraphQLBuilder builder = null)

propGraphQLType = prop.PropertyType.TryConvertToGraphQLType();

if(propGraphQLType is null)
if(propGraphQLType is null || propGraphQLType.IsEnum)
{
builder
.GetType()
Expand Down