From 56f0190c90ee57953f72e39a71606d0797684997 Mon Sep 17 00:00:00 2001 From: Ivan Maximov Date: Wed, 24 Aug 2022 11:42:51 +0300 Subject: [PATCH 1/2] Add test --- .../SchemaInitializationTests.cs | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/src/GraphQL.Tests/Initialization/SchemaInitializationTests.cs b/src/GraphQL.Tests/Initialization/SchemaInitializationTests.cs index 5d8b706ecd..35f201b959 100644 --- a/src/GraphQL.Tests/Initialization/SchemaInitializationTests.cs +++ b/src/GraphQL.Tests/Initialization/SchemaInitializationTests.cs @@ -1,4 +1,5 @@ using GraphQL.Types; +using GraphQL.Utilities; using GraphQLParser.AST; namespace GraphQL.Tests.Initialization; @@ -79,6 +80,12 @@ public void SchemaWithEnumWithoutValues_Should_Throw() ShouldThrow("An Enum type 'EnumWithoutValues' must define one or more unique enum values."); ShouldThrow("An Enum type 'Enumeration' must define one or more unique enum values."); } + + [Fact] + public void SchemaWithDirective_Should_Not_Throw() + { + ShouldNotThrow(); + } } public class EmptyQuerySchema : Schema @@ -311,3 +318,72 @@ public SchemaWithEnumWithoutValues2() RegisterType(type); } } + +// https://github.com/graphql-dotnet/graphql-dotnet/issues/3301 +public class SchemaWithDirective : Schema +{ + public class MaxLength : Directive + { + public MaxLength() + : base("maxLength", DirectiveLocation.Mutation, DirectiveLocation.InputFieldDefinition) + { + Description = "Used to specify the minimum and/or maximum length for an input field or argument."; + Arguments = new QueryArguments( + new QueryArgument + { + Name = "min", + Description = "If specified, specifies the minimum length that the input field or argument must have." + }, + new QueryArgument + { + Name = "max", + Description = "If specified, specifies the maximum length that the input field or argument must have." + } + ); + } + } + + public class MaxLengthDirectiveVistor : BaseSchemaNodeVisitor + { + public override void VisitObjectFieldDefinition(FieldType field, IObjectGraphType type, ISchema schema) + { + var applied = field.FindAppliedDirective("maxLength"); + applied.ShouldBeNull(); + } + + public override void VisitInputObjectFieldDefinition(FieldType field, IInputObjectGraphType type, ISchema schema) + { + if (field.Name == "count") + { + var applied = field.FindAppliedDirective("maxLength"); + applied.ShouldNotBeNull(); + applied.ArgumentsCount.ShouldBe(2); + } + } + } + + public class BookSummaryCreateArgInputType : InputObjectGraphType + { + public BookSummaryCreateArgInputType() + { + Name = "BookSummaryCreateArg"; + Field(_ => _.Count).Directive("maxLength", x => + x.AddArgument(new DirectiveArgument("min") { Name = "min", Value = 1 }) + .AddArgument(new DirectiveArgument("max") { Name = "max", Value = 10 })); + } + } + + public class BookSummaryCreateArg + { + public int Count { get; set; } + } + + public SchemaWithDirective() + { + var root = new ObjectGraphType(); + root.Field("field").Argument("arg"); + Query = root; + + this.RegisterVisitor(); + } +} From 2a5fdcd2955d660da82ae6ac6c6e2b10bc001ce0 Mon Sep 17 00:00:00 2001 From: Ivan Maximov Date: Wed, 24 Aug 2022 11:57:36 +0300 Subject: [PATCH 2/2] fix --- .../Initialization/SchemaInitializationTests.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/GraphQL.Tests/Initialization/SchemaInitializationTests.cs b/src/GraphQL.Tests/Initialization/SchemaInitializationTests.cs index 35f201b959..5c5f91be11 100644 --- a/src/GraphQL.Tests/Initialization/SchemaInitializationTests.cs +++ b/src/GraphQL.Tests/Initialization/SchemaInitializationTests.cs @@ -343,7 +343,7 @@ public MaxLength() } } - public class MaxLengthDirectiveVistor : BaseSchemaNodeVisitor + public class MaxLengthDirectiveVisitor : BaseSchemaNodeVisitor { public override void VisitObjectFieldDefinition(FieldType field, IObjectGraphType type, ISchema schema) { @@ -384,6 +384,7 @@ public SchemaWithDirective() root.Field("field").Argument("arg"); Query = root; - this.RegisterVisitor(); + Directives.Register(new MaxLength()); + this.RegisterVisitor(); } }