Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding GraphQL content Items property to ListPart #3057

Merged
merged 1 commit into from
Jan 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<ListPart>
{
public ListQueryObjectType(IStringLocalizer<ListQueryObjectType> T)
{
Name = "ListPart";
Description = T["Represents a collection of content items."];

Field<ListGraphType<ContentItemInterface>, IEnumerable<ContentItem>>()
.Name("contentItems")
.Description("the content items")
.Argument<IntGraphType, int>("first", "the first n elements (10 by default)", 0)
.Argument<IntGraphType, int>("skip", "the number of elements to skip", 0)
.ResolveAsync(async g =>
{
var context = (GraphQLContext)g.UserContext;
var session = context.ServiceProvider.GetService<ISession>();

var query = session.Query<ContentItem>()
.With<ContainedPartIndex>(x => x.ListContentItemId == g.Source.ContentItem.ContentItemId)
.With<ContentItemIndex>(x => x.Published)
.OrderByDescending(x => x.CreatedUtc);

// Apply a default limit
var pagedQuery = query.Take(10);

var skip = g.GetArgument<int>("skip");
var first = g.GetArgument<int>("first");

if (skip > 0)
{
pagedQuery = pagedQuery.Skip(skip);
}

if (first > 0)
{
pagedQuery = pagedQuery.Take(first);
}

return await query.ListAsync();
Jetski5822 marked this conversation as resolved.
Show resolved Hide resolved
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public override void ConfigureServices(IServiceCollection services)
{
services.AddInputObjectGraphType<ContainedPart, ContainedInputObjectType>();
services.AddObjectGraphType<ContainedPart, ContainedQueryObjectType>();
services.AddObjectGraphType<ListPart, ListQueryObjectType>();
services.AddTransient<IIndexAliasProvider, ContainedPartIndexAliasProvider>();
}
}
Expand Down