Skip to content

Commit

Permalink
Added additional filter methods for FilterOperator.Present and Filter…
Browse files Browse the repository at this point in the history
…Operator.Empty
  • Loading branch information
klaasvandeweerdt committed Jun 25, 2023
1 parent 1000ce6 commit 875e256
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)))
Expand Down
12 changes: 11 additions & 1 deletion Scr/Sdk4me.GraphQL/Helpers/ExecutionQueryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)}]"
});
Expand All @@ -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<string>());
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)
Expand Down Expand Up @@ -443,6 +451,8 @@ private static string SerializeObject(string? value)
private static string[] SerializeObject(params string?[] values)
{
List<string> retval = new();
if (values == null)
values = Array.Empty<string>();
foreach (string? value in values)
retval.Add(SerializeObject(value));
return retval.ToArray();
Expand Down
25 changes: 25 additions & 0 deletions Scr/Sdk4me.GraphQL/Queries/Base/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,31 @@ public TEntity Filter(TFields field, FilterOperator filterOperator, params strin
return Filter(GetEnumStringValue(field), filterOperator, values);
}

/// <summary>
/// Add a filter to the query.
/// </summary>
/// <param name="field">The field name.</param>
/// <param name="filterOperator">The filter operator, which should be <c>Present</c> or <c>Blankt</c>.</param>
/// <returns>The current <see cref="IQuery"/>.</returns>
/// <exception cref="NullReferenceException"></exception>
public TEntity Filter(string field, FilterOperator filterOperator)
{
filters.Add(ExecutionQueryBuilder.BuildStringFilter(field, filterOperator));
return this as TEntity ?? throw new NullReferenceException(nameof(TEntity));
}

/// <summary>
/// Add a filter to the query.
/// </summary>
/// <param name="field">The field name.</param>
/// <param name="filterOperator">The filter operator, which should be <c>Present</c> or <c>Blankt</c>.</param>
/// <returns>The current <see cref="IQuery"/>.</returns>
/// <exception cref="NullReferenceException"></exception>
public TEntity Filter(TFields field, FilterOperator filterOperator)
{
return Filter(GetEnumStringValue(field), filterOperator);
}

/// <summary>
/// Add a filter to the query.
/// </summary>
Expand Down
6 changes: 3 additions & 3 deletions Scr/Sdk4me.GraphQL/Sdk4me.GraphQL.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
<RepositoryUrl>https://github.com/code4me/4me-sdk-graphql-dotnet</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>4me</PackageTags>
<AssemblyVersion>1.0.5</AssemblyVersion>
<FileVersion>1.0.5</FileVersion>
<AssemblyVersion>1.0.6</AssemblyVersion>
<FileVersion>1.0.6</FileVersion>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageIcon>LogoDark128x218.png</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
<GeneratePackageOnBuild>False</GeneratePackageOnBuild>
<Version>1.0.5</Version>
<Version>1.0.6</Version>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 875e256

Please sign in to comment.