コード例 #1
0
        public PizzaOrderQuery(IOrderDetailService orderDetailService, IPizzaDetailService pizzaDetailService)
        {
            Name = nameof(PizzaOrderQuery);
            FieldAsync <ListGraphType <OrderDetailsType> >(name: "newOrders", resolve:
                                                           async context => await orderDetailService.GetAllNewOrdersAsync()).AuthorizeWith(Constants.AuthPolicy.RestaurantPolicy);

            FieldAsync <PizzaDetailsType>("pizzaDetails", arguments: new QueryArguments(new QueryArgument <IntGraphType>
            {
                Name = "id"
            }), resolve: async context => await pizzaDetailService.GetPizzaDetail(context.GetArgument <int>("id")));

            FieldAsync <OrderDetailsType>("orderDetails", arguments: new QueryArguments(new QueryArgument <IntGraphType>
            {
                Name = "id"
            }), resolve: async context => await orderDetailService.GetOrderDetailAsync(context.GetArgument <int>("id")));

            Connection <OrderDetailsType>().Name("completedOrder")
            .Unidirectional()
            .PageSize(10)
            .Argument <CompletedOrderOrderByInputType>("orderBy", "pass in field and direction in which you want to sort data")
            .ResolveAsync(async context =>
            {
                var pagedRequested = new PagedRequest
                {
                    First   = context.First,
                    Last    = context.Last,
                    Before  = context.Before,
                    After   = context.After,
                    OrderBy = context.GetArgument <SortingDetails <CompletedOrderSortingFields> >("orderBy")
                };

                var pagedResponse = await orderDetailService.GetCompletedOrdersAsync(pagedRequested);
                var(startingCursor, endingCursor) =
                    CursorHelper.GetFirstAndLastCursor(pagedResponse.Nodes.Select(x => x.Id).ToList());
                var edge = pagedResponse.Nodes.Select(x => new Edge <OrderDetail>
                {
                    Cursor = CursorHelper.ToCursor(x.Id),
                    Node   = x
                }).ToList();
                var connection = new Connection <OrderDetail>
                {
                    Edges      = edge,
                    TotalCount = pagedResponse.TotalCount,
                    PageInfo   = new PageInfo
                    {
                        HasNextPage     = pagedResponse.HasNextPage,
                        HasPreviousPage = pagedResponse.HasPreviousPage,
                        EndCursor       = endingCursor,
                        StartCursor     = startingCursor
                    }
                };
                return(connection);
            });
        }
コード例 #2
0
        public PizzaOrderQuery(IOrderDetailService orderDetailService, IPizzaDetailService pizzaDetailService)
        {
            this.service            = orderDetailService;
            this.pizzaDetailService = pizzaDetailService;
            Name = nameof(PizzaOrderQuery);
            FieldAsync <ListGraphType <OrderDetailType> >(name: "newOrders",
                                                          resolve: async context => await orderDetailService.GetAllNewOrdersAsync());


            FieldAsync <PizzaDetailType>(name: "pizzaDetails",
                                         arguments: new QueryArguments(new QueryArgument <NonNullGraphType <IntGraphType> >()
            {
                Name = "id"
            }),
                                         resolve: async context => await pizzaDetailService.GetPizzaDetailAsync(context.GetArgument <int>("id")));

            FieldAsync <OrderDetailType>(name: "orderDetails",
                                         arguments: new QueryArguments(new QueryArgument <NonNullGraphType <IntGraphType> >()
            {
                Name = "id"
            }),
                                         resolve: async context => await orderDetailService.GetOrderDetailAsync(context.GetArgument <int>("id"))).AuthorizeWith(AuthPolicy.RestaurantPolicy);

            Connection <OrderDetailType>()
            .Name("completedOrders")
            // .Unidirectional() exposes first and after
            .Bidirectional()
            .Argument <CompletedOrderOrderByInputType>("orderBy", "Pass field & direction on which you want to sort data")
            .ResolveAsync(async context =>
            {
                var pageRequest = new PageRequest
                {
                    First   = context.First,
                    Last    = context.Last,
                    After   = context.After,
                    Before  = context.Before,
                    OrderBy = context.GetArgument <SortingDetails <CompletedOrdersSortingFields> >("orderBy")
                };

                var pageResponse = await orderDetailService.GetCompletedOrdersAsync(pageRequest);

                (string startCursor, string endCursor) = CursorHelper.GetFirstAndLastCursor(pageResponse.Nodes.Select(x => x.Id));

                var edge = pageResponse.Nodes.Select(x => new Edge <OrderDetail>
                {
                    Cursor = CursorHelper.ToCursor(x.Id),
                    Node   = x
                }).ToList();

                var connection = new Connection <OrderDetail>()
                {
                    Edges      = edge,
                    TotalCount = pageResponse.TotalCount,
                    PageInfo   = new PageInfo
                    {
                        HasNextPage     = pageResponse.HasNextPage,
                        HasPreviousPage = pageResponse.HasPreviousPage,
                        StartCursor     = startCursor,
                        EndCursor       = endCursor
                    }
                };

                return(connection);
            });
        }
コード例 #3
0
        public PizzaOrderQuery(IOrderDetailsService orderDetailsService, IPizzaDetailsService pizzaDetailsService)
        {
            Name = nameof(PizzaOrderQuery);
            //this.AuthorizeWith(Constants.AuthPolicy.CustomerPolicy, Constants.AuthPolicy.RestaurantPolicy);

            FieldAsync <ListGraphType <OrderDetailsType> >(
                name: "newOrders",
                resolve: async context => await orderDetailsService.GettAllNewOrdersAsync());
            //.AuthorizeWith(Constants.AuthPolicy.RestaurantPolicy);

            FieldAsync <PizzaDetailsType>(
                name: "pizzaDetails",
                arguments: new QueryArguments(new QueryArgument <IntGraphType> {
                Name = "id"
            }),
                resolve: async context => await pizzaDetailsService.GetPizzaDetailsAsync(context.GetArgument <int>("id")));

            FieldAsync <OrderDetailsType>(
                name: "orderDetails",
                arguments: new QueryArguments(new QueryArgument <IntGraphType> {
                Name = "id"
            }),
                resolve: async context => await orderDetailsService.GetOrderDetailsAsync(context.GetArgument <int>("id")))
            .AuthorizeWith(Constants.AuthPolicy.AdminPolicy);

            Connection <OrderDetailsType>()
            .Name("completedOrders")
            .Unidirectional()
            .PageSize(10)
            .Argument <CompletedOrderOrderByInputType>("orderBy", "Pass field & direction on which you want to sort data")
            .ResolveAsync(async context =>
            {
                var pageRequest = new PageRequest
                {
                    First   = context.First,
                    Last    = context.Last,
                    After   = context.After,
                    Before  = context.Before,
                    OrderBy = context.GetArgument <SortingDetails <CompletedOrdersSortingFields> >("orderBy")
                };

                var pageResponse = await orderDetailsService.GetCompletedOrdersAsync(pageRequest);

                (string startCursor, string endCursor) = CursorHelper.GetFirstAndLastCursor(pageResponse.Nodes.Select(x => x.Id));

                var edge = pageResponse.Nodes.Select(x => new Edge <OrderDetails>
                {
                    Cursor = CursorHelper.ToCursor(x.Id),
                    Node   = x
                }).ToList();

                var connection = new Connection <OrderDetails>()
                {
                    Edges      = edge,
                    TotalCount = pageResponse.TotalCount,
                    PageInfo   = new PageInfo
                    {
                        HasNextPage     = pageResponse.HasNextPage,
                        HasPreviousPage = pageResponse.HasPreviousPage,
                        StartCursor     = startCursor,
                        EndCursor       = endCursor
                    }
                };

                return(connection);
            });

            Field <PizzaDetailsType>(
                name: "exceptionDemo",
                resolve: context =>
            {
                var data = new Dictionary <string, string>
                {
                    { "key", "value" }
                };

                var ex = new ExecutionError("Some error message", data);
                ex.AddLocation(20, 500);
                context.Errors.Add(ex);

                return(pizzaDetailsService.GetPizzaDetailsOrError());
            });
        }