Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix DISchemaTypes #16

Merged
merged 5 commits into from
Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
15 changes: 10 additions & 5 deletions src/GraphQL.DI/DISchemaTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ 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;

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

/// <summary>
Expand All @@ -29,6 +27,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 +39,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
76 changes: 74 additions & 2 deletions src/Tests/Execution/DISchemaTypes.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using GraphQL;
using GraphQL.DI;
using GraphQL.Types;
using Moq;
Expand All @@ -13,7 +14,7 @@ public class DISchemaTypesTests
[Theory]
[InlineData(typeof(int), false, typeof(IntGraphType))]
[InlineData(typeof(int), true, typeof(IntGraphType))]
[InlineData(typeof(Class1), false, null)]
[InlineData(typeof(Class1), false, typeof(AutoObjectGraphType<Class1>))]
[InlineData(typeof(Class1), true, typeof(AutoInputObjectGraphType<Class1>))]
public void GetGraphTypeFromClrType(Type clrType, bool isInputType, Type graphType)
{
Expand All @@ -24,12 +25,83 @@ public void GetGraphTypeFromClrType(Type clrType, bool isInputType, Type graphTy

private class MySchemaTypes : DISchemaTypes
{
public MySchemaTypes() : base(new Schema(), Mock.Of<IServiceProvider>(), true, true) { }
public MySchemaTypes() : base(new Schema(), Mock.Of<IServiceProvider>()) { }

public new Type GetGraphTypeFromClrType(Type clrType, bool isInputType, List<(Type ClrType, Type GraphType)> typeMappings)
=> base.GetGraphTypeFromClrType(clrType, isInputType, typeMappings);
}

[Theory]
[InlineData(typeof(int), false, typeof(IntGraphType))]
[InlineData(typeof(int), true, typeof(IntGraphType))]
[InlineData(typeof(Class1), false, null)]
[InlineData(typeof(Class1), true, typeof(AutoInputObjectGraphType<Class1>))]
public void GetGraphTypeFromClrType_NoOutput(Type clrType, bool isInputType, Type graphType)
{
var mySchemaTypes2 = new MySchemaTypes2();
var mappedTypes = new List<(Type, Type)>();
mySchemaTypes2.GetGraphTypeFromClrType(clrType, isInputType, mappedTypes).ShouldBe(graphType);
}

private class MySchemaTypes2 : DISchemaTypes
{
public MySchemaTypes2() : base(new Schema(), Mock.Of<IServiceProvider>(), true, true) { }

public new Type GetGraphTypeFromClrType(Type clrType, bool isInputType, List<(Type ClrType, Type GraphType)> typeMappings)
=> base.GetGraphTypeFromClrType(clrType, isInputType, typeMappings);

protected override bool AutoMapOutputType(Type clrType) => false;
}

[Theory]
[InlineData(typeof(int), false, typeof(IntGraphType))]
[InlineData(typeof(int), true, typeof(IntGraphType))]
[InlineData(typeof(Class1), false, typeof(AutoObjectGraphType<Class1>))]
[InlineData(typeof(Class1), true, null)]
public void GetGraphTypeFromClrType_NoInput(Type clrType, bool isInputType, Type graphType)
{
var mySchemaTypes3 = new MySchemaTypes3();
var mappedTypes = new List<(Type, Type)>();
mySchemaTypes3.GetGraphTypeFromClrType(clrType, isInputType, mappedTypes).ShouldBe(graphType);
}

private class MySchemaTypes3 : DISchemaTypes
{
public MySchemaTypes3() : base(new Schema(), Mock.Of<IServiceProvider>(), true, true) { }

public new Type GetGraphTypeFromClrType(Type clrType, bool isInputType, List<(Type ClrType, Type GraphType)> typeMappings)
=> base.GetGraphTypeFromClrType(clrType, isInputType, typeMappings);

protected override bool AutoMapInputType(Type clrType) => false;
}

private class Class1 { }

[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; }
}
}
}