I'm trying to work through a very simple example of a graphql server and picked.
https://www.blexin.com/en-US/Article/Blog/Creating-our-API-with-GraphQL-and-Hot-Chocolate-79
(there's no SQL back end, I want to understand it end to end, so this has in memory tables of data).
I've implemented it pretty much as per the walkthrough (some of the attributes e.g "[UseFiltering]" don't compile, I've just commented them out for the moment).
my start up looks like this though
services.AddSingleton<IAuthorService, InMemoryAuthorService>();
services.AddSingleton<IBookService, InMemoryBookService>();
services.
AddGraphQLServer().
AddType<AuthorType>().
AddType<BookType>().
AddQueryType<GraphQL.Query>();
The piece that seems odd is when I try to query the author of a book
{
books {
title
authorId
price
author {
name
surname
}
}
}
Banana cake pop complains
field author argument book of type BookInput! is required
(if I don't ask for the Author then everything works nicely)
booktype looks like this (as per the walkthrough)
public class BookType : ObjectType<Book>
{
protected override void Configure(IObjectTypeDescriptor<Book> descriptor)
{
descriptor.Field(b => b.Id).Type<IdType>();
descriptor.Field(b => b.Title).Type<StringType>();
descriptor.Field(b => b.Price).Type<DecimalType>();
descriptor.Field<AuthorResolver>(t => t.GetAuthor(default, default));
}
}
author resolver looks like this
public class AuthorResolver
{
private readonly IAuthorService _authorService;
public AuthorResolver([Service]IAuthorService authorService)
{
_authorService = authorService;
}
public Author GetAuthor(Book book, IResolverContext ctx)
{
return _authorService.GetAll().Where(a => a.Id == book.AuthorId).FirstOrDefault();
}
}
again, as per the walkthrough.
I basically understand the error, but I can't comprehend how this was ever supposed to work, somehow the "book" parent has to get into the GetAuthor method on the AuthorResolver....I'm missing some magic, or v11 is missing some magic.
P.S.
my preference is for declarative typed expressions, rather than reflexive magic...so maybe I'm missing something
The problem is that the GetAuthor method on my AuthorResolver, was missing the "Parent" attribute, to trigger some magic....
public Author GetAuthor([Parent]Book book, IResolverContext ctx)
{
return _authorService.GetAll().Where(a => a.Id == book.AuthorId).FirstOrDefault();
}
Ideally I would want to remove this custom attribute magic.