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

Added default binding behavior option. #963

Merged
merged 9 commits into from
Aug 6, 2019
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
22 changes: 4 additions & 18 deletions build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -222,28 +222,21 @@ Task("RedisTests")
.IsDependentOn("EnvironmentSetup")
.Does(() =>
{
var buildSettings = new DotNetCoreBuildSettings
{
Configuration = "Debug"
};

int i = 0;
var testSettings = new DotNetCoreTestSettings
{
Configuration = "Debug",
ResultsDirectory = $"./{testOutputDir}",
Logger = "trx",
NoRestore = true,
NoBuild = true,
NoRestore = false,
NoBuild = false,
ArgumentCustomization = args => args
.Append("/p:CollectCoverage=true")
.Append("/p:Exclude=[xunit.*]*")
.Append("/p:CoverletOutputFormat=opencover")
.Append($"/p:CoverletOutput=\"../../{testOutputDir}/core_{i++}\" --blame")
};

DotNetCoreBuild("./tools/Build.Core.sln", buildSettings);

foreach(var file in GetFiles("./src/**/*.Tests.csproj"))
{
if(file.FullPath.Contains("Redis"))
Expand All @@ -257,28 +250,21 @@ Task("MongoTests")
.IsDependentOn("EnvironmentSetup")
.Does(() =>
{
var buildSettings = new DotNetCoreBuildSettings
{
Configuration = "Debug"
};

int i = 0;
var testSettings = new DotNetCoreTestSettings
{
Configuration = "Debug",
ResultsDirectory = $"./{testOutputDir}",
Logger = "trx",
NoRestore = true,
NoBuild = true,
NoRestore = false,
NoBuild = false,
ArgumentCustomization = args => args
.Append("/p:CollectCoverage=true")
.Append("/p:Exclude=[xunit.*]*")
.Append("/p:CoverletOutputFormat=opencover")
.Append($"/p:CoverletOutput=\"../../{testOutputDir}/core_{i++}\" --blame")
};

DotNetCoreBuild("./tools/Build.Core.sln", buildSettings);

foreach(var file in GetFiles("./src/**/*.Tests.csproj"))
{
if(file.FullPath.Contains("Mongo"))
Expand Down
2 changes: 2 additions & 0 deletions src/Core/Types.Filters/FilterFieldDescriptorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ protected FilterFieldDescriptorBase(
Definition.Description = context.Naming.GetMemberDescription(
property, MemberKind.InputObjectField);
Definition.Type = context.Inspector.GetInputReturnType(property);
Definition.Filters.BindingBehavior =
context.Options.DefaultBindingBehavior;
}

protected override FilterFieldDefintion Definition { get; } =
Expand Down
2 changes: 2 additions & 0 deletions src/Core/Types.Filters/FilterInputTypeDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ protected FilterInputTypeDescriptor(
// TODO : should we rework get type description?
Definition.Description = context.Naming.GetTypeDescription(
entityType, TypeKind.Object);
Definition.Fields.BindingBehavior =
context.Options.DefaultBindingBehavior;
}

protected override FilterInputTypeDefinition Definition { get; } =
Expand Down
57 changes: 57 additions & 0 deletions src/Core/Types.Tests/Configuration/ReadOnlySchemaOptionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using HotChocolate.Types;
using Xunit;
using Snapshooter.Xunit;
using HotChocolate.Configuration;
using System;

namespace HotChocolate
{
public class ReadOnlySchemaOptionsTests
{
[Fact]
public void Copy_Options()
{
// arrange
var options = new SchemaOptions
{
QueryTypeName = "A",
MutationTypeName = "B",
SubscriptionTypeName = "C",
StrictValidation = false,
UseXmlDocumentation = false,
DefaultBindingBehavior = BindingBehavior.Explicit,
FieldMiddleware = FieldMiddlewareApplication.AllFields
};

// act
var copied = new ReadOnlySchemaOptions(options);

// assert
copied.MatchSnapshot();
}

[Fact]
public void Copy_Options_Defaults()
{
// arrange
var options = new SchemaOptions();

// act
var copied = new ReadOnlySchemaOptions(options);

// assert
copied.MatchSnapshot();
}

[Fact]
public void Create_Options_Null()
{
// arrange
// act
Action action = () => new ReadOnlySchemaOptions(null);

// assert
Assert.Throws<ArgumentNullException>(action);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"QueryTypeName": "A",
"MutationTypeName": "B",
"SubscriptionTypeName": "C",
"StrictValidation": false,
"UseXmlDocumentation": false,
"DefaultBindingBehavior": "Explicit",
"FieldMiddleware": "AllFields"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"QueryTypeName": "Query",
"MutationTypeName": "Mutation",
"SubscriptionTypeName": "Subscription",
"StrictValidation": true,
"UseXmlDocumentation": true,
"DefaultBindingBehavior": "Implicit",
"FieldMiddleware": "UserDefinedFields"
}
48 changes: 48 additions & 0 deletions src/Core/Types.Tests/Types/DirectiveTypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,40 @@ public void ConfigureTypedDirectiveWithResolver()
Assert.True(directiveType.IsExecutable);
Assert.NotEmpty(directiveType.MiddlewareComponents);
Assert.Equal(typeof(CustomDirective), directiveType.ClrType);
Assert.Collection(directiveType.Arguments,
t => Assert.Equal("argument", t.Name.Value));
}

[Fact]
public void ConfigureTypedDirective_DefaultBinding_Explicit()
{
// arrange
// act
DirectiveType directiveType =
CreateDirective(new CustomDirectiveType(),
b => b.ModifyOptions(o =>
o.DefaultBindingBehavior = BindingBehavior.Explicit));

// assert
Assert.True(directiveType.IsExecutable);
Assert.NotEmpty(directiveType.MiddlewareComponents);
Assert.Equal(typeof(CustomDirective), directiveType.ClrType);
Assert.Empty(directiveType.Arguments);
}

[Fact]
public void ConfigureTypedDirectiveNoArguments()
{
// arrange
// act
DirectiveType directiveType =
CreateDirective(new Custom2DirectiveType());

// assert
Assert.True(directiveType.IsExecutable);
Assert.NotEmpty(directiveType.MiddlewareComponents);
Assert.Equal(typeof(CustomDirective), directiveType.ClrType);
Assert.Empty(directiveType.Arguments);
}

[Fact]
Expand Down Expand Up @@ -481,6 +515,20 @@ protected override void Configure(
}
}

public class Custom2DirectiveType
: DirectiveType<CustomDirective>
{
protected override void Configure(
IDirectiveTypeDescriptor<CustomDirective> descriptor)
{
descriptor.Name("Custom");
descriptor.Location(DirectiveLocation.Enum);
descriptor.Location(DirectiveLocation.Field);
descriptor.Use(next => context => Task.CompletedTask);
descriptor.BindArgumentsImplicitly().BindArgumentsExplicitly();
}
}

public class DirectiveMiddleware
{
private FieldDelegate _next;
Expand Down
23 changes: 23 additions & 0 deletions src/Core/Types.Tests/Types/EnumTypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,29 @@ public void ExplicitEnumType_OnlyContainDeclaredValues()
Assert.Null(value);
}

[Fact]
public void ExplicitEnumType_OnlyContainDeclaredValues_2()
{
// act
var schema = Schema.Create(c =>
{
c.RegisterType(new EnumType<Foo>(d =>
{
d.BindItemsImplicitly().BindItemsExplicitly();
d.Item(Foo.Bar1);
}));
c.Options.StrictValidation = false;
});

// assert
EnumType type = schema.GetType<EnumType>("Foo");
Assert.NotNull(type);
Assert.True(type.TryGetValue("BAR1", out object value));
Assert.Equal(Foo.Bar1, value);
Assert.False(type.TryGetValue("BAR2", out value));
Assert.Null(value);
}

[Fact]
public void ImplicitEnumType_OnlyBar1HasCustomName()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace HotChocolate.Configuration
using HotChocolate.Types;

namespace HotChocolate.Configuration
{
public interface IReadOnlySchemaOptions
{
Expand All @@ -12,6 +14,11 @@ public interface IReadOnlySchemaOptions

bool UseXmlDocumentation { get; }

/// <summary>
/// Defines the default binding behavior.
/// </summary>
BindingBehavior DefaultBindingBehavior { get; }

/// <summary>
/// Defines on which fields a middleware pipeline can be applied on.
/// </summary>
Expand Down
6 changes: 5 additions & 1 deletion src/Core/Types/Configuration/Contracts/ISchemaOptions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace HotChocolate.Configuration
using HotChocolate.Types;

namespace HotChocolate.Configuration
{
public interface ISchemaOptions
: IReadOnlySchemaOptions
Expand All @@ -13,6 +15,8 @@ public interface ISchemaOptions

new bool UseXmlDocumentation { get; set; }

new BindingBehavior DefaultBindingBehavior { get; set; }

new FieldMiddlewareApplication FieldMiddleware { get; set; }
}
}
4 changes: 4 additions & 0 deletions src/Core/Types/Configuration/ReadOnlySchemaOptions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using HotChocolate.Types;

namespace HotChocolate.Configuration
{
Expand All @@ -20,6 +21,7 @@ public ReadOnlySchemaOptions(IReadOnlySchemaOptions options)
?? "Subscription";
StrictValidation = options.StrictValidation;
UseXmlDocumentation = options.UseXmlDocumentation;
DefaultBindingBehavior = options.DefaultBindingBehavior;
FieldMiddleware = options.FieldMiddleware;
}

Expand All @@ -33,6 +35,8 @@ public ReadOnlySchemaOptions(IReadOnlySchemaOptions options)

public bool UseXmlDocumentation { get; }

public BindingBehavior DefaultBindingBehavior { get; }

public FieldMiddlewareApplication FieldMiddleware { get; }
}
}
8 changes: 7 additions & 1 deletion src/Core/Types/Configuration/SchemaOptions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using HotChocolate.Types;

namespace HotChocolate.Configuration
{
public class SchemaOptions
Expand All @@ -13,6 +15,9 @@ public class SchemaOptions

public bool UseXmlDocumentation { get; set; } = true;

public BindingBehavior DefaultBindingBehavior { get; set; } =
BindingBehavior.Implicit;

public FieldMiddlewareApplication FieldMiddleware
{
get;
Expand All @@ -28,7 +33,8 @@ public static SchemaOptions FromOptions(IReadOnlySchemaOptions options)
SubscriptionTypeName = options.SubscriptionTypeName,
StrictValidation = options.StrictValidation,
UseXmlDocumentation = options.UseXmlDocumentation,
FieldMiddleware = options.FieldMiddleware
FieldMiddleware = options.FieldMiddleware,
DefaultBindingBehavior = options.DefaultBindingBehavior
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ IDirectiveTypeDescriptor<T> SyntaxNode(
IDirectiveTypeDescriptor<T> BindArguments(
BindingBehavior behavior);

/// <summary>
/// Defines that all arguments have to be specified explicitly.
/// </summary>
IDirectiveTypeDescriptor<T> BindArgumentsExplicitly();

/// <summary>
/// The directive type will add arguments for all compatible properties.
/// </summary>
IDirectiveTypeDescriptor<T> BindArgumentsImplicitly();

/// <summary>
/// Specifies a directive argument.
/// </summary>
Expand Down
11 changes: 11 additions & 0 deletions src/Core/Types/Types/Descriptors/Contracts/IEnumTypeDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ IEnumValueDescriptor Value<T>(
IEnumTypeDescriptor BindItems(
BindingBehavior behavior);

/// <summary>
/// Defines that all enum values have to be specified explicitly.
/// </summary>
IEnumTypeDescriptor BindItemsExplicitly();

/// <summary>
/// Defines that all enum values shall be infered
/// from the associated .Net type,
/// </summary>
IEnumTypeDescriptor BindItemsImplicitly();

IEnumTypeDescriptor Directive<T>(
T directiveInstance)
where T : class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ IEnumTypeDescriptor<T> SyntaxNode(

IEnumTypeDescriptor<T> BindItems(BindingBehavior behavior);

/// <summary>
/// Defines that all enum values have to be specified explicitly.
/// </summary>
IEnumTypeDescriptor<T> BindItemsExplicitly();

/// <summary>
/// Defines that all enum values shall be infered
/// from the associated .Net type,
/// </summary>
IEnumTypeDescriptor<T> BindItemsImplicitly();

IEnumTypeDescriptor<T> Directive<TDirective>(
TDirective directiveInstance)
where TDirective : class;
Expand Down
Loading