diff --git a/src/OrchardCore.Modules/OrchardCore.Lists/GraphQL/ListQueryObjectType.cs b/src/OrchardCore.Modules/OrchardCore.Lists/GraphQL/ListQueryObjectType.cs new file mode 100644 index 00000000000..310e90af3f3 --- /dev/null +++ b/src/OrchardCore.Modules/OrchardCore.Lists/GraphQL/ListQueryObjectType.cs @@ -0,0 +1,57 @@ +using System.Collections.Generic; +using GraphQL.Types; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Localization; +using OrchardCore.Apis.GraphQL; +using OrchardCore.ContentManagement; +using OrchardCore.ContentManagement.GraphQL.Queries.Types; +using OrchardCore.ContentManagement.Records; +using OrchardCore.Lists.Indexes; +using OrchardCore.Lists.Models; +using YesSql; + +namespace OrchardCore.Lists.GraphQL +{ + public class ListQueryObjectType : ObjectGraphType + { + public ListQueryObjectType(IStringLocalizer T) + { + Name = "ListPart"; + Description = T["Represents a collection of content items."]; + + Field, IEnumerable>() + .Name("contentItems") + .Description("the content items") + .Argument("first", "the first n elements (10 by default)", 0) + .Argument("skip", "the number of elements to skip", 0) + .ResolveAsync(async g => + { + var context = (GraphQLContext)g.UserContext; + var session = context.ServiceProvider.GetService(); + + var query = session.Query() + .With(x => x.ListContentItemId == g.Source.ContentItem.ContentItemId) + .With(x => x.Published) + .OrderByDescending(x => x.CreatedUtc); + + // Apply a default limit + var pagedQuery = query.Take(10); + + var skip = g.GetArgument("skip"); + var first = g.GetArgument("first"); + + if (skip > 0) + { + pagedQuery = pagedQuery.Skip(skip); + } + + if (first > 0) + { + pagedQuery = pagedQuery.Take(first); + } + + return await query.ListAsync(); + }); + } + } +} diff --git a/src/OrchardCore.Modules/OrchardCore.Lists/GraphQL/Startup.cs b/src/OrchardCore.Modules/OrchardCore.Lists/GraphQL/Startup.cs index 94c12ec65ad..4ac1210b042 100644 --- a/src/OrchardCore.Modules/OrchardCore.Lists/GraphQL/Startup.cs +++ b/src/OrchardCore.Modules/OrchardCore.Lists/GraphQL/Startup.cs @@ -13,6 +13,7 @@ public override void ConfigureServices(IServiceCollection services) { services.AddInputObjectGraphType(); services.AddObjectGraphType(); + services.AddObjectGraphType(); services.AddTransient(); } }