Skip to content

Commit

Permalink
Fix DISchemaTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
Shane32 committed Nov 17, 2021
1 parent a466017 commit 6a9815a
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/GraphQL.DI/AutoObjectGraphType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ protected virtual IEnumerable<PropertyInfo> GetPropertiesToProcess()
protected virtual IEnumerable<MethodInfo> GetMethodsToProcess()
{
var methods = typeof(TSourceType).GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly)
.Where(x => !x.ContainsGenericParameters);
.Where(x => !x.ContainsGenericParameters && !x.IsSpecialName);
return methods;
}

Expand Down
2 changes: 1 addition & 1 deletion src/GraphQL.DI/DIObjectGraphType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ protected virtual IEnumerable<DIFieldType> CreateFieldTypeList()
protected virtual IEnumerable<MethodInfo> GetMethodsToProcess()
{
var methods = typeof(TDIGraph).GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly)
.Where(x => !x.ContainsGenericParameters);
.Where(x => !x.ContainsGenericParameters && !x.IsSpecialName);
return methods;
}

Expand Down
58 changes: 53 additions & 5 deletions src/GraphQL.DI/DISchemaTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,51 @@ namespace GraphQL.DI
/// <inheritdoc/>
public class DISchemaTypes : SchemaTypes
{
private readonly bool _autoMapInputTypes;
private readonly bool _autoMapOutputTypes;
private readonly bool _autoMapInputTypes = true;
private readonly bool _autoMapOutputTypes = true;

private readonly Type _autoInputObjectGraphType = typeof(AutoInputObjectGraphType<>);
/*
/// <summary>
/// The generic type definition which is used to construct auto-mapped input graph types.
/// Defaults to <see cref="AutoInputObjectGraphType{TSourceType}"/>.
/// </summary>
protected Type AutoInputObjectGraphType {
get => _autoInputObjectGraphType;
set {
if (value == null) throw new ArgumentNullException(nameof(value));
if (!value.IsGenericTypeDefinition || value.GenericTypeArguments.Length != 1)
throw new ArgumentOutOfRangeException(nameof(value),
$"The type '{value}' is not a generic type definition with an arity of 1.");
if (!typeof(IInputObjectGraphType).IsAssignableFrom(value))
throw new ArgumentOutOfRangeException(nameof(value),
$"The type '{value}' does not implement {nameof(IInputObjectGraphType)}");
_autoInputObjectGraphType = value;
}
}
*/

private readonly Type _autoObjectGraphType = typeof(AutoObjectGraphType<>);
/*
/// <summary>
/// The generic type definition which is used to construct auto-mapped Output graph types.
/// Defaults to <see cref="AutoObjectGraphType{TSourceType}"/>.
/// </summary>
protected Type AutoObjectGraphType {
get => _autoObjectGraphType;
set {
if (value == null)
throw new ArgumentNullException(nameof(value));
if (!value.IsGenericTypeDefinition || value.GenericTypeArguments.Length != 1)
throw new ArgumentOutOfRangeException(nameof(value),
$"The type '{value}' is not a generic type definition with an arity of 1.");
if (!typeof(IObjectGraphType).IsAssignableFrom(value))
throw new ArgumentOutOfRangeException(nameof(value),
$"The type '{value}' does not implement {nameof(IObjectGraphType)}");
_autoObjectGraphType = value;
}
}
*/

/// <summary>
/// Initializes a new instance for the specified schema, and with the specified type resolver.
Expand All @@ -17,8 +60,6 @@ public class DISchemaTypes : SchemaTypes
/// </summary>
public DISchemaTypes(ISchema schema, IServiceProvider serviceProvider) : base(schema, serviceProvider)
{
_autoMapInputTypes = true;
_autoMapOutputTypes = true;
}

/// <summary>
Expand All @@ -29,6 +70,10 @@ public DISchemaTypes(ISchema schema, IServiceProvider serviceProvider) : base(sc
/// </summary>
public DISchemaTypes(ISchema schema, IServiceProvider serviceProvider, bool autoMapInputTypes, bool autoMapOutputTypes) : base(schema, serviceProvider)
{
if (autoMapInputTypes == false || autoMapOutputTypes == false)
throw new NotSupportedException(
"This functionality is not yet supported due to a design constraint within GraphQL.NET. " +
$"Please override {nameof(AutoMapInputType)} and/or {nameof(AutoMapOutputType)} to disable auto mapping.");
_autoMapInputTypes = autoMapInputTypes;
_autoMapOutputTypes = autoMapOutputTypes;
}
Expand All @@ -37,9 +82,12 @@ public DISchemaTypes(ISchema schema, IServiceProvider serviceProvider, bool auto
protected override Type? GetGraphTypeFromClrType(Type clrType, bool isInputType, List<(Type ClrType, Type GraphType)> typeMappings)
{
var type = base.GetGraphTypeFromClrType(clrType, isInputType, typeMappings);
if (type == null && isInputType && clrType.IsClass) {
if (type == null && isInputType && clrType.IsClass && AutoMapInputType(clrType)) {
return typeof(AutoInputObjectGraphType<>).MakeGenericType(clrType);
}
if (type == null && !isInputType && clrType.IsClass && AutoMapOutputType(clrType)) {
return typeof(AutoObjectGraphType<>).MakeGenericType(clrType);
}
return type;
}

Expand Down
38 changes: 38 additions & 0 deletions src/Tests/DISchemaTypesTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using GraphQL;
using GraphQL.DI;
using GraphQL.Types;
using Shouldly;
using Xunit;

namespace DISchemaTypesTests
{
public class Properties
{
[Fact]
public void InputOutputTypesWork()
{
var schema = new Schema();
schema.Query = new ObjectGraphType();
var newField = new FieldType { Name = "Test", Type = typeof(GraphQLClrOutputTypeReference<Class1Model>) };
newField.Arguments = new QueryArguments(new QueryArgument<GraphQLClrInputTypeReference<Class2InputModel>> { Name = "arg" });
schema.Query.AddField(newField);
var schemaTypes = new DISchemaTypes(schema, new DefaultServiceProvider());
var class1Type = schemaTypes["Class1"].ShouldBeAssignableTo<IObjectGraphType>();
class1Type.Fields.Count.ShouldBe(1);
class1Type.Fields.Find("value").ShouldNotBeNull();
var class2Type = schemaTypes["Class2Input"].ShouldBeAssignableTo<IInputObjectGraphType>();
class2Type.Fields.Count.ShouldBe(1);
class2Type.Fields.Find("value").ShouldNotBeNull();
}

private class Class1Model
{
public int Value { get; set; }
}

private class Class2InputModel
{
public int Value { get; set; }
}
}
}

0 comments on commit 6a9815a

Please sign in to comment.