public static void AddPublisherQueries(this BooksQuery booksQuery, QueryBuilderFactory queryBuilderFactory)
 {
     queryBuilderFactory.Create <Publisher>(booksQuery, GetPublisherByIdQuery)
     .WithCache(TimeSpan.FromSeconds(10))
     .WithParameterBuilder()
     .WithProperty(x => x.Id)
     .BuildQuery()
     .BuildWithSingleResult();
 }
        public static void AddBooksQueries(this BooksQuery booksQuery, QueryBuilderFactory queryBuilderFactory)
        {
            // Example 1: We are getting a single Book. You are defining the argument yourself to pass into the repository with context. There's no caching and paging support. This is what comes out-of-the-box.

            queryBuilderFactory.Create <Book>(booksQuery, "getBookNoCache")
            .WithParameterBuilder()
            .WithProperty(x => x.Id)
            .BuildQuery()
            .BuildWithSingleResult();

            // Example 2: We are getting a single Book. The argument to pass into the repository is defined by the Model with at least one property with the KeyAttribute.
            //            The work is done by the cache repository which will cache the book result for a specific time you have defined. There's no paging support.

            queryBuilderFactory.Create <Book>(booksQuery, "getBook")
            .WithCache(TimeSpan.FromSeconds(10))
            .WithParameterBuilder()
            .WithKeys()
            .BuildQuery()
            .BuildWithSingleResult();

            // Example 3: We are getting a list of Books based on an argument. You are defining the key to pass into the repository without having to use context directly.
            //            The cache repository which will cache the book result for a specific time you have defined. There's no paging support.

            queryBuilderFactory.Create <Book>(booksQuery, "getBooksByCategory")
            .WithCache(TimeSpan.FromSeconds(10))
            .WithParameterBuilder()
            .WithProperty(x => x.Category)
            .BuildQuery()
            .BuildWithListResult();

            // Example 4: We are getting a list of paged Books. Technically, you are able to get all books by using TotalCount, although there's already a default page limit of 10 items per page if you don't specify.
            //            There's no caching support.

            queryBuilderFactory.Create <Book>(booksQuery, "getPagedBooks")
            .WithPaging()
            .BuildWithListResult();

            queryBuilderFactory.Create <Book>(booksQuery, "getPagedBooksByCategory")
            .WithPaging()
            .WithParameterBuilder()
            .WithProperty(x => x.Category, true)
            .BuildQuery()
            .BuildWithListResult();

            // Example 6: We are getting a list of paged Books with a argument to be passed in. You are defining the key to pass into the repository without having to use context directly.
            //            The cache repository which will cache the book result for a specific time you have defined. You will get paged results with a default page limit of 10 items per page if you don't specify.

            queryBuilderFactory.Create <Book>(booksQuery, "getCachedPagedBooksByCategory")
            .WithPaging()
            .WithCache(TimeSpan.FromSeconds(10))
            .WithParameterBuilder()
            .WithProperty(x => x.Category)
            .BuildQuery()
            .BuildWithListResult();
        }
        public static void AddBookReviewQueries(this BooksQuery booksQuery, QueryBuilderFactory queryBuilderFactory)
        {
            queryBuilderFactory.Create <BookReviewOutput>(booksQuery, "GetBookReviewByBookName")
            .WithCache(TimeSpan.FromSeconds(10))
            .WithParameterBuilder()
            .BeginQuery <Book>()
            .WithProperty(x => x.Name)
            .BuildQueryResult(ctx =>
            {
                // Temporary store books in storage.
                ctx.Items["books"] = ctx.GetQueryResults <Book>();
            })
            .ThenWithQuery <BookReview>()
            .WithPropertyFromSource(x => x.BookId, ctx => ctx.GetItems <Book>("books").Select(y => (object)y.Id).ToList())
            .BuildQueryResult(ctx =>
            {
                var bookReviews = ctx.GetQueryResults <BookReview>();

                ctx.Items["reviewerIdList"] = bookReviews.Select(x => (object)x.ReviewerId).ToList();

                var books = ctx.GetItems <Book>("books");

                ctx.SetResults(bookReviews.Select(br => new BookReviewOutput
                {
                    Id         = br.Id,
                    BookId     = br.BookId,
                    Comments   = br.Comments,
                    ReviewerId = br.ReviewerId,
                    Stars      = br.Stars,
                    Book       = books.Single(x => x.Id == br.BookId)
                }).ToList());
            })
            .ThenWithQuery <Reviewer>()
            .WithPropertyFromSource(x => x.Id, ctx => (List <object>)ctx.Items["reviewerIdList"])
            .BuildQueryResult(ctx =>
            {
                var reviewers = ctx.GetQueryResults <Reviewer>();
                ctx.GetResults <BookReviewOutput>().ForEach(
                    x => x.Reviewer = reviewers.Single(y => y.Id == x.ReviewerId));
            })
            .BuildQuery()
            .BuildWithListResult();


            queryBuilderFactory.Create <BookReviewOutput>(booksQuery, "GetBookReviewsWithBookNameAndCategory")
            .WithCache(TimeSpan.FromSeconds(10))
            .WithParameterBuilder()
            .BeginQuery <Book>()                     // Gives you the ability to query with both book name and category.
            .WithProperty(x => x.Name)
            .WithProperty(x => x.Category)
            .BuildQueryResult(ctx =>
            {
                // Temporary store books in storage.
                ctx.Items["books"] = ctx.GetQueryResults <Book>();
            })
            .ThenWithQuery <BookReview>()                    // Gives you the ability to query with book review stars, written on, active reviews and book Id matches.
            .WithPropertyFromSource(x => x.BookId, ctx => ctx.GetItems <Book>("books").Select(y => (object)y.Id).ToList())
            .WithProperty(x => x.Stars)
            .WithProperty(x => x.Active)
            .WithProperty(x => x.WrittenOn)
            .BuildQueryResult(ctx =>
            {
                var bookReviews = ctx.GetQueryResults <BookReview>();

                ctx.Items["reviewerIdList"] = bookReviews.Select(x => (object)x.ReviewerId).ToList();

                var books = ctx.GetItems <Book>("books");

                ctx.SetResults(bookReviews.Select(br => new BookReviewOutput
                {
                    Id         = br.Id,
                    BookId     = br.BookId,
                    Comments   = br.Comments,
                    ReviewerId = br.ReviewerId,
                    Stars      = br.Stars,
                    Book       = books.Single(x => x.Id == br.BookId),
                    WrittenOn  = br.WrittenOn,
                    Active     = br.Active
                }).ToList());
            })
            .ThenWithQuery <Reviewer>()
            .WithPropertyFromSource(x => x.Id, ctx => (List <object>)ctx.Items["reviewerIdList"])
            .BuildQueryResult(ctx =>
            {
                var reviewers = ctx.GetQueryResults <Reviewer>();
                ctx.GetResults <BookReviewOutput>().ForEach(
                    x => x.Reviewer = reviewers.Single(y => y.Id == x.ReviewerId));
            })
            .BuildQuery()
            .BuildWithListResult();

            // Example of how we are leveraging searches across business domains, reviewers and books.
            queryBuilderFactory.Create <BookReviewOutput>(booksQuery, "SearchBookReviews")
            .WithCache(TimeSpan.FromSeconds(10))
            .WithParameterBuilder()
            .BeginSearch()
            .Add <BookSearch>().Add <ReviewerSearch>().Build()
            .BuildQueryResult(ctx =>
            {
                var searches     = ctx.GetQueryResults <SearchResultModel>();
                var bookSearches = searches.GetTypeList <BookSearch>();

                ctx.Items["bookSearches"]       = bookSearches;
                ctx.Items["bookSearchesIdList"] = bookSearches.Select(x => x.Id).ToList();

                var reviewerSearches = searches.GetTypeList <ReviewerSearch>();

                ctx.Items["reviewerSearchesIdList"] = reviewerSearches.Select(x => x.Id).ToList();
            })
            .ThenWithQuery <BookReview>()
            .WithPropertyFromSource(x => x.BookId, ctx => ctx.ConvertItemsToObjectList <string>("bookSearchesIdList"))
            .BuildQueryResult(ctx =>
            {
                // Temporary store books in storage.
                ctx.Items["bookReviews"] = ctx.GetQueryResults <BookReview>();
            })
            .ThenWithQuery <BookReview>()
            .WithPropertyFromSource(x => x.ReviewerId, ctx => ctx.ConvertItemsToObjectList <string>("reviewerSearchesIdList"))
            .BuildQueryResult(ctx =>
            {
                // Combine the results of book reviews from book Id list and reviewer Id List.
                List <BookReview> bookReviews = (List <BookReview>)ctx.Items["bookReviews"];

                bookReviews.AddRange(ctx.GetQueryResults <BookReview>());

                var finalResults = bookReviews.Distinct().Select(br => new BookReviewOutput
                {
                    Id         = br.Id,
                    BookId     = br.BookId,
                    Comments   = br.Comments,
                    ReviewerId = br.ReviewerId,
                    Stars      = br.Stars,
                    WrittenOn  = br.WrittenOn,
                    Active     = br.Active
                }).ToList();

                ctx.Items["bookIdList"]     = finalResults.Select(x => x.BookId).Distinct().ToList();
                ctx.Items["reviewerIdList"] = finalResults.Select(x => x.ReviewerId).Distinct().ToList();

                ctx.SetResults(finalResults);
            })
            .ThenWithQuery <Book>()
            .WithPropertyFromSource(x => x.Id, ctx => ctx.ConvertItemsToObjectList <string>("bookIdList"))
            .BuildQueryResult(ctx =>
            {
                var books = ctx.GetQueryResults <Book>();
                ctx.GetResults <BookReviewOutput>().ForEach(x =>
                                                            x.Book = books.Single(b => b.Id == x.BookId));
            })
            .ThenWithQuery <Reviewer>()
            .WithPropertyFromSource(x => x.Id, ctx => ctx.ConvertItemsToObjectList <string>("reviewerIdList"))
            .BuildQueryResult(ctx =>
            {
                var reviewers = ctx.GetQueryResults <Reviewer>();
                ctx.GetResults <BookReviewOutput>().ForEach(x =>
                                                            x.Reviewer = reviewers.Single(b => b.Id == x.ReviewerId));
            })
            .BuildQuery()
            .BuildWithListResult();
        }
 public BooksSchema(IDependencyResolver resolver, BooksQuery booksQuery, BooksMutation booksMutation) : base(resolver)
 {
     Query    = booksQuery;
     Mutation = booksMutation;
 }
Ejemplo n.º 5
0
 public BooksSchema(IServiceProvider resolver, BooksQuery booksQuery, BooksMutation booksMutation) : base(resolver)
 {
     Query    = booksQuery;
     Mutation = booksMutation;
 }
        public static void AddBookAuthorsOutputQueries(this BooksQuery booksQuery, QueryBuilderFactory queryBuilderFactory)
        {
            queryBuilderFactory.Create <BookAuthorsOutput>(booksQuery, "getBookAuthorsByCategory")
            .WithPaging()
            .WithCache(TimeSpan.FromSeconds(10))
            .WithParameterBuilder()
            // We need to get all book-authors by book category. In order to perform this, we need to get all books with the category first.
            .BeginWithProperty <Book>(x => x.Category, ctx =>
            {
                // Temporary store books in storage.
                ctx.Items["books"] = ctx.GetQueryResults <Book>();
            })
            // Then, we can get all book authors what has the book ids.
            .ThenWithProperty <BookAuthors>(x => x.BookId, ctx => ctx.GetItems <Book>("books").Select(y => (object)y.Id).ToList(), ctx =>
            {
                // Map initial result.
                var bookAuthors = ctx.GetQueryResults <BookAuthors>();
                var books       = ctx.GetItems <Book>("books");

                ctx.SetResults(bookAuthors.Select(ba => new BookAuthorsOutput
                {
                    Id           = ba.Id,
                    Book         = books.Single(o => o.Id == ba.BookId),
                    AuthorIdList = ba.AuthorIdList,
                    BookId       = ba.BookId,
                    RoyaltyType  = ba.RoyaltyType
                }).ToList());
            })
            // Lastly, we can get all the authors for all the found books.
            .ThenWithProperty <Author>(x => x.Id, ctx =>
            {
                var list = new List <string>();
                ctx.GetResults <BookAuthorsOutput>().ForEach(y => list.AddRange(y.AuthorIdList));
                return(list.Distinct().Select(y => (object)y).ToList());
            }, ctx =>
            {
                // Map authors to result.
                var authors = ctx.GetQueryResults <Author>();

                // Only include authors who are in the AuthorIdList in each individual book author.
                ctx.GetResults <BookAuthorsOutput>().ForEach(ba => ba.Authors = authors.Where(x => ba.AuthorIdList.Contains(x.Id)).ToList());
            })
            .BuildQuery()
            .AssertWithClaimsPrincipal(cp => cp.IsInRole("Eklee.User.Read"))
            .BuildWithListResult();

            queryBuilderFactory.Create <BookAuthorsOutput>(booksQuery, "getBookAuthorsById")
            .WithPaging()
            .WithCache(TimeSpan.FromSeconds(10))
            .WithParameterBuilder()
            .BeginWithProperty <BookAuthors>(x => x.Id, ctx =>
            {
                ctx.SetResults(ctx.GetQueryResults <BookAuthors>().Select(ba => new BookAuthorsOutput
                {
                    Id           = ba.Id,
                    AuthorIdList = ba.AuthorIdList,
                    BookId       = ba.BookId,
                    RoyaltyType  = ba.RoyaltyType
                }).ToList());
            })
            .ThenWithProperty <Author>(x => x.Id, ctx =>
            {
                var list = new List <string>();
                ctx.GetResults <BookAuthorsOutput>().ForEach(y => list.AddRange(y.AuthorIdList));
                return(list.Distinct().Select(y => (object)y).ToList());
            }, ctx =>
            {
                // Map authors to result.
                var authors = ctx.GetQueryResults <Author>();

                // Only include authors who are in the AuthorIdList in each individual book author.
                ctx.GetResults <BookAuthorsOutput>().ForEach(ba => ba.Authors = authors.Where(x => ba.AuthorIdList.Contains(x.Id)).ToList());
            })
            .BuildQuery()
            .BuildWithSingleResult();

            queryBuilderFactory.Create <BookAuthorsOutput>(booksQuery, "getBookAuthorsByRoyaltyType")
            .WithPaging()
            .WithCache(TimeSpan.FromSeconds(10))
            .WithParameterBuilder()
            .BeginWithProperty <BookAuthors>(x => x.RoyaltyType, ctx =>
            {
                ctx.SetResults(ctx.GetQueryResults <BookAuthors>().Select(ba => new BookAuthorsOutput
                {
                    Id           = ba.Id,
                    AuthorIdList = ba.AuthorIdList,
                    BookId       = ba.BookId,
                    RoyaltyType  = ba.RoyaltyType
                }).ToList());
            })
            .ThenWithProperty <Author>(x => x.Id, ctx =>
            {
                var list = new List <string>();
                ctx.GetResults <BookAuthorsOutput>().ForEach(y => list.AddRange(y.AuthorIdList));
                return(list.Distinct().Select(y => (object)y).ToList());
            }, ctx =>
            {
                // Map authors to result.
                var authors = ctx.GetQueryResults <Author>();

                // Only include authors who are in the AuthorIdList in each individual book author.
                ctx.GetResults <BookAuthorsOutput>().ForEach(ba => ba.Authors = authors.Where(x => ba.AuthorIdList.Contains(x.Id)).ToList());
            })
            .BuildQuery()
            .BuildWithListResult();
        }