Exemplo n.º 1
0
    public OrderType(IDataLoaderContextAccessor accessor, IUsersStore users, IOrdersStore orders)
    {
        Name = "Order";

        Field(x => x.OrderId);
        Field(x => x.OrderedOn);

        Field <UserType, User>()
        .Name("User")
        .ResolveAsync(ctx =>
        {
            var loader = accessor.Context.GetOrAddBatchLoader <int, User>("GetUsersById",
                                                                          users.GetUsersByIdAsync);

            return(loader.LoadAsync(ctx.Source.UserId));
        });

        Field <ListGraphType <OrderItemType>, IEnumerable <OrderItem> >()
        .Name("Items")
        .ResolveAsync(ctx =>
        {
            var loader = accessor.Context.GetOrAddCollectionBatchLoader <int, OrderItem>("GetOrderItemsById",
                                                                                         orders.GetItemsByOrderIdAsync);

            return(loader.LoadAsync(ctx.Source.OrderId));
        });
    }
Exemplo n.º 2
0
        public QueryType(IDataLoaderContextAccessor accessor, IUsersStore users, IOrdersStore orders)
        {
            Name = "Query";

            Field <ListGraphType <UserType>, IEnumerable <User> >()
            .Name("Users")
            .Description("Get all Users")
            .Returns <IEnumerable <User> >()
            .ResolveAsync(ctx =>
            {
                var loader = accessor.Context.GetOrAddLoader("GetAllUsers",
                                                             users.GetAllUsersAsync);

                return(loader.LoadAsync());
            });

            Field <ListGraphType <UserType>, IEnumerable <User> >()
            .Name("UsersWithDelay")
            .Description("Get all Users")
            .Returns <IEnumerable <User> >()
            .ResolveAsync(async ctx =>
            {
                await System.Threading.Tasks.Task.Delay(20);

                var loader = accessor.Context.GetOrAddLoader("GetAllUsersWithDelay",
                                                             users.GetAllUsersAsync);

                return(loader.LoadAsync());
            });

            Field <OrderType, Order>()
            .Name("Order")
            .Description("Get Order by ID")
            .Argument <NonNullGraphType <IntGraphType> >("orderId", "")
            .ResolveAsync(ctx =>
            {
                var loader = accessor.Context.GetOrAddBatchLoader <int, Order>("GetOrderById",
                                                                               orders.GetOrderByIdAsync, x => x.OrderId);

                return(loader.LoadAsync(ctx.GetArgument <int>("orderId")));
            });

            Field <ListGraphType <OrderType>, IEnumerable <Order> >()
            .Name("Orders")
            .Description("Get all Orders")
            .ResolveAsync(ctx =>
            {
                var loader = accessor.Context.GetOrAddLoader("GetAllOrders",
                                                             orders.GetAllOrdersAsync);

                return(loader.LoadAsync());
            });
        }
    public SubscriptionType(IOrdersStore ordersStore)
    {
        this.ordersStore = ordersStore;

        Name = "Subscription";

        AddField(new FieldType
        {
            Name           = "orderAdded",
            Type           = typeof(OrderType),
            Resolver       = new FuncFieldResolver <Order>(ResolveMessage),
            StreamResolver = new SourceStreamResolver <Order>(ResolveStream)
        });
    }
Exemplo n.º 4
0
        public UserType(IDataLoaderContextAccessor accessor, IOrdersStore orders)
        {
            Name = "User";

            Field(x => x.UserId);
            Field(x => x.FirstName);
            Field(x => x.LastName);
            Field(x => x.Email);

            Field <ListGraphType <OrderType>, IEnumerable <Order> >()
            .Name("Orders")
            .ResolveAsync(ctx =>
            {
                var ordersLoader = accessor.Context.GetOrAddCollectionBatchLoader <int, Order>("GetOrdersByUserId",
                                                                                               orders.GetOrdersByUserIdAsync);

                return(ordersLoader.LoadAsync(ctx.Source.UserId));
            });

            Field <ListGraphType <OrderItemType>, IEnumerable <OrderItem> >()
            .Name("OrderedItems")
            .ResolveAsync(ctx =>
            {
                //obtain a reference to the GetOrdersByUserId batch loader
                var ordersLoader = accessor.Context.GetOrAddCollectionBatchLoader <int, Order>("GetOrdersByUserId",
                                                                                               orders.GetOrdersByUserIdAsync);

                //wait for dataloader to pull the orders for this user
                var ret = ordersLoader.LoadAsync(ctx.Source.UserId).Then(orderResults =>
                {
                    //obtain a reference to the GetOrderItemsById batch loader
                    var itemsLoader = accessor.Context.GetOrAddCollectionBatchLoader <int, OrderItem>("GetOrderItemsById",
                                                                                                      orders.GetItemsByOrderIdAsync);

                    //wait for dataloader to pull the items for each order
                    return(itemsLoader.LoadAsync(orderResults.Select(o => o.OrderId)).Then(allResults =>
                    {
                        //without dataloader, this would be:
                        //var batchResults = await orders.GetItemsByOrderIdAsync(orderResults.Select(o => o.OrderId));
                        //var allResults = orderResults.Select(o => batchResults[o.OrderId]);

                        //flatten and return the results
                        return allResults.SelectMany(x => x);
                    }));
                });

                return(ret);
            });
        }
Exemplo n.º 5
0
        public MyComplexQueryType(IDataLoaderContextAccessor accessor, IOrdersStore orders)
        {
            Field <ListGraphType <OrderType>, IEnumerable <Order> >()
            .Name("Orders")
            .ResolveAsync(ctx =>
            {
                // Get or add a collection batch loader with the key "GetOrdersByUserId"
                // The loader will call GetOrdersByUserIdAsync with a batch of keys
                var ordersLoader = accessor.Context.GetOrAddCollectionBatchLoader <int, Order>("GetOrdersByUserId",
                                                                                               orders.GetOrdersByUserIdAsync);

                // Add this UserId to the pending keys to fetch data for
                // The execution strategy will trigger the data loader to fetch the data via GetOrdersByUserId() at the
                //   appropriate time, and the field will be resolved with an instance of IEnumerable<Order> once
                //   GetOrdersByUserId() returns with the batched results
                return(ordersLoader.LoadAsync(ctx.Source.Id));
            });
        }
Exemplo n.º 6
0
        public UserType(IDataLoaderContextAccessor accessor, IOrdersStore orders)
        {
            Name = "User";

            Field(x => x.UserId);
            Field(x => x.FirstName);
            Field(x => x.LastName);
            Field(x => x.Email);

            Field <ListGraphType <OrderType>, IEnumerable <Order> >()
            .Name("Orders")
            .ResolveAsync(ctx =>
            {
                var ordersLoader = accessor.Context.GetOrAddCollectionBatchLoader <int, Order>("GetOrdersByUserId",
                                                                                               orders.GetOrdersByUserIdAsync);

                return(ordersLoader.LoadAsync(ctx.Source.UserId));
            });
        }
Exemplo n.º 7
0
        public QueryType(IDataLoaderContextAccessor accessor, IUsersStore users, IOrdersStore orders)
        {
            Name = "Query";

            Field <ListGraphType <UserType>, IEnumerable <User> >()
            .Name("Users")
            .Description("Get all Users")
            .Returns <IEnumerable <User> >()
            .ResolveAsync(ctx =>
            {
                var loader = accessor.Context.GetOrAddLoader("GetAllUsers",
                                                             users.GetAllUsersAsync);

                return(loader.LoadAsync());
            });

            Field <ListGraphType <UserType>, IEnumerable <User> >()
            .Name("UsersWithDelay")
            .Description("Get all Users")
            .Returns <IEnumerable <User> >()
            .ResolveAsync(async ctx =>
            {
                await System.Threading.Tasks.Task.Delay(20);

                var loader = accessor.Context.GetOrAddLoader("GetAllUsersWithDelay",
                                                             users.GetAllUsersAsync);

                return(loader.LoadAsync());
            });

            Field <OrderType, Order>()
            .Name("Order")
            .Description("Get Order by ID")
            .Argument <NonNullGraphType <IntGraphType> >("orderId", "")
            .ResolveAsync(ctx =>
            {
                var loader = accessor.Context.GetOrAddBatchLoader <int, Order>("GetOrderById",
                                                                               orders.GetOrderByIdAsync, x => x.OrderId);

                return(loader.LoadAsync(ctx.GetArgument <int>("orderId")));
            });

            Field <ListGraphType <OrderType>, IEnumerable <Order> >()
            .Name("Orders")
            .Description("Get all Orders")
            .ResolveAsync(ctx =>
            {
                var loader = accessor.Context.GetOrAddLoader("GetAllOrders",
                                                             orders.GetAllOrdersAsync);

                return(loader.LoadAsync());
            });

            Field <NonNullGraphType <ListGraphType <UserType> >, IEnumerable <IDataLoaderResult <User> > >()
            .Name("SpecifiedUsers")
            .Description("Get Users by ID")
            .Argument <NonNullGraphType <ListGraphType <NonNullGraphType <IntGraphType> > > >("ids")
            .Resolve(ctx =>
            {
                var loader = accessor.Context.GetOrAddBatchLoader <int, User>("GetUserById",
                                                                              users.GetUsersByIdAsync);

                var ids = ctx.GetArgument <IEnumerable <int> >("ids");
                var ret = ids.Select(id => loader.LoadAsync(id));
                return(ret);
            });

            Field <NonNullGraphType <ListGraphType <NonNullGraphType <ListGraphType <NonNullGraphType <IntGraphType> > > > > >()
            .Name("ExerciseListsOfLists")
            .Argument <ListGraphType <ListGraphType <IntGraphType> > >("values")
            .Resolve(ctx =>
            {
                var ret  = ctx.GetArgument <IEnumerable <IEnumerable <int?> > >("values"); //new int?[][] { new int?[] { 1, 2 }, new int?[] { 4, 5, 6 } };
                var ret2 = ret.Select(x => new SimpleDataLoader <IEnumerable <SimpleDataLoader <int?> > >(_ => Task.FromResult(x.Select(y => new SimpleDataLoader <int?>(_ => Task.FromResult(y))))));
                return(ret2);
            });
        }
 public ReceivingController(IOrdersStore ordersStore)
 {
     _ordersStore = ordersStore;
 }
Exemplo n.º 9
0
 public ValuesController(IOrdersStore ordersStore)
 {
     _ordersStore = ordersStore;
 }
Exemplo n.º 10
0
 public ShippingController(IOrdersStore ordersStore)
 {
     _ordersStore = ordersStore;
 }
Exemplo n.º 11
0
    public QueryType(IDataLoaderContextAccessor accessor, IUsersStore users, IOrdersStore orders)
    {
        Name = "Query";

        Field <ListGraphType <UserType>, IEnumerable <User> >()
        .Name("Users")
        .Description("Get all Users")
        .Returns <IEnumerable <User> >()
        .ResolveAsync(ctx =>
        {
            var loader = accessor.Context.GetOrAddLoader("GetAllUsers",
                                                         users.GetAllUsersAsync);

            return(loader.LoadAsync());
        });

        Field <ListGraphType <UserType>, IEnumerable <User> >()
        .Name("UsersWithDelay")
        .Description("Get all Users")
        .Returns <IEnumerable <User> >()
        .ResolveAsync(async ctx =>
        {
            await System.Threading.Tasks.Task.Delay(20).ConfigureAwait(false);

            var loader = accessor.Context.GetOrAddLoader("GetAllUsersWithDelay",
                                                         users.GetAllUsersAsync);

            return(loader.LoadAsync());
        });

        Field <OrderType, Order>()
        .Name("Order")
        .Description("Get Order by ID")
        .Argument <NonNullGraphType <IntGraphType> >("orderId", "")
        .ResolveAsync(ctx =>
        {
            var loader = accessor.Context.GetOrAddBatchLoader <int, Order>("GetOrderById",
                                                                           orders.GetOrderByIdAsync, x => x.OrderId);

            return(loader.LoadAsync(ctx.GetArgument <int>("orderId")));
        });

        Field <ListGraphType <OrderType>, IEnumerable <Order> >()
        .Name("Orders")
        .Description("Get all Orders")
        .ResolveAsync(ctx =>
        {
            var loader = accessor.Context.GetOrAddLoader("GetAllOrders",
                                                         orders.GetAllOrdersAsync);

            return(loader.LoadAsync());
        });

        Field <NonNullGraphType <ListGraphType <UserType> >, IEnumerable <IDataLoaderResult <User> > >()
        .Name("SpecifiedUsers")
        .Description("Get Users by ID")
        .Argument <NonNullGraphType <ListGraphType <NonNullGraphType <IntGraphType> > > >("ids")
        .Resolve(ctx =>
        {
            var loader = accessor.Context.GetOrAddBatchLoader <int, User>("GetUserById",
                                                                          users.GetUsersByIdAsync);

            var ids = ctx.GetArgument <IEnumerable <int> >("ids");
            var ret = ids.Select(id => loader.LoadAsync(id));
            return(ret);
        });

        Field <NonNullGraphType <ListGraphType <UserType> >, IDataLoaderResult <IEnumerable <User> > >()
        .Name("SpecifiedUsersWithThen")
        .Description("Get Users by ID skipping null matches")
        .Argument <NonNullGraphType <ListGraphType <NonNullGraphType <IntGraphType> > > >("ids")
        .Resolve(ctx =>
        {
            var loader = accessor.Context.GetOrAddBatchLoader <int, User>("GetUserById",
                                                                          users.GetUsersByIdAsync);

            var ids = ctx.GetArgument <IEnumerable <int> >("ids");
            // note: does not work properly without ToList, because LoadAsync would not have
            // been called, so the ids would not have been queued for execution prior to the
            // first call to GetResultAsync
            var ret  = ids.Select(id => loader.LoadAsync(id)).ToList();
            var ret2 = ret.Then(values => values.Where(x => x != null));
            return(ret2);
        });

        Field <NonNullGraphType <ListGraphType <NonNullGraphType <ListGraphType <NonNullGraphType <IntGraphType> > > > > >()
        .Name("ExerciseListsOfLists")
        .Argument <ListGraphType <ListGraphType <IntGraphType> > >("values")
        .Resolve(ctx =>
        {
            var ret  = ctx.GetArgument <IEnumerable <IEnumerable <int?> > >("values"); //new int?[][] { new int?[] { 1, 2 }, new int?[] { 4, 5, 6 } };
            var ret2 = ret.Select(x => new SimpleDataLoader <IEnumerable <SimpleDataLoader <int?> > >(_ => Task.FromResult(x.Select(y => new SimpleDataLoader <int?>(_ => Task.FromResult(y))))));
            return(ret2);
        });
    }
 public PostalCarrierController(IOrdersStore ordersStore)
 {
     _ordersStore = ordersStore;
 }