Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix nested sorting a new approach QueryContext #8061

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions src/HotChocolate/Data/src/Data/Sorting/Context/SortingContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,18 +184,25 @@ protected override ISyntaxVisitorAction Leave(
Context context)
{
var type = context.Types.Peek();
var field = (SortField)type.Fields[node.Name.Value];
var fieldType = field.Type.NamedType();
var expression = context.Parents.Pop();

if (fieldType.IsInputObjectType())
if (type.Fields.TryGetField(node.Name.Value, out var inputField) && inputField is SortField sortField)
{
context.Types.Pop();
var fieldType = sortField.Type.NamedType();
var expression = context.Parents.Pop();

if (fieldType.IsInputObjectType())
{
context.Types.Pop();
}
else
{
var ascending = node.Value.Value?.Equals("ASC") ?? true;
context.Completed.Add((expression, ascending, sortField.Member!.GetReturnType()));
}
}
else
{
var ascending = node.Value.Value?.Equals("ASC") ?? true;
context.Completed.Add((expression, ascending, field.Member!.GetReturnType()));
context.Types.Pop();
}

return base.Leave(node, context);
Expand Down
55 changes: 55 additions & 0 deletions src/HotChocolate/Data/test/Data.Sorting.Tests/IntegrationTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using GreenDonut.Data;
using HotChocolate.Execution;
using HotChocolate.Types;
using HotChocolate.Types.Pagination;
using Microsoft.Extensions.DependencyInjection;

namespace HotChocolate.Data.Tests;
Expand Down Expand Up @@ -30,6 +32,34 @@ public async Task Sorting_Should_Work_When_UsedWithNonNullDateTime()
// assert
result.MatchSnapshot();
}

[Fact]
public async Task Sorting_Should_Work_When_Nested()
{
// arrange
var executor = await new ServiceCollection()
.AddGraphQL()
.AddQueryType<Query>()
.AddSorting()
.BuildRequestExecutorAsync();

const string query = @"
{
books(order: [{ author: { name: ASC } }]) {
title
author {
name
}
}
}
";

// act
var result = await executor.ExecuteAsync(query);

// assert
result.MatchSnapshot();
}
}

public class Query
Expand All @@ -41,10 +71,35 @@ public IEnumerable<Foo> Foos() => new[]
new Foo { CreatedUtc = new DateTime(2010, 1, 1, 1, 1, 1), },
new Foo { CreatedUtc = new DateTime(2020, 1, 1, 1, 1, 1), },
};

[UseSorting]
public IEnumerable<Book> GetBooks(QueryContext<Book> queryContext)
=> new[]
{
new Book { Title = "Book5", Author = new Author { Name = "Author6" } },
new Book { Title = "Book7", Author = new Author { Name = "Author17" } },
new Book { Title = "Book1", Author = new Author { Name = "Author5" } },
}
.AsQueryable()
.With(queryContext);
}

public class Foo
{
[GraphQLType(typeof(NonNullType<DateType>))]
public DateTime CreatedUtc { get; set; }
}

public class Author
{
public string Name { get; set; } = string.Empty;

[UseSorting]
public Book[] Books { get; set; } = Array.Empty<Book>();
}

public class Book
{
public string Title { get; set; } = string.Empty;
public Author? Author { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"data": {
"books": [
{
"title": "Book7",
"author": {
"name": "Author17"
}
},
{
"title": "Book1",
"author": {
"name": "Author5"
}
},
{
"title": "Book5",
"author": {
"name": "Author6"
}
}
]
}
}