public BookType(IBooksDataSource booksDataSource, IAuthorsDataSource authorsDataSource) { Name = "Book"; Field(b => b.Id).Description("The id of the book."); Field(b => b.Name).Description("The name of the book."); Field <AuthorType>( "author", resolve: context => authorsDataSource.GetAuthor(context.Source.AuthorId) ); }
public Types.Book EditBook(int id, BookInput book) { if (book == null) { return(null); } var existingBook = booksDataSource.GetBook(id); if (existingBook == null) { return(null); } existingBook.Name = book.Name; existingBook.AuthorId = book.AuthorId; var author = authorsDataSource.GetAuthor(book.AuthorId); return(BookMapper.ConvertModelBookToGraphQLBook(existingBook, author)); }
public Query(IBooksDataSource booksDataSource, IAuthorsDataSource authorsDataSource) { Name = "Query"; Field <BookType>( "book", arguments: new QueryArguments( new QueryArgument <NonNullGraphType <IdGraphType> > { Name = "id", Description = "id of the book" } ), resolve: context => booksDataSource.GetBook(context.GetArgument <int>("id")) ); Field <StringGraphType>( "bookSource", arguments: new QueryArguments( new QueryArgument <NonNullGraphType <IdGraphType> > { Name = "id", Description = "id of the book" } ), resolve: context => JsonConvert.SerializeObject(booksDataSource.GetBook(context.GetArgument <int>("id"))) ); Field <ListGraphType <BookType> >( "books", resolve: context => booksDataSource.GetBooks() ); Field <IntGraphType>( "booksCount", resolve: context => booksDataSource.GetBooks().Count ); Field <AuthorType>( "author", arguments: new QueryArguments( new QueryArgument <NonNullGraphType <IdGraphType> > { Name = "id", Description = "id of the author" } ), resolve: context => authorsDataSource.GetAuthor(context.GetArgument <int>("id")) ); Field <StringGraphType>( "authorSource", arguments: new QueryArguments( new QueryArgument <NonNullGraphType <IdGraphType> > { Name = "id", Description = "id of the author" } ), resolve: context => JsonConvert.SerializeObject(authorsDataSource.GetAuthor(context.GetArgument <int>("id"))) ); Field <ListGraphType <AuthorType> >( "authors", resolve: context => authorsDataSource.GetAuthors() ); Field <IntGraphType>( "authorsCount", resolve: context => authorsDataSource.GetAuthors().Count ); }
private Book ConvertToGraphQLBook(Model.Book modelBook, bool includeAuthor = false) { var modelAuthor = includeAuthor ? authorsDataSource.GetAuthor(modelBook.AuthorId) : null; return(BookMapper.ConvertModelBookToGraphQLBook(modelBook, modelAuthor)); }