Skip to content

Commit

Permalink
Adding failing test for fusion query planner
Browse files Browse the repository at this point in the history
  • Loading branch information
ndejaco2 committed Sep 1, 2023
1 parent de63bd0 commit 8635bfc
Show file tree
Hide file tree
Showing 10 changed files with 265 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/HotChocolate/Fusion/test/Core.Tests/RequestPlannerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,41 @@ query TopProducts {
await snapshot.MatchAsync();
}

[Fact(Skip = "This test currently fails during query planning")]
public async Task Query_Plan_27_Selection_Set_Empty()
{
// arrange
using var demoProject = await DemoProject.CreateAsync();

var fusionGraph = await FusionGraphComposer.ComposeAsync(
new[]
{
demoProject.Authors.ToConfiguration(),
demoProject.Books.ToConfiguration()
});

// act
var result = await CreateQueryPlanAsync(
fusionGraph,
"""
query Query {
authorById(id: "1") {
id,
name,
bio,
books {
id
author {
books {
id
}
}
}
}
}
""");
}

private static async Task<(DocumentNode UserRequest, Execution.Nodes.QueryPlan QueryPlan)> CreateQueryPlanAsync(
Skimmed.Schema fusionGraph,
[StringSyntax("graphql")] string query)
Expand Down
16 changes: 16 additions & 0 deletions src/HotChocolate/Fusion/test/Shared/Authors/Author.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace HotChocolate.Fusion.Shared.Authors;

public class Author
{
public string Id { get; set;}

public string Name { get; set; }

public string Bio { get; set;}

public Author(string id, string name, string bio) {
this.Id = id;
this.Name = name;
this.Bio = bio;
}
}
20 changes: 20 additions & 0 deletions src/HotChocolate/Fusion/test/Shared/Authors/AuthorQuery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace HotChocolate.Fusion.Shared.Authors;

[GraphQLName("Query")]
public sealed class AuthorQuery
{
public Author? AuthorById(
string id,
[Service] AuthorRepository repository)
=> repository.GetAuthorById(id);

public IEnumerable<Author> Authors(int limit, [Service] AuthorRepository repository)
=> repository.GetAuthors(limit);

public Book BookByAuthorId(
string authorId,
[Service] AuthorRepository repository) {
Author author = repository.GetAuthorById(authorId);
return new Book(authorId, author);
}
}
22 changes: 22 additions & 0 deletions src/HotChocolate/Fusion/test/Shared/Authors/AuthorRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace HotChocolate.Fusion.Shared.Authors;

public sealed class AuthorRepository
{
private readonly Dictionary<string, Author> _authors;

public AuthorRepository()
{
_authors = new[]
{
new Author("1", "First author", "The first author")
}.ToDictionary(t => t.Id);
}

public IEnumerable<Author> GetAuthors(int limit)
=> _authors.Values.OrderBy(t => t.Id).Take(limit);

public Author? GetAuthorById(string id)
=> _authors.TryGetValue(id, out var author)
? author
: null;
}
15 changes: 15 additions & 0 deletions src/HotChocolate/Fusion/test/Shared/Authors/Book.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

namespace HotChocolate.Fusion.Shared.Authors;

public class Book
{

public string AuthorId { get; set;}

public Author Author {get; set; }

public Book(string authorId, Author author) {
this.AuthorId = authorId;
this.Author = author;
}
}
14 changes: 14 additions & 0 deletions src/HotChocolate/Fusion/test/Shared/Books/Author.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace HotChocolate.Fusion.Shared.Books;


public class Author
{
public string Id { get; set;}

public IEnumerable<Book> Books { get; set; }

public Author(string id, IEnumerable<Book> books) {
this.Id = id;
this.Books = books;
}
}
17 changes: 17 additions & 0 deletions src/HotChocolate/Fusion/test/Shared/Books/Book.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace HotChocolate.Fusion.Shared.Books;

public class Book
{
public string Id { get; set;}

public string AuthorId { get; set;}

public string Title {get; set; }


public Book(string id, string authorId, string title) {
this.Id = id;
this.AuthorId = authorId;
this.Title = title;
}
}
18 changes: 18 additions & 0 deletions src/HotChocolate/Fusion/test/Shared/Books/BookQuery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace HotChocolate.Fusion.Shared.Books;

[GraphQLName("Query")]
public sealed class BookQuery
{
public Book? BookById(
string id,
[Service] BookRepository repository)
=> repository.GetBookById(id);

public IEnumerable<Book> Books(int limit, [Service] BookRepository repository)
=> repository.GetBooks(limit);

public Author authorById(
string id,
[Service] BookRepository repository)
=> new Author(id, repository.GetBooksByAuthorId(id));
}
25 changes: 25 additions & 0 deletions src/HotChocolate/Fusion/test/Shared/Books/BookRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace HotChocolate.Fusion.Shared.Books;

public sealed class BookRepository
{
private readonly Dictionary<string, Book> _books;

public BookRepository()
{
_books = new[]
{
new Book("1", "1", "The first book")
}.ToDictionary(t => t.Id);
}

public IEnumerable<Book> GetBooks(int limit)
=> _books.Values.OrderBy(t => t.Id).Take(limit);

public Book? GetBookById(string id)
=> _books.TryGetValue(id, out var book)
? book
: null;

public IEnumerable<Book> GetBooksByAuthorId(string authorId)
=> _books.Values.Where(b => b.AuthorId.Equals(authorId));
}
83 changes: 83 additions & 0 deletions src/HotChocolate/Fusion/test/Shared/DemoProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using HotChocolate.Fusion.Shared.Products;
using HotChocolate.Fusion.Shared.Reviews;
using HotChocolate.Fusion.Shared.Shipping;
using HotChocolate.Fusion.Shared.Books;
using HotChocolate.Fusion.Shared.Authors;
using HotChocolate.Transport.Http;
using HotChocolate.Types.Descriptors;
using HotChocolate.Utilities.Introspection;
Expand All @@ -29,6 +31,8 @@ private DemoProject(
DemoSubgraph shipping,
DemoSubgraph appointment,
DemoSubgraph patient1,
DemoSubgraph books,
DemoSubgraph authors,
IHttpClientFactory clientFactory,
IWebSocketConnectionFactory webSocketConnectionFactory)
{
Expand All @@ -40,6 +44,8 @@ private DemoProject(
Shipping = shipping;
Appointment = appointment;
Patient1 = patient1;
Books = books;
Authors = authors;
HttpClientFactory = clientFactory;
WebSocketConnectionFactory = webSocketConnectionFactory;
}
Expand All @@ -62,6 +68,10 @@ private DemoProject(

public DemoSubgraph Patient1 { get; }

public DemoSubgraph Books { get; }

public DemoSubgraph Authors { get; }

public static async Task<DemoProject> CreateAsync(CancellationToken ct = default)
{
var disposables = new List<IDisposable>();
Expand Down Expand Up @@ -215,6 +225,41 @@ public static async Task<DemoProject> CreateAsync(CancellationToken ct = default
.DownloadSchemaAsync(patient1Client, ct)
.ConfigureAwait(false);

var books = testServerFactory.Create(
s => s
.AddRouting()
.AddGraphQLServer()
.AddQueryType<BookQuery>()
.AddConvention<INamingConventions>(_ => new DefaultNamingConventions()),
c => c
.UseRouting()
.UseEndpoints(endpoints => endpoints.MapGraphQL()));
disposables.Add(books);

var booksClient = books.CreateClient();
booksClient.BaseAddress = new Uri("http://localhost:5000/graphql");
var booksSchema = await introspection
.DownloadSchemaAsync(booksClient, ct)
.ConfigureAwait(false);

var authors = testServerFactory.Create(
s => s
.AddRouting()
.AddGraphQLServer()
.AddQueryType<AuthorQuery>()
.AddConvention<INamingConventions>(_ => new DefaultNamingConventions()),
c => c
.UseRouting()
.UseEndpoints(endpoints => endpoints.MapGraphQL()));
disposables.Add(authors);

var authorsClient = authors.CreateClient();
authorsClient.BaseAddress = new Uri("http://localhost:5000/graphql");
var authorsSchema = await introspection
.DownloadSchemaAsync(authorsClient, ct)
.ConfigureAwait(false);


var httpClients = new Dictionary<string, Func<HttpClient>>
{
{
Expand Down Expand Up @@ -287,6 +332,26 @@ public static async Task<DemoProject> CreateAsync(CancellationToken ct = default
return httpClient;
}
},
{
"Books", () =>
{
// ReSharper disable once AccessToDisposedClosure
var httpClient = books.CreateClient();
httpClient.BaseAddress = new Uri("http://localhost:5000/graphql");
httpClient.DefaultRequestHeaders.AddGraphQLPreflight();
return httpClient;
}
},
{
"Authors", () =>
{
// ReSharper disable once AccessToDisposedClosure
var httpClient = books.CreateClient();
httpClient.BaseAddress = new Uri("http://localhost:5000/graphql");
httpClient.DefaultRequestHeaders.AddGraphQLPreflight();
return httpClient;
}
},
};

var webSocketClients = new Dictionary<string, Func<IWebSocketConnection>>
Expand All @@ -312,6 +377,12 @@ public static async Task<DemoProject> CreateAsync(CancellationToken ct = default
{
"Patient1", () => new MockWebSocketConnection(patient1.CreateWebSocketClient())
},
{
"Books", () => new MockWebSocketConnection(books.CreateWebSocketClient())
},
{
"Authors", () => new MockWebSocketConnection(authors.CreateWebSocketClient())
},
};

return new DemoProject(
Expand Down Expand Up @@ -358,6 +429,18 @@ public static async Task<DemoProject> CreateAsync(CancellationToken ct = default
new Uri("ws://localhost:5000/graphql"),
patient1Schema,
patient1),
new DemoSubgraph(
"Books",
booksClient.BaseAddress,
new Uri("ws://localhost:5000/graphql"),
booksSchema,
books),
new DemoSubgraph(
"Authors",
authorsClient.BaseAddress,
new Uri("ws://localhost:5000/graphql"),
authorsSchema,
authors),
new MockHttpClientFactory(httpClients),
new MockWebSocketConnectionFactory(webSocketClients));
}
Expand Down

0 comments on commit 8635bfc

Please sign in to comment.