Skip to content

Commit

Permalink
Added IsDefined check to IFilterContext and ISortingContext. (#6999)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib authored Mar 20, 2024
1 parent b64ed0a commit e469640
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using HotChocolate.Language;
using HotChocolate.Resolvers;
using HotChocolate.Types;
Expand All @@ -9,7 +6,7 @@
namespace HotChocolate.Data.Filters;

/// <summary>
/// Encapuslates all filter specific information
/// Encapsulates all filter specific information
/// </summary>
public class FilterContext : IFilterContext
{
Expand Down Expand Up @@ -42,6 +39,9 @@ public void Handled(bool isHandled)
}
}

/// <inheritdoc />
public bool IsDefined => _value.ValueNode.Kind is not SyntaxKind.NullValue;

/// <inheritdoc />
public IReadOnlyList<IFilterFieldInfo> GetFields() => _value.GetFields();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Collections.Generic;

namespace HotChocolate.Data.Filters;

/// <summary>
Expand All @@ -14,6 +12,11 @@ public interface IFilterContext : IFilterInfo
/// </summary>
/// <param name="isHandled">If false, sorting is applied on the result of the resolver</param>
void Handled(bool isHandled);

/// <summary>
/// Specifies if a filter was defined.
/// </summary>
bool IsDefined { get; }

/// <summary>
/// Serializes the input object to a dictionary
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Collections.Generic;

namespace HotChocolate.Data.Sorting;

/// <summary>
Expand All @@ -15,6 +13,11 @@ public interface ISortingContext
/// <param name="isHandled">If false, sorting is applied on the result of the resolver</param>
void Handled(bool isHandled);

/// <summary>
/// Specifies if sorting was defined.
/// </summary>
bool IsDefined { get; }

/// <summary>
/// Serializes the input object to a dictionary
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using HotChocolate.Language;
using HotChocolate.Resolvers;
using HotChocolate.Types;
Expand Down Expand Up @@ -46,6 +43,9 @@ public void Handled(bool isHandled)
}
}

/// <inheritdoc />
public bool IsDefined => _value is not [{ ValueNode.Kind: SyntaxKind.NullValue, },];

/// <inheritdoc />
public IReadOnlyList<IReadOnlyList<ISortingFieldInfo>> GetFields()
=> _value.Select(x => x.GetFields()).ToArray();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading.Tasks;
using CookieCrumble;
using HotChocolate.Data.Filters.Expressions;
using HotChocolate.Execution;
Expand Down Expand Up @@ -54,6 +50,80 @@ public async Task GetFields_Should_ReturnScalarField()
Assert.Equal("eq", operation.Field.Name);
Assert.Equal("test", Assert.IsType<FilterValue>(operation.Value).Value);
}

[Fact]
public async Task When_Query_Is_Empty_IsDefined_Should_Be_False()
{
// arrange
IFilterContext? context = null;
var executor = await new ServiceCollection()
.AddGraphQL()
.AddQueryType(t => t
.Name("Query")
.Field("test")
.Type<ListType<ObjectType<Book>>>()
.UseFiltering()
.Resolve(ctx =>
{
context = ctx.GetFilterContext();
return Array.Empty<Book>();
}))
.AddFiltering()
.BuildRequestExecutorAsync();

// act
const string query =
"""
{
test {
title
}
}
""";

await executor.ExecuteAsync(query);

// assert
Assert.NotNull(context);
Assert.False(context!.IsDefined);
}

[Fact]
public async Task When_Query_Is_Set_IsDefined_Should_Be_False()
{
// arrange
IFilterContext? context = null;
var executor = await new ServiceCollection()
.AddGraphQL()
.AddQueryType(t => t
.Name("Query")
.Field("test")
.Type<ListType<ObjectType<Book>>>()
.UseFiltering()
.Resolve(ctx =>
{
context = ctx.GetFilterContext();
return Array.Empty<Book>();
}))
.AddFiltering()
.BuildRequestExecutorAsync();

// act
const string query =
"""
{
test(where: { title: { eq: "test" } }) {
title
}
}
""";

await executor.ExecuteAsync(query);

// assert
Assert.NotNull(context);
Assert.True(context!.IsDefined);
}

[Fact]
public async Task GetFields_Should_ReturnScalarFieldWithListOperation()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Immutable;
using System.Threading.Tasks;
using CookieCrumble;
using HotChocolate.Data.Sorting.Expressions;
using HotChocolate.Execution;
Expand Down Expand Up @@ -49,6 +47,80 @@ public async Task GetFields_Should_ReturnScalarField()
Assert.Equal("title", field.Field.Name);
Assert.Equal("DESC", operation);
}

[Fact]
public async Task When_Sorting_Is_Empty_IsDefined_Should_Be_False()
{
// arrange
ISortingContext? context = null;
var executor = await new ServiceCollection()
.AddGraphQL()
.AddQueryType(x => x
.Name("Query")
.Field("test")
.Type<ListType<ObjectType<Book>>>()
.UseSorting()
.Resolve(x =>
{
context = x.GetSortingContext();
return Array.Empty<Book>();
}))
.AddSorting()
.BuildRequestExecutorAsync();

// act
const string query =
"""
{
test {
title
}
}
""";

await executor.ExecuteAsync(query);

// assert
Assert.NotNull(context);
Assert.False(context!.IsDefined);
}

[Fact]
public async Task When_Sorting_Is_Set_IsDefined_Should_Be_True()
{
// arrange
ISortingContext? context = null;
var executor = await new ServiceCollection()
.AddGraphQL()
.AddQueryType(x => x
.Name("Query")
.Field("test")
.Type<ListType<ObjectType<Book>>>()
.UseSorting()
.Resolve(x =>
{
context = x.GetSortingContext();
return Array.Empty<Book>();
}))
.AddSorting()
.BuildRequestExecutorAsync();

// act
const string query =
"""
{
test(order: { title: DESC }) {
title
}
}
""";

await executor.ExecuteAsync(query);

// assert
Assert.NotNull(context);
Assert.True(context!.IsDefined);
}

[Fact]
public async Task GetFields_Should_ReturnMultipleScalarField()
Expand Down

0 comments on commit e469640

Please sign in to comment.