//Här använder jag asyncron metoden för att böcker och författare ska kunna hämtas oberoende av main tråden //samt att await pausar all vidare exekvering fram tills alla böcker och författare är framtagna. public async static void GetAll() { var bookRepo = new BooksRepository(); var authorRepo = new AuthorsRepository(); Console.WriteLine("Starting"); Task <ICollection <Book> > books = bookRepo.GetAllAsync(); foreach (var book in books.Result) { Console.WriteLine(book.Title); } Console.WriteLine("In process"); Task <ICollection <Author> > authors = authorRepo.GetAllAsync(); foreach (var author in authors.Result) { Console.WriteLine(author.FirstName); } await Task.WhenAll(books, authors); Console.WriteLine("Both tasks done"); }
public LibraryQuery(BooksRepository booksRepository, AuthorsRepository authorsRepository) { FieldAsync <ListGraphType <BookType> >( "books", resolve: async context => { return(await booksRepository.GetAllAsync()); }); Field <BookType>( "book", arguments: new QueryArguments(new QueryArgument <NonNullGraphType <IdGraphType> > { Name = "id" }), resolve: context => { var id = context.GetArgument <long>("id"); return(booksRepository.GetById(id)); }); Field <ListGraphType <AuthorType> >( "authors", resolve: context => authorsRepository.GetAuthorsAsync()); Field <AuthorType>( "author", arguments: new QueryArguments(new QueryArgument <NonNullGraphType <IdGraphType> > { Name = "id" }), resolve: context => { var id = context.GetArgument <long>("id"); return(authorsRepository.GetAuthorById(id)); }); }