From e1a457654be7b8c8d305ee93ebbaac40c8dede7d Mon Sep 17 00:00:00 2001 From: Michael Staib Date: Tue, 6 Aug 2019 21:46:26 +0200 Subject: [PATCH 1/7] Updated Descriptor Interfaces --- src/Core/Types.Filters/FilterFieldDescriptorBase.cs | 2 ++ src/Core/Types.Filters/FilterInputTypeDescriptor.cs | 2 ++ .../Configuration/Contracts/IReadOnlySchemaOptions.cs | 6 +++++- .../Contracts/IDirectiveTypeDescriptor~1.cs | 10 ++++++++++ .../Descriptors/Contracts/IEnumTypeDescriptor.cs | 11 +++++++++++ .../Contracts/IInputObjectTypeDescriptor~1.cs | 11 +++++++++++ .../Contracts/IInterfaceTypeDescriptor~1.cs | 11 +++++++++++ .../Descriptors/Contracts/IObjectTypeDescriptor~1.cs | 11 +++++++++++ .../Descriptors/Definitions/FieldDefinitionBase.cs | 2 +- .../Types/Descriptors/DirectiveTypeDescriptor~1.cs | 2 ++ .../Types/Types/Descriptors/EnumTypeDescriptor.cs | 4 ++++ .../Types/Descriptors/InputObjectTypeDescriptor~1.cs | 2 ++ .../Types/Descriptors/InterfaceTypeDescriptor~1.cs | 2 ++ .../Types/Types/Descriptors/ObjectTypeDescriptor~1.cs | 2 ++ 14 files changed, 76 insertions(+), 2 deletions(-) diff --git a/src/Core/Types.Filters/FilterFieldDescriptorBase.cs b/src/Core/Types.Filters/FilterFieldDescriptorBase.cs index 2bd946542df..356d2628a2b 100644 --- a/src/Core/Types.Filters/FilterFieldDescriptorBase.cs +++ b/src/Core/Types.Filters/FilterFieldDescriptorBase.cs @@ -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; } = diff --git a/src/Core/Types.Filters/FilterInputTypeDescriptor.cs b/src/Core/Types.Filters/FilterInputTypeDescriptor.cs index d071b947f14..d9e8642e113 100644 --- a/src/Core/Types.Filters/FilterInputTypeDescriptor.cs +++ b/src/Core/Types.Filters/FilterInputTypeDescriptor.cs @@ -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; } = diff --git a/src/Core/Types/Configuration/Contracts/IReadOnlySchemaOptions.cs b/src/Core/Types/Configuration/Contracts/IReadOnlySchemaOptions.cs index 8e42eae987a..9728eb9c372 100644 --- a/src/Core/Types/Configuration/Contracts/IReadOnlySchemaOptions.cs +++ b/src/Core/Types/Configuration/Contracts/IReadOnlySchemaOptions.cs @@ -1,4 +1,6 @@ -namespace HotChocolate.Configuration +using HotChocolate.Types; + +namespace HotChocolate.Configuration { public interface IReadOnlySchemaOptions { @@ -11,5 +13,7 @@ public interface IReadOnlySchemaOptions bool StrictValidation { get; } bool UseXmlDocumentation { get; } + + BindingBehavior DefaultBindingBehavior { get; } } } diff --git a/src/Core/Types/Types/Descriptors/Contracts/IDirectiveTypeDescriptor~1.cs b/src/Core/Types/Types/Descriptors/Contracts/IDirectiveTypeDescriptor~1.cs index 6b87fda8efd..034123e089a 100644 --- a/src/Core/Types/Types/Descriptors/Contracts/IDirectiveTypeDescriptor~1.cs +++ b/src/Core/Types/Types/Descriptors/Contracts/IDirectiveTypeDescriptor~1.cs @@ -59,6 +59,16 @@ IDirectiveTypeDescriptor SyntaxNode( IDirectiveTypeDescriptor BindArguments( BindingBehavior behavior); + /// + /// Defines that all arguments have to be specified explicitly. + /// + IDirectiveTypeDescriptor BindArgumentsExplicitly(); + + /// + /// The directive type will add arguments for all compatible properties. + /// + IDirectiveTypeDescriptor BindArgumentsImplicitly(); + /// /// Specifies a directive argument. /// diff --git a/src/Core/Types/Types/Descriptors/Contracts/IEnumTypeDescriptor.cs b/src/Core/Types/Types/Descriptors/Contracts/IEnumTypeDescriptor.cs index af2303149a3..8f81b113ba7 100644 --- a/src/Core/Types/Types/Descriptors/Contracts/IEnumTypeDescriptor.cs +++ b/src/Core/Types/Types/Descriptors/Contracts/IEnumTypeDescriptor.cs @@ -44,6 +44,17 @@ IEnumValueDescriptor Value( IEnumTypeDescriptor BindItems( BindingBehavior behavior); + /// + /// Defines that all enum values have to be specified explicitly. + /// + IEnumTypeDescriptor BindItemsExplicitly(); + + /// + /// Defines that all enum values shall be infered + /// from the associated .Net type, + /// + IEnumTypeDescriptor BindItemsImplicitly(); + IEnumTypeDescriptor Directive( T directiveInstance) where T : class; diff --git a/src/Core/Types/Types/Descriptors/Contracts/IInputObjectTypeDescriptor~1.cs b/src/Core/Types/Types/Descriptors/Contracts/IInputObjectTypeDescriptor~1.cs index 5014469e091..51a595b0f5c 100644 --- a/src/Core/Types/Types/Descriptors/Contracts/IInputObjectTypeDescriptor~1.cs +++ b/src/Core/Types/Types/Descriptors/Contracts/IInputObjectTypeDescriptor~1.cs @@ -19,6 +19,17 @@ IInputObjectTypeDescriptor SyntaxNode( IInputObjectTypeDescriptor BindFields( BindingBehavior behavior); + /// + /// Defines that all fields have to be specified explicitly. + /// + IInputObjectTypeDescriptor BindFieldsExplicitly(); + + /// + /// Defines that all fields shall be infered + /// from the associated .Net type, + /// + IInputObjectTypeDescriptor BindFieldsImplicitly(); + IInputFieldDescriptor Field(NameString name); IInputFieldDescriptor Field( diff --git a/src/Core/Types/Types/Descriptors/Contracts/IInterfaceTypeDescriptor~1.cs b/src/Core/Types/Types/Descriptors/Contracts/IInterfaceTypeDescriptor~1.cs index ff8bbfe948b..8ed37abeb75 100644 --- a/src/Core/Types/Types/Descriptors/Contracts/IInterfaceTypeDescriptor~1.cs +++ b/src/Core/Types/Types/Descriptors/Contracts/IInterfaceTypeDescriptor~1.cs @@ -58,6 +58,17 @@ IInterfaceTypeDescriptor SyntaxNode( /// IInterfaceTypeDescriptor BindFields(BindingBehavior behavior); + /// + /// Defines that all fields have to be specified explicitly. + /// + IInterfaceTypeDescriptor BindFieldsExplicitly(); + + /// + /// Defines that all fields shall be infered + /// from the associated .Net type, + /// + IInterfaceTypeDescriptor BindFieldsImplicitly(); + IInterfaceTypeDescriptor ResolveAbstractType( ResolveAbstractType typeResolver); diff --git a/src/Core/Types/Types/Descriptors/Contracts/IObjectTypeDescriptor~1.cs b/src/Core/Types/Types/Descriptors/Contracts/IObjectTypeDescriptor~1.cs index 69bf5a5fb6c..75a9d414d68 100644 --- a/src/Core/Types/Types/Descriptors/Contracts/IObjectTypeDescriptor~1.cs +++ b/src/Core/Types/Types/Descriptors/Contracts/IObjectTypeDescriptor~1.cs @@ -47,6 +47,17 @@ public interface IObjectTypeDescriptor /// IObjectTypeDescriptor BindFields(BindingBehavior behavior); + /// + /// Defines that all fields have to be specified explicitly. + /// + IObjectTypeDescriptor BindFieldsExplicitly(); + + /// + /// Defines that all fields shall be infered + /// from the associated .Net type, + /// + IObjectTypeDescriptor BindFieldsImplicitly(); + /// /// Specifies an interface that is implemented by the /// . diff --git a/src/Core/Types/Types/Descriptors/Definitions/FieldDefinitionBase.cs b/src/Core/Types/Types/Descriptors/Definitions/FieldDefinitionBase.cs index e84e1a67011..7d551ca0775 100644 --- a/src/Core/Types/Types/Descriptors/Definitions/FieldDefinitionBase.cs +++ b/src/Core/Types/Types/Descriptors/Definitions/FieldDefinitionBase.cs @@ -21,6 +21,6 @@ public abstract class FieldDefinitionBase /// Gets the list of directives that are annotated to this field. /// public IList Directives { get; } = - new List(); + new List(); } } diff --git a/src/Core/Types/Types/Descriptors/DirectiveTypeDescriptor~1.cs b/src/Core/Types/Types/Descriptors/DirectiveTypeDescriptor~1.cs index c1505e7939f..0bc647cebcd 100644 --- a/src/Core/Types/Types/Descriptors/DirectiveTypeDescriptor~1.cs +++ b/src/Core/Types/Types/Descriptors/DirectiveTypeDescriptor~1.cs @@ -18,6 +18,8 @@ public class DirectiveTypeDescriptor protected internal DirectiveTypeDescriptor(IDescriptorContext context) : base(context, typeof(T)) { + Definition.Arguments.BindingBehavior = + context.Options.DefaultBindingBehavior; } Type IHasClrType.ClrType => Definition.ClrType; diff --git a/src/Core/Types/Types/Descriptors/EnumTypeDescriptor.cs b/src/Core/Types/Types/Descriptors/EnumTypeDescriptor.cs index 26ca3069bc1..6d254711eb3 100644 --- a/src/Core/Types/Types/Descriptors/EnumTypeDescriptor.cs +++ b/src/Core/Types/Types/Descriptors/EnumTypeDescriptor.cs @@ -14,6 +14,8 @@ protected EnumTypeDescriptor(IDescriptorContext context) : base(context) { Definition.ClrType = typeof(object); + Definition.Values.BindingBehavior = + context.Options.DefaultBindingBehavior; } protected EnumTypeDescriptor(IDescriptorContext context, Type clrType) @@ -25,6 +27,8 @@ protected EnumTypeDescriptor(IDescriptorContext context, Type clrType) clrType, TypeKind.Enum); Definition.Description = context.Naming.GetTypeDescription( clrType, TypeKind.Enum); + Definition.Values.BindingBehavior = + context.Options.DefaultBindingBehavior; } protected override EnumTypeDefinition Definition { get; } = diff --git a/src/Core/Types/Types/Descriptors/InputObjectTypeDescriptor~1.cs b/src/Core/Types/Types/Descriptors/InputObjectTypeDescriptor~1.cs index 8a310e8e666..b4ea8028d9f 100644 --- a/src/Core/Types/Types/Descriptors/InputObjectTypeDescriptor~1.cs +++ b/src/Core/Types/Types/Descriptors/InputObjectTypeDescriptor~1.cs @@ -16,6 +16,8 @@ public class InputObjectTypeDescriptor protected internal InputObjectTypeDescriptor(IDescriptorContext context) : base(context, typeof(T)) { + Definition.Fields.BindingBehavior = + context.Options.DefaultBindingBehavior; } Type IHasClrType.ClrType => Definition.ClrType; diff --git a/src/Core/Types/Types/Descriptors/InterfaceTypeDescriptor~1.cs b/src/Core/Types/Types/Descriptors/InterfaceTypeDescriptor~1.cs index 66b451958d1..3fc0b3a1914 100644 --- a/src/Core/Types/Types/Descriptors/InterfaceTypeDescriptor~1.cs +++ b/src/Core/Types/Types/Descriptors/InterfaceTypeDescriptor~1.cs @@ -16,6 +16,8 @@ public class InterfaceTypeDescriptor protected internal InterfaceTypeDescriptor(IDescriptorContext context) : base(context, typeof(T)) { + Definition.Fields.BindingBehavior = + context.Options.DefaultBindingBehavior; } Type IHasClrType.ClrType => Definition.ClrType; diff --git a/src/Core/Types/Types/Descriptors/ObjectTypeDescriptor~1.cs b/src/Core/Types/Types/Descriptors/ObjectTypeDescriptor~1.cs index 78546e5f325..d313f0efdf6 100644 --- a/src/Core/Types/Types/Descriptors/ObjectTypeDescriptor~1.cs +++ b/src/Core/Types/Types/Descriptors/ObjectTypeDescriptor~1.cs @@ -15,6 +15,8 @@ public class ObjectTypeDescriptor public ObjectTypeDescriptor(IDescriptorContext context) : base(context, typeof(T)) { + Definition.Fields.BindingBehavior = + context.Options.DefaultBindingBehavior; } Type IHasClrType.ClrType => Definition.ClrType; From a21fd8826f3a9fe649a0827d929f5c6d94abab31 Mon Sep 17 00:00:00 2001 From: Michael Staib Date: Tue, 6 Aug 2019 21:54:59 +0200 Subject: [PATCH 2/7] Implemented new interfaces --- src/Core/Types/Configuration/Contracts/ISchemaOptions.cs | 6 +++++- src/Core/Types/Configuration/ReadOnlySchemaOptions.cs | 4 ++++ src/Core/Types/Configuration/SchemaOptions.cs | 5 +++++ .../Types/Types/Descriptors/DirectiveTypeDescriptor~1.cs | 6 ++++++ src/Core/Types/Types/Descriptors/EnumTypeDescriptor.cs | 6 ++++++ .../Types/Types/Descriptors/InputObjectTypeDescriptor~1.cs | 6 ++++++ .../Types/Types/Descriptors/InterfaceTypeDescriptor~1.cs | 6 ++++++ src/Core/Types/Types/Descriptors/ObjectTypeDescriptor~1.cs | 6 ++++++ 8 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/Core/Types/Configuration/Contracts/ISchemaOptions.cs b/src/Core/Types/Configuration/Contracts/ISchemaOptions.cs index 9ad7fe44089..7007daa8306 100644 --- a/src/Core/Types/Configuration/Contracts/ISchemaOptions.cs +++ b/src/Core/Types/Configuration/Contracts/ISchemaOptions.cs @@ -1,4 +1,6 @@ -namespace HotChocolate.Configuration +using HotChocolate.Types; + +namespace HotChocolate.Configuration { public interface ISchemaOptions : IReadOnlySchemaOptions @@ -12,5 +14,7 @@ public interface ISchemaOptions new bool StrictValidation { get; set; } new bool UseXmlDocumentation { get; set; } + + new BindingBehavior DefaultBindingBehavior { get; set; } } } diff --git a/src/Core/Types/Configuration/ReadOnlySchemaOptions.cs b/src/Core/Types/Configuration/ReadOnlySchemaOptions.cs index 3432e64f9c3..dd92eac3fdf 100644 --- a/src/Core/Types/Configuration/ReadOnlySchemaOptions.cs +++ b/src/Core/Types/Configuration/ReadOnlySchemaOptions.cs @@ -1,4 +1,5 @@ using System; +using HotChocolate.Types; namespace HotChocolate.Configuration { @@ -20,6 +21,7 @@ public ReadOnlySchemaOptions(IReadOnlySchemaOptions options) ?? "Subscription"; StrictValidation = options.StrictValidation; UseXmlDocumentation = options.UseXmlDocumentation; + DefaultBindingBehavior = options.DefaultBindingBehavior; } public string QueryTypeName { get; } @@ -31,5 +33,7 @@ public ReadOnlySchemaOptions(IReadOnlySchemaOptions options) public bool StrictValidation { get; } public bool UseXmlDocumentation { get; } + + public BindingBehavior DefaultBindingBehavior { get; } } } diff --git a/src/Core/Types/Configuration/SchemaOptions.cs b/src/Core/Types/Configuration/SchemaOptions.cs index 0733ba31b19..5062b71cf3b 100644 --- a/src/Core/Types/Configuration/SchemaOptions.cs +++ b/src/Core/Types/Configuration/SchemaOptions.cs @@ -1,3 +1,5 @@ +using HotChocolate.Types; + namespace HotChocolate.Configuration { public class SchemaOptions @@ -13,6 +15,9 @@ public class SchemaOptions public bool UseXmlDocumentation { get; set; } = true; + public BindingBehavior DefaultBindingBehavior { get; set; } = + BindingBehavior.Implicit; + public static SchemaOptions FromOptions(IReadOnlySchemaOptions options) { return new SchemaOptions diff --git a/src/Core/Types/Types/Descriptors/DirectiveTypeDescriptor~1.cs b/src/Core/Types/Types/Descriptors/DirectiveTypeDescriptor~1.cs index 0bc647cebcd..47cfe2851f3 100644 --- a/src/Core/Types/Types/Descriptors/DirectiveTypeDescriptor~1.cs +++ b/src/Core/Types/Types/Descriptors/DirectiveTypeDescriptor~1.cs @@ -72,6 +72,12 @@ public IDirectiveTypeDescriptor BindArguments( return this; } + public IDirectiveTypeDescriptor BindArgumentsExplicitly() => + BindArguments(BindingBehavior.Explicit); + + public IDirectiveTypeDescriptor BindArgumentsImplicitly() => + BindArguments(BindingBehavior.Implicit); + public IDirectiveArgumentDescriptor Argument( Expression> property) { diff --git a/src/Core/Types/Types/Descriptors/EnumTypeDescriptor.cs b/src/Core/Types/Types/Descriptors/EnumTypeDescriptor.cs index 6d254711eb3..5e2dc4faec4 100644 --- a/src/Core/Types/Types/Descriptors/EnumTypeDescriptor.cs +++ b/src/Core/Types/Types/Descriptors/EnumTypeDescriptor.cs @@ -100,6 +100,12 @@ public IEnumTypeDescriptor BindItems( return this; } + public IEnumTypeDescriptor BindItemsExplicitly() => + BindItems(BindingBehavior.Explicit); + + public IEnumTypeDescriptor BindItemsImplicitly() => + BindItems(BindingBehavior.Implicit); + public IEnumValueDescriptor Item(T value) { var descriptor = new EnumValueDescriptor(Context, value); diff --git a/src/Core/Types/Types/Descriptors/InputObjectTypeDescriptor~1.cs b/src/Core/Types/Types/Descriptors/InputObjectTypeDescriptor~1.cs index b4ea8028d9f..7cf2f173da9 100644 --- a/src/Core/Types/Types/Descriptors/InputObjectTypeDescriptor~1.cs +++ b/src/Core/Types/Types/Descriptors/InputObjectTypeDescriptor~1.cs @@ -66,6 +66,12 @@ public IInputObjectTypeDescriptor BindFields( return this; } + public IInputObjectTypeDescriptor BindFieldsExplicitly() => + BindFields(BindingBehavior.Explicit); + + public IInputObjectTypeDescriptor BindFieldsImplicitly() => + BindFields(BindingBehavior.Implicit); + public IInputFieldDescriptor Field( Expression> property) { diff --git a/src/Core/Types/Types/Descriptors/InterfaceTypeDescriptor~1.cs b/src/Core/Types/Types/Descriptors/InterfaceTypeDescriptor~1.cs index 3fc0b3a1914..2328a200882 100644 --- a/src/Core/Types/Types/Descriptors/InterfaceTypeDescriptor~1.cs +++ b/src/Core/Types/Types/Descriptors/InterfaceTypeDescriptor~1.cs @@ -66,6 +66,12 @@ public IInterfaceTypeDescriptor BindFields( return this; } + public IInterfaceTypeDescriptor BindFieldsExplicitly() => + BindFields(BindingBehavior.Explicit); + + public IInterfaceTypeDescriptor BindFieldsImplicitly() => + BindFields(BindingBehavior.Implicit); + public IInterfaceFieldDescriptor Field( Expression> propertyOrMethod) { diff --git a/src/Core/Types/Types/Descriptors/ObjectTypeDescriptor~1.cs b/src/Core/Types/Types/Descriptors/ObjectTypeDescriptor~1.cs index d313f0efdf6..fa1cd05e210 100644 --- a/src/Core/Types/Types/Descriptors/ObjectTypeDescriptor~1.cs +++ b/src/Core/Types/Types/Descriptors/ObjectTypeDescriptor~1.cs @@ -59,6 +59,12 @@ public IObjectTypeDescriptor BindFields( return this; } + public IObjectTypeDescriptor BindFieldsExplicitly() => + BindFields(BindingBehavior.Explicit); + + public IObjectTypeDescriptor BindFieldsImplicitly() => + BindFields(BindingBehavior.Implicit); + public new IObjectTypeDescriptor Interface() where TInterface : InterfaceType { From 2498f48bc45e28a365313a5c3847d4fd85a7be73 Mon Sep 17 00:00:00 2001 From: Michael Staib Date: Tue, 6 Aug 2019 22:05:42 +0200 Subject: [PATCH 3/7] Updated Build --- build.cake | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/build.cake b/build.cake index 81525f0f5dc..71f14416fd1 100644 --- a/build.cake +++ b/build.cake @@ -222,19 +222,14 @@ 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.*]*") @@ -242,8 +237,6 @@ Task("RedisTests") .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")) @@ -257,19 +250,14 @@ 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.*]*") @@ -277,8 +265,6 @@ Task("MongoTests") .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")) From 3932e96e4a1887104dbc1baca9e2c51fa1429ab8 Mon Sep 17 00:00:00 2001 From: Michael Staib Date: Tue, 6 Aug 2019 22:44:32 +0200 Subject: [PATCH 4/7] more refinements --- .../ReadOnlySchemaOptionsTests.cs | 45 +++++++++++++++++ ...adOnlySchemaOptionsTests.Copy_Options.snap | 9 ++++ ...emaOptionsTests.Copy_Options_Defaults.snap | 9 ++++ .../Types.Tests/Types/DirectiveTypeTests.cs | 48 +++++++++++++++++++ src/Core/Types.Tests/Types/EnumTypeTests.cs | 23 +++++++++ .../Contracts/IEnumTypeDescriptor~1.cs | 11 +++++ .../Types/Descriptors/EnumTypeDescriptor~1.cs | 6 +++ 7 files changed, 151 insertions(+) create mode 100644 src/Core/Types.Tests/Configuration/ReadOnlySchemaOptionsTests.cs create mode 100644 src/Core/Types.Tests/Configuration/__snapshots__/ReadOnlySchemaOptionsTests.Copy_Options.snap create mode 100644 src/Core/Types.Tests/Configuration/__snapshots__/ReadOnlySchemaOptionsTests.Copy_Options_Defaults.snap diff --git a/src/Core/Types.Tests/Configuration/ReadOnlySchemaOptionsTests.cs b/src/Core/Types.Tests/Configuration/ReadOnlySchemaOptionsTests.cs new file mode 100644 index 00000000000..a8742d69025 --- /dev/null +++ b/src/Core/Types.Tests/Configuration/ReadOnlySchemaOptionsTests.cs @@ -0,0 +1,45 @@ +using HotChocolate.Types; +using Xunit; +using Snapshooter.Xunit; +using HotChocolate.Configuration; + +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 + options.MatchSnapshot(); + } + + [Fact] + public void Copy_Options_Defaults() + { + // arrange + var options = new SchemaOptions(); + + // act + var copied = new ReadOnlySchemaOptions(options); + + // assert + options.MatchSnapshot(); + } + } +} diff --git a/src/Core/Types.Tests/Configuration/__snapshots__/ReadOnlySchemaOptionsTests.Copy_Options.snap b/src/Core/Types.Tests/Configuration/__snapshots__/ReadOnlySchemaOptionsTests.Copy_Options.snap new file mode 100644 index 00000000000..21cabdb0d7d --- /dev/null +++ b/src/Core/Types.Tests/Configuration/__snapshots__/ReadOnlySchemaOptionsTests.Copy_Options.snap @@ -0,0 +1,9 @@ +{ + "QueryTypeName": "A", + "MutationTypeName": "B", + "SubscriptionTypeName": "C", + "StrictValidation": false, + "UseXmlDocumentation": false, + "DefaultBindingBehavior": "Explicit", + "FieldMiddleware": "AllFields" +} diff --git a/src/Core/Types.Tests/Configuration/__snapshots__/ReadOnlySchemaOptionsTests.Copy_Options_Defaults.snap b/src/Core/Types.Tests/Configuration/__snapshots__/ReadOnlySchemaOptionsTests.Copy_Options_Defaults.snap new file mode 100644 index 00000000000..fcbbc524d36 --- /dev/null +++ b/src/Core/Types.Tests/Configuration/__snapshots__/ReadOnlySchemaOptionsTests.Copy_Options_Defaults.snap @@ -0,0 +1,9 @@ +{ + "QueryTypeName": null, + "MutationTypeName": null, + "SubscriptionTypeName": null, + "StrictValidation": true, + "UseXmlDocumentation": true, + "DefaultBindingBehavior": "Implicit", + "FieldMiddleware": "UserDefinedFields" +} diff --git a/src/Core/Types.Tests/Types/DirectiveTypeTests.cs b/src/Core/Types.Tests/Types/DirectiveTypeTests.cs index 37825f2f1ff..c0788cf2cec 100644 --- a/src/Core/Types.Tests/Types/DirectiveTypeTests.cs +++ b/src/Core/Types.Tests/Types/DirectiveTypeTests.cs @@ -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] @@ -481,6 +515,20 @@ protected override void Configure( } } + public class Custom2DirectiveType + : DirectiveType + { + protected override void Configure( + IDirectiveTypeDescriptor descriptor) + { + descriptor.Name("Custom"); + descriptor.Location(DirectiveLocation.Enum); + descriptor.Location(DirectiveLocation.Field); + descriptor.Use(next => context => Task.CompletedTask); + descriptor.BindArgumentsExplicitly(); + } + } + public class DirectiveMiddleware { private FieldDelegate _next; diff --git a/src/Core/Types.Tests/Types/EnumTypeTests.cs b/src/Core/Types.Tests/Types/EnumTypeTests.cs index 3fe6d8c80d4..f14758d9c2b 100644 --- a/src/Core/Types.Tests/Types/EnumTypeTests.cs +++ b/src/Core/Types.Tests/Types/EnumTypeTests.cs @@ -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(d => + { + d.BindItemsImplicitly(); + d.Item(Foo.Bar1); + })); + c.Options.StrictValidation = false; + }); + + // assert + EnumType type = schema.GetType("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() { diff --git a/src/Core/Types/Types/Descriptors/Contracts/IEnumTypeDescriptor~1.cs b/src/Core/Types/Types/Descriptors/Contracts/IEnumTypeDescriptor~1.cs index e1e3367ff30..43fa837d89f 100644 --- a/src/Core/Types/Types/Descriptors/Contracts/IEnumTypeDescriptor~1.cs +++ b/src/Core/Types/Types/Descriptors/Contracts/IEnumTypeDescriptor~1.cs @@ -39,6 +39,17 @@ IEnumTypeDescriptor SyntaxNode( IEnumTypeDescriptor BindItems(BindingBehavior behavior); + /// + /// Defines that all enum values have to be specified explicitly. + /// + IEnumTypeDescriptor BindItemsExplicitly(); + + /// + /// Defines that all enum values shall be infered + /// from the associated .Net type, + /// + IEnumTypeDescriptor BindItemsImplicitly(); + IEnumTypeDescriptor Directive( TDirective directiveInstance) where TDirective : class; diff --git a/src/Core/Types/Types/Descriptors/EnumTypeDescriptor~1.cs b/src/Core/Types/Types/Descriptors/EnumTypeDescriptor~1.cs index 4bca3618b77..9cbe9237ce4 100644 --- a/src/Core/Types/Types/Descriptors/EnumTypeDescriptor~1.cs +++ b/src/Core/Types/Types/Descriptors/EnumTypeDescriptor~1.cs @@ -36,6 +36,12 @@ protected internal EnumTypeDescriptor(IDescriptorContext context) return this; } + public new IEnumTypeDescriptor BindItemsExplicitly() => + BindItems(BindingBehavior.Explicit); + + public new IEnumTypeDescriptor BindItemsImplicitly() => + BindItems(BindingBehavior.Implicit); + public IEnumValueDescriptor Item(T value) { return base.Item(value); From 688cee88e6707389e713e487ca5049528d95892a Mon Sep 17 00:00:00 2001 From: Michael Staib Date: Tue, 6 Aug 2019 23:16:05 +0200 Subject: [PATCH 5/7] fixed tests --- src/Core/Types.Tests/Types/DirectiveTypeTests.cs | 2 +- src/Core/Types.Tests/Types/EnumTypeTests.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Core/Types.Tests/Types/DirectiveTypeTests.cs b/src/Core/Types.Tests/Types/DirectiveTypeTests.cs index c0788cf2cec..4f27eee4299 100644 --- a/src/Core/Types.Tests/Types/DirectiveTypeTests.cs +++ b/src/Core/Types.Tests/Types/DirectiveTypeTests.cs @@ -525,7 +525,7 @@ protected override void Configure( descriptor.Location(DirectiveLocation.Enum); descriptor.Location(DirectiveLocation.Field); descriptor.Use(next => context => Task.CompletedTask); - descriptor.BindArgumentsExplicitly(); + descriptor.BindArgumentsImplicitly().BindArgumentsExplicitly(); } } diff --git a/src/Core/Types.Tests/Types/EnumTypeTests.cs b/src/Core/Types.Tests/Types/EnumTypeTests.cs index f14758d9c2b..a8d8677e8b7 100644 --- a/src/Core/Types.Tests/Types/EnumTypeTests.cs +++ b/src/Core/Types.Tests/Types/EnumTypeTests.cs @@ -178,7 +178,7 @@ public void ExplicitEnumType_OnlyContainDeclaredValues_2() { c.RegisterType(new EnumType(d => { - d.BindItemsImplicitly(); + d.BindItemsImplicitly().BindItemsExplicitly(); d.Item(Foo.Bar1); })); c.Options.StrictValidation = false; From fd3798490dacec3c813930142e4702efdea1f928 Mon Sep 17 00:00:00 2001 From: Michael Staib Date: Tue, 6 Aug 2019 23:40:28 +0200 Subject: [PATCH 6/7] Added more tests --- .../Configuration/ReadOnlySchemaOptionsTests.cs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Core/Types.Tests/Configuration/ReadOnlySchemaOptionsTests.cs b/src/Core/Types.Tests/Configuration/ReadOnlySchemaOptionsTests.cs index a8742d69025..4b125674bd2 100644 --- a/src/Core/Types.Tests/Configuration/ReadOnlySchemaOptionsTests.cs +++ b/src/Core/Types.Tests/Configuration/ReadOnlySchemaOptionsTests.cs @@ -2,6 +2,7 @@ using Xunit; using Snapshooter.Xunit; using HotChocolate.Configuration; +using System; namespace HotChocolate { @@ -26,7 +27,7 @@ public void Copy_Options() var copied = new ReadOnlySchemaOptions(options); // assert - options.MatchSnapshot(); + copied.MatchSnapshot(); } [Fact] @@ -39,7 +40,18 @@ public void Copy_Options_Defaults() var copied = new ReadOnlySchemaOptions(options); // assert - options.MatchSnapshot(); + copied.MatchSnapshot(); + } + + [Fact] + public void Create_Options_Null() + { + // arrange + // act + Action action = () => new ReadOnlySchemaOptions(null); + + // assert + Assert.Throws(action); } } } From 0f11cdbae64bdae669a07be6d0eb8e33ed467b93 Mon Sep 17 00:00:00 2001 From: Michael Staib Date: Tue, 6 Aug 2019 23:48:08 +0200 Subject: [PATCH 7/7] fixed test snapshot --- .../ReadOnlySchemaOptionsTests.Copy_Options_Defaults.snap | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Core/Types.Tests/Configuration/__snapshots__/ReadOnlySchemaOptionsTests.Copy_Options_Defaults.snap b/src/Core/Types.Tests/Configuration/__snapshots__/ReadOnlySchemaOptionsTests.Copy_Options_Defaults.snap index fcbbc524d36..c66403abeed 100644 --- a/src/Core/Types.Tests/Configuration/__snapshots__/ReadOnlySchemaOptionsTests.Copy_Options_Defaults.snap +++ b/src/Core/Types.Tests/Configuration/__snapshots__/ReadOnlySchemaOptionsTests.Copy_Options_Defaults.snap @@ -1,7 +1,7 @@ { - "QueryTypeName": null, - "MutationTypeName": null, - "SubscriptionTypeName": null, + "QueryTypeName": "Query", + "MutationTypeName": "Mutation", + "SubscriptionTypeName": "Subscription", "StrictValidation": true, "UseXmlDocumentation": true, "DefaultBindingBehavior": "Implicit",