Skip to content

Commit

Permalink
Fixed explicit binding behavior (#6237)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib authored Jun 2, 2023
1 parent 679cc61 commit 9274bc9
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 43 deletions.
33 changes: 29 additions & 4 deletions src/HotChocolate/Core/src/Types/SchemaOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ namespace HotChocolate;
/// </summary>
public class SchemaOptions : IReadOnlySchemaOptions
{
private BindingBehavior _defaultBindingBehavior = BindingBehavior.Implicit;
private FieldBindingFlags _defaultFieldBindingFlags = FieldBindingFlags.Instance;

/// <summary>
/// Gets or sets the name of the query type.
/// </summary>
Expand Down Expand Up @@ -64,15 +67,37 @@ public class SchemaOptions : IReadOnlySchemaOptions
/// <summary>
/// Defines the default binding behavior.
/// </summary>
public BindingBehavior DefaultBindingBehavior { get; set; } =
BindingBehavior.Implicit;
public BindingBehavior DefaultBindingBehavior
{
get => _defaultBindingBehavior;
set
{
_defaultBindingBehavior = value;

if (value is BindingBehavior.Explicit)
{
_defaultFieldBindingFlags = FieldBindingFlags.Default;
}
}
}

/// <summary>
/// Defines which members shall be by default inferred as GraphQL fields.
/// This default applies to <see cref="ObjectType"/> and <see cref="ObjectTypeExtension"/>.
/// </summary>
public FieldBindingFlags DefaultFieldBindingFlags { get; set; } =
FieldBindingFlags.Instance;
public FieldBindingFlags DefaultFieldBindingFlags
{
get => _defaultFieldBindingFlags;
set
{
_defaultFieldBindingFlags = value;

if (value is not FieldBindingFlags.Default)
{
_defaultBindingBehavior = BindingBehavior.Implicit;
}
}
}

/// <summary>
/// Defines on which fields a middleware pipeline can be applied on.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ private bool CanBeHandled(
property.GetIndexParameters().Length == 0;
}

if (member is MethodInfo method &&
if (member is MethodInfo { IsGenericMethodDefinition: false } method &&
CanHandleReturnType(member, method.ReturnType, allowObjectType))
{
foreach (var parameter in method.GetParameters())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,9 @@ public FieldBindingFlags FieldBindingFlags
{
get
{
if (Fields.BindingBehavior == BindingBehavior.Explicit)
{
return FieldBindingFlags.Default;
}

return _fieldBindingFlags;
return Fields.BindingBehavior is BindingBehavior.Explicit
? FieldBindingFlags.Default
: _fieldBindingFlags;
}
set
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"SortFieldsByName": true,
"PreserveSyntaxNodes": true,
"RemoveUnreachableTypes": true,
"DefaultBindingBehavior": "Explicit",
"DefaultBindingBehavior": "Implicit",
"DefaultFieldBindingFlags": "InstanceAndStatic",
"FieldMiddleware": "AllFields",
"EnableDirectiveIntrospection": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"PreserveSyntaxNodes": true,
"RemoveUnreachableTypes": false,
"DefaultBindingBehavior": "Explicit",
"DefaultFieldBindingFlags": "Instance",
"DefaultFieldBindingFlags": "Default",
"FieldMiddleware": "AllFields",
"EnableDirectiveIntrospection": true,
"DefaultDirectiveVisibility": "Public",
Expand Down
52 changes: 52 additions & 0 deletions src/HotChocolate/Core/test/Types.Tests/SchemaBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Moq;
using Snapshooter.Xunit;
using Xunit;
using SnapshotExtensions = CookieCrumble.SnapshotExtensions;

namespace HotChocolate;

Expand Down Expand Up @@ -2088,6 +2089,19 @@ public void Convention_Should_UseDefault_When_NotRegisteredAndApplyExtensions()
Assert.True(Assert.IsType<MockConvention>(result).IsExtended);
}

[Fact]
public async Task Ensure_Types_Are_Bound_Explicitly()
{
var schema =
await new ServiceCollection()
.AddGraphQL()
.AddQueryType<RootQuery>()
.ModifyOptions(options => options.DefaultBindingBehavior = BindingBehavior.Explicit)
.BuildSchemaAsync();

SnapshotExtensions.MatchSnapshot(schema);
}

public interface IMockConvention : IConvention
{
}
Expand Down Expand Up @@ -2298,4 +2312,42 @@ public override void OnBeforeCreateSchema(
ISchemaBuilder schemaBuilder) =>
_onBeforeCreate(context);
}

public class TestData
{
public T ResolveValue<T>()
{
return (T)new object();
}
}

public class TestDataType : ObjectType<TestData>
{
protected override void Configure(IObjectTypeDescriptor<TestData> descriptor)
{
descriptor.Name("TestDataType");
descriptor.Description("Test Data Type.");

descriptor
.Field("id")
.Description("Id")
.Type<StringType>()
.Resolve(c=> c.Parent<TestData>().ResolveValue<string>());
}
}

public class RootQuery : ObjectType
{
protected override void Configure(IObjectTypeDescriptor descriptor)
{
descriptor.Name( "RootQuery");
descriptor.Description( "The root query");

descriptor
.Field("testData")
.Description("Returns testDataType.")
.Type<TestDataType>()
.Resolve(c => new TestData());
}
}
}
39 changes: 21 additions & 18 deletions src/HotChocolate/Core/test/Types.Tests/Types/ObjectTypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2074,31 +2074,15 @@ public async Task Static_Field_Inference_3()
{
// arrange
// act
var schema =
async Task Error() =>
await new ServiceCollection()
.AddGraphQL()
.AddQueryType<WithStaticField>()
.ModifyOptions(o => o.DefaultBindingBehavior = BindingBehavior.Explicit)
.BuildSchemaAsync();

// assert
SnapshotExtensions.MatchSnapshot(schema);
}

[Fact]
public async Task Static_Field_Inference_3_Execute()
{
// arrange
// act
var result =
await new ServiceCollection()
.AddGraphQL()
.AddQueryType<WithStaticField>()
.ModifyOptions(o => o.DefaultBindingBehavior = BindingBehavior.Explicit)
.ExecuteRequestAsync("{ hello }");

// assert
SnapshotExtensions.MatchSnapshot(result);
await Assert.ThrowsAsync<SchemaException>(Error);
}

[Fact]
Expand Down Expand Up @@ -2208,6 +2192,18 @@ public void AbstractResolver_UsingMethodInfo_ShouldThrow()
Assert.Contains("non-abstract type is required", ex.Message);
}

[Fact]
public async Task Ignore_Generic_Methods()
{
var schema =
await new ServiceCollection()
.AddGraphQL()
.AddQueryType<QueryWithGenerics>()
.BuildSchemaAsync();

SnapshotExtensions.MatchSnapshot(schema);
}

public abstract class ResolverBase
{
public int GetValue() => 1024;
Expand Down Expand Up @@ -2530,4 +2526,11 @@ public class Book

public static bool IsComic => true;
}

public class QueryWithGenerics
{
public string Bar() => "bar";

public T Foo<T>() => default!;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
schema {
query: QueryWithGenerics
}

type QueryWithGenerics {
bar: String!
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
schema {
query: RootQuery
}

"The root query"
type RootQuery {
"Returns testDataType."
testData: TestDataType
}

"Test Data Type."
type TestDataType {
"Id"
id: String
}

0 comments on commit 9274bc9

Please sign in to comment.