From 875e256459f7b1ea7de84bf5ff01f62b52c299ea Mon Sep 17 00:00:00 2001 From: Klaas Vandeweerdt Date: Sun, 25 Jun 2023 12:10:02 +0200 Subject: [PATCH] Added additional filter methods for FilterOperator.Present and FilterOperator.Empty --- README.md | 20 ++++++++++++++- .../Helpers/ExecutionQueryBuilder.cs | 12 ++++++++- Scr/Sdk4me.GraphQL/Queries/Base/Query.cs | 25 +++++++++++++++++++ Scr/Sdk4me.GraphQL/Sdk4me.GraphQL.csproj | 6 ++--- 4 files changed, 58 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 0846e13..b50fbf9 100644 --- a/README.md +++ b/README.md @@ -146,10 +146,26 @@ client.MaximumQueryDepthLevelConnections = 5; ``` #### Filtering +There are three methods to filtering: `Filter`, `CustomFilter` and, `FreeFormatFilter`. +Filters can only be applied at the top level of the query. + ```csharp IQuery query = Query.Person.Filter(PersonField.Name, FilterOperator.Equals, "Howard"); ``` -Filters can only be used on the top level query. +The `Filter` method allows you to specify field filters at the top level of the query. +You can define the field, the operator (e.g., equals, not equals), and the value to match against. + +```csharp +PersonQuery query = new PersonQuery() + .CustomFilter("Age", FilterOperator.NotEquals, new string[] { null }); +``` +The `CustomFilter` method enables the use of existing custom filters. + +```csharp +PersonQuery query = new PersonQuery() + .FreeFormatFilter("Howard"); +``` +The `FreeFormatFilter` method enables the usage of same filter functionality available in the top screen filter in the user interface (UI). #### Sorting ```csharp @@ -171,6 +187,8 @@ PersonQuery query = new PersonQuery() .Select(PermissionField.Account, PermissionField.Roles)) .SelectTeams(new TeamQuery() .ItemsPerRequest(10) + .SelectConfigurationManager(new PersonQuery() + .Select(PersonField.Name, PersonField.EmployeeID)) .SelectMembers(new PersonQuery() .ItemsPerRequest(50) .Select(PersonField.Name))) diff --git a/Scr/Sdk4me.GraphQL/Helpers/ExecutionQueryBuilder.cs b/Scr/Sdk4me.GraphQL/Helpers/ExecutionQueryBuilder.cs index 47813d9..8eae6f8 100644 --- a/Scr/Sdk4me.GraphQL/Helpers/ExecutionQueryBuilder.cs +++ b/Scr/Sdk4me.GraphQL/Helpers/ExecutionQueryBuilder.cs @@ -377,7 +377,7 @@ internal static string BuildCustomFilter(string name, FilterOperator filterOpera builder.Append(" values:"); builder.Append(serializedValues.Length switch { - 0 => "[null]", + 0 => "[]", 1 => $"[{serializedValues[0]}]", _ => $"[{string.Join(',', serializedValues)}]" }); @@ -398,6 +398,14 @@ internal static string BuildCustomFilter(string name, FilterOperator filterOpera } } + internal static string BuildStringFilter(string field, FilterOperator filterOperator) + { + if (filterOperator == FilterOperator.Present || filterOperator == FilterOperator.Empty) + return BuildStringFilter(field, filterOperator, Array.Empty()); + else + throw new Sdk4meFilterException("Invalid filter operator"); + } + internal static string BuildStringFilter(string field, FilterOperator filterOperator, params string?[] values) { if (filterOperator == FilterOperator.In || filterOperator == FilterOperator.NotIn || filterOperator == FilterOperator.Equals || filterOperator == FilterOperator.NotEquals) @@ -443,6 +451,8 @@ private static string SerializeObject(string? value) private static string[] SerializeObject(params string?[] values) { List retval = new(); + if (values == null) + values = Array.Empty(); foreach (string? value in values) retval.Add(SerializeObject(value)); return retval.ToArray(); diff --git a/Scr/Sdk4me.GraphQL/Queries/Base/Query.cs b/Scr/Sdk4me.GraphQL/Queries/Base/Query.cs index 5670dc6..6417b8a 100644 --- a/Scr/Sdk4me.GraphQL/Queries/Base/Query.cs +++ b/Scr/Sdk4me.GraphQL/Queries/Base/Query.cs @@ -291,6 +291,31 @@ public TEntity Filter(TFields field, FilterOperator filterOperator, params strin return Filter(GetEnumStringValue(field), filterOperator, values); } + /// + /// Add a filter to the query. + /// + /// The field name. + /// The filter operator, which should be Present or Blankt. + /// The current . + /// + public TEntity Filter(string field, FilterOperator filterOperator) + { + filters.Add(ExecutionQueryBuilder.BuildStringFilter(field, filterOperator)); + return this as TEntity ?? throw new NullReferenceException(nameof(TEntity)); + } + + /// + /// Add a filter to the query. + /// + /// The field name. + /// The filter operator, which should be Present or Blankt. + /// The current . + /// + public TEntity Filter(TFields field, FilterOperator filterOperator) + { + return Filter(GetEnumStringValue(field), filterOperator); + } + /// /// Add a filter to the query. /// diff --git a/Scr/Sdk4me.GraphQL/Sdk4me.GraphQL.csproj b/Scr/Sdk4me.GraphQL/Sdk4me.GraphQL.csproj index 6bbf89b..3533ef0 100644 --- a/Scr/Sdk4me.GraphQL/Sdk4me.GraphQL.csproj +++ b/Scr/Sdk4me.GraphQL/Sdk4me.GraphQL.csproj @@ -16,13 +16,13 @@ https://github.com/code4me/4me-sdk-graphql-dotnet git 4me - 1.0.5 - 1.0.5 + 1.0.6 + 1.0.6 MIT LogoDark128x218.png README.md False - 1.0.5 + 1.0.6