Skip to content

Commit

Permalink
Fixed UseFirstOrDefault missing type reference issue. (ChilliCream#2921)
Browse files Browse the repository at this point in the history
  • Loading branch information
PascalSenn authored and michaelstaib committed Nov 11, 2021
1 parent 7748bc3 commit e27df75
Show file tree
Hide file tree
Showing 16 changed files with 524 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ private static IObjectFieldDescriptor ApplyMiddleware(

Type selectionType = typeInfo.NamedType;
definition.ResultType = selectionType;
definition.Type = context.TypeInspector.GetTypeRef(selectionType);
definition.Type =
context.TypeInspector.GetTypeRef(selectionType, TypeContext.Output);

definition.Configurations.Add(
new CompleteConfiguration<ObjectFieldDefinition>(
Expand Down
5 changes: 3 additions & 2 deletions src/HotChocolate/Data/test/Data.Tests/Author.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace HotChocolate.Data
{
Expand All @@ -12,5 +10,8 @@ public class Author

public virtual ICollection<Book> Books { get; set; } =
new List<Book>();

public virtual ICollection<Publisher> Publishers { get; set; } =
new List<Publisher>();
}
}
2 changes: 2 additions & 0 deletions src/HotChocolate/Data/test/Data.Tests/Book.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ public class Book
public string? Title { get; set; }

public virtual Author? Author { get; set; }

public virtual Publisher? Publisher { get; set; }
}
}
109 changes: 109 additions & 0 deletions src/HotChocolate/Data/test/Data.Tests/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,74 @@ query GetBooks($title: String) {
result.ToJson().MatchSnapshot(new SnapshotNameExtension("Result"));
}

[Fact]
public async Task ExecuteAsync_Should_ArgumentAndFirstOrDefault_When_Executed()
{
// arrange
IRequestExecutor executor = await new ServiceCollection()
.AddGraphQL()
.AddFiltering()
.AddSorting()
.AddProjections()
.AddQueryType<FirstOrDefaulQuery>()
.BuildRequestExecutorAsync();

// act
IExecutionResult result = await executor.ExecuteAsync(
@"
{
books(book: {id: 1, authorId: 0}) {
title
}
}
");

// assert
result.ToJson().MatchSnapshot();
}

[Fact]
public async Task
Schema_Should_Generate_WhenMutationInputHasManyToManyRelationshipWithOutputType()
{
// arrange
IRequestExecutor executor = await new ServiceCollection()
.AddGraphQL()
.AddFiltering()
.AddSorting()
.AddProjections()
.AddQueryType<FirstOrDefaulQuery>()
.AddMutationType<FirstOrDefaultMutation_ManyToMany>()
.BuildRequestExecutorAsync();

// act
var result = executor.Schema.Print();

// assert
result.MatchSnapshot();
}

[Fact]
public async Task
Schema_Should_Generate_WhenMutationInputHasManyToOneRelationshipWithOutputType()
{
// arrange
IRequestExecutor executor = await new ServiceCollection()
.AddGraphQL()
.AddFiltering()
.AddSorting()
.AddProjections()
.AddQueryType<FirstOrDefaulQuery>()
.AddMutationType<FirstOrDefaultMutation_ManyToOne>()
.BuildRequestExecutorAsync();

// act
var result = executor.Schema.Print();

// assert
result.MatchSnapshot();
}

public class FooType : ObjectType
{
protected override void Configure(IObjectTypeDescriptor descriptor)
Expand Down Expand Up @@ -736,5 +804,46 @@ public IQueryable<Book> GetBooks() => new[]
new Book { Id = 1, Title = "BookTitle", Author = new Author { Name = "Author" } }
}.AsQueryable();
}

public class BookInput
{
public int Id { get; set; }

}

public class FirstOrDefaulQuery
{
[UseFirstOrDefault, UseProjection]
public IQueryable<Book> GetBooks(Book book) => new[]
{
new Book
{
Id = 1, Title = "BookTitle", Author = new Author { Name = "Author" }
},
new Book
{
Id = 2, Title = "BookTitle2", Author = new Author { Name = "Author2" }
}
}.AsQueryable()
.Where(x => x.Id == book.Id);
}

public class FirstOrDefaultMutation_ManyToMany
{
[UseFirstOrDefault, UseProjection]
public IQueryable<Author> AddPublisher(Publisher publisher) => new[]
{
new Author { Name = "Author", Publishers = new List<Publisher> { publisher } }
}.AsQueryable();
}

public class FirstOrDefaultMutation_ManyToOne
{
[UseFirstOrDefault, UseProjection]
public IQueryable<Author> AddBook(Book book) => new[]
{
new Author { Name = "Author", Books = new List<Book> { book } }
}.AsQueryable();
}
}
}
19 changes: 19 additions & 0 deletions src/HotChocolate/Data/test/Data.Tests/Publisher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Collections.Generic;

namespace HotChocolate.Data
{
public class Publisher
{
public int Id { get; set; }

public string Name { get; set; }

public string Address { get; set; }

public string Zipcode { get; set; }

public virtual ICollection<Book> Books { get; set; }

public virtual ICollection<Author> Authors { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
"Id": 1,
"AuthorId": 0,
"Title": "Foo1",
"Author": null
"Author": null,
"Publisher": null
}
]
],
"Publishers": []
},
"executable": "HotChocolate.Data.Author[]"
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
"Id": 1,
"AuthorId": 0,
"Title": "Foo1",
"Author": null
"Author": null,
"Publisher": null
}
]
],
"Publishers": []
},
"executable": "System.Linq.Enumerable+ListPartition`1[HotChocolate.Data.Author]"
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
"Id": 1,
"AuthorId": 0,
"Title": "Foo1",
"Author": null
"Author": null,
"Publisher": null
}
]
],
"Publishers": []
},
{
"Id": 2,
Expand All @@ -20,9 +22,11 @@
"Id": 2,
"AuthorId": 0,
"Title": "Bar1",
"Author": null
"Author": null,
"Publisher": null
}
]
],
"Publishers": []
},
{
"Id": 3,
Expand All @@ -32,9 +36,11 @@
"Id": 3,
"AuthorId": 0,
"Title": "Baz1",
"Author": null
"Author": null,
"Publisher": null
}
]
],
"Publishers": []
}
],
"executable": "HotChocolate.Data.Author[]"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ type Author {
id: Int!
name: String
books: [Book!]!
publishers: [Publisher!]!
}

type Book {
id: Int!
authorId: Int!
title: String
author: Author
publisher: Publisher
}

"A connection to a list of items."
Expand Down Expand Up @@ -49,12 +51,22 @@ type PageInfo {
endCursor: String
}

type Publisher {
id: Int!
name: String!
address: String!
zipcode: String!
books: [Book!]!
authors: [Author!]!
}

input Foo_AuthorFilterInput {
and: [Foo_AuthorFilterInput!]
or: [Foo_AuthorFilterInput!]
id: Foo_ComparableInt32OperationFilterInput
name: Foo_StringOperationFilterInput
books: Foo_ListFilterInputTypeOfBookFilterInput
publishers: Foo_ListFilterInputTypeOfPublisherFilterInput
}

input Foo_AuthorSortInput {
Expand All @@ -69,13 +81,15 @@ input Foo_BookFilterInput {
authorId: Foo_ComparableInt32OperationFilterInput
title: Foo_StringOperationFilterInput
author: Foo_AuthorFilterInput
publisher: Foo_PublisherFilterInput
}

input Foo_BookSortInput {
id: Foo_SortEnumType
authorId: Foo_SortEnumType
title: Foo_SortEnumType
author: Foo_AuthorSortInput
publisher: Foo_PublisherSortInput
}

input Foo_ComparableInt32OperationFilterInput {
Expand All @@ -93,13 +107,45 @@ input Foo_ComparableInt32OperationFilterInput {
nlte: Int
}

input Foo_ListFilterInputTypeOfAuthorFilterInput {
all: Foo_AuthorFilterInput
none: Foo_AuthorFilterInput
some: Foo_AuthorFilterInput
any: Boolean
}

input Foo_ListFilterInputTypeOfBookFilterInput {
all: Foo_BookFilterInput
none: Foo_BookFilterInput
some: Foo_BookFilterInput
any: Boolean
}

input Foo_ListFilterInputTypeOfPublisherFilterInput {
all: Foo_PublisherFilterInput
none: Foo_PublisherFilterInput
some: Foo_PublisherFilterInput
any: Boolean
}

input Foo_PublisherFilterInput {
and: [Foo_PublisherFilterInput!]
or: [Foo_PublisherFilterInput!]
id: Foo_ComparableInt32OperationFilterInput
name: Foo_StringOperationFilterInput
address: Foo_StringOperationFilterInput
zipcode: Foo_StringOperationFilterInput
books: Foo_ListFilterInputTypeOfBookFilterInput
authors: Foo_ListFilterInputTypeOfAuthorFilterInput
}

input Foo_PublisherSortInput {
id: Foo_SortEnumType
name: Foo_SortEnumType
address: Foo_SortEnumType
zipcode: Foo_SortEnumType
}

input Foo_StringOperationFilterInput {
and: [Foo_StringOperationFilterInput!]
or: [Foo_StringOperationFilterInput!]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"data": {
"books": {
"title": "BookTitle"
}
}
}
Loading

0 comments on commit e27df75

Please sign in to comment.