From 2fc932ba6ca7462183a573f61fad2a831ef79613 Mon Sep 17 00:00:00 2001 From: PascalSenn Date: Sun, 5 Feb 2023 19:20:52 +0100 Subject: [PATCH] Extend documentation of scalar filter types (#5759) Co-authored-by: Glen --- .../v13/fetching-data/filtering.md | 89 +++++++++++++++---- 1 file changed, 70 insertions(+), 19 deletions(-) diff --git a/website/src/docs/hotchocolate/v13/fetching-data/filtering.md b/website/src/docs/hotchocolate/v13/fetching-data/filtering.md index caacb2cb508..48eff6c4988 100644 --- a/website/src/docs/hotchocolate/v13/fetching-data/filtering.md +++ b/website/src/docs/hotchocolate/v13/fetching-data/filtering.md @@ -647,7 +647,7 @@ type Query { ## Binding of FilterTypes -`FilterInputType`'s **cannot** just be registered on the schema. You have to bind them to the runtime type on the convention. +`FilterInputType`'s can be registered like any other type on the schema. **Configuration** @@ -660,22 +660,27 @@ public class UserFilterInput : FilterInputType descriptor.Field(x => x.Name).Description("This is the name"); } } +``` + +```csharp +services + .AddGraphQLServer() + .AddFiltering() + .AddType() + ...; +``` + +In case you use custom conventions, you can also bind the filter types on your convention. -public class CustomStringOperationFilterInput : StringOperationFilterInput +```csharp +public class CustomFilterConvention : FilterConvention { - protected override void Configure(IFilterInputTypeDescriptor descriptor) + protected override void Configure(IFilterConventionDescriptor descriptor) { - descriptor - .Operation(DefaultFilterOperations.Equals) - .Type(); - descriptor - .Operation(DefaultFilterOperations.NotEquals) - .Type(); + descriptor.BindRuntimeType() } } -descriptor.BindRuntimeType(); -descriptor.BindRuntimeType(); ``` **Result** @@ -689,21 +694,67 @@ type User { name: String! } -input CustomStringOperationFilterInput { - and: [CustomStringOperationFilterInput!] - or: [CustomStringOperationFilterInput!] - eq: String - neq: String -} - input UserFilterInput { and: [UserFilterInput!] or: [UserFilterInput!] "This is the name" - name: CustomStringOperationFilterInput + name: StringOperationFilterInput +} + +# ... StringOperationFilterInput left out for brevity +``` + +**Scalars / Operation Input Types** + +> This is also required when you use `HotChocolate.Types.Scalars`! + +When you add custom scalars, you will have to create custom filter types. +Scalars are mapped to a `FilterInputType` that defines the operations that are possible for this scalar. +The built-in scalars are already mapped to matching filter types. +For custom scalars, or scalars from `HotChocolate.Types.Scalars`, you have to create and bind types. + +```csharp +public class EmailAddressOperationFilterInputType : FilterInputType +{ + protected override void Configure(IFilterInputTypeDescriptor descriptor) + { + descriptor.Operation(DefaultFilterOperations.Equals).Type(); + descriptor.Operation(DefaultFilterOperations.NotEquals).Type(); + descriptor.Operation(DefaultFilterOperations.Contains).Type(); + descriptor.Operation(DefaultFilterOperations.NotContains).Type(); + descriptor.Operation(DefaultFilterOperations.In).Type>(); + descriptor.Operation(DefaultFilterOperations.NotIn).Type>(); + descriptor.Operation(DefaultFilterOperations.StartsWith).Type(); + descriptor.Operation(DefaultFilterOperations.NotStartsWith).Type(); + descriptor.Operation(DefaultFilterOperations.EndsWith).Type(); + descriptor.Operation(DefaultFilterOperations.NotEndsWith).Type(); + } +} +``` + +For comparable value types, you can use the `ComparableOperationFilterInputType` base class. + +```csharp +public class UnsignedIntOperationFilterInputType + : ComparableOperationFilterInputType +{ + protected override void Configure(IFilterInputTypeDescriptor descriptor) + { + descriptor.Name("UnsignedIntOperationFilterInputType"); + base.Configure(descriptor); + } } ``` +These types have to be registered on the filter convention: + +```csharp +services + .AddGraphQLServer() + .AddFiltering(x => x.AddDefaults().BindRuntimeType()) + ...; +``` + ## Extend FilterTypes Instead of defining your own operation type, you can also just change the configuration of the built