forked from OrchardCMS/OrchardCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from EasyOC/UserPickerField
User picker field
- Loading branch information
Showing
5 changed files
with
96 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
...ardCore.Modules/OrchardCore.ContentFields/GraphQL/Types/UserPickerFieldQueryObjectType.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using GraphQL; | ||
using GraphQL.DataLoader; | ||
using GraphQL.Types; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using OrchardCore.Apis.GraphQL; | ||
using OrchardCore.ContentFields.Fields; | ||
using OrchardCore.ContentManagement; | ||
using OrchardCore.Users.GraphQL; | ||
using OrchardCore.Users.Indexes; | ||
using OrchardCore.Users.Models; | ||
using YesSql; | ||
using YesSql.Services; | ||
|
||
namespace OrchardCore.ContentFields.GraphQL | ||
{ | ||
public class UserPickerFieldQueryObjectType : ObjectGraphType<UserPickerField> | ||
{ | ||
public UserPickerFieldQueryObjectType(UserType userType) | ||
{ | ||
Name = nameof(UserPickerField); | ||
|
||
Field<ListGraphType<StringGraphType>, IEnumerable<string>>("userIds") | ||
.Description("user ids") | ||
.PagingArguments() | ||
.Resolve(x => | ||
{ | ||
return x.Page(x.Source.UserIds); | ||
}); | ||
|
||
Field<ListGraphType<UserType>, IEnumerable<User>>("users") | ||
.Type(new ListGraphType(userType)) | ||
.Description("the user items") | ||
.PagingArguments() | ||
.ResolveAsync(x => | ||
{ | ||
var userLoader = GetOrAddUserProfileByIdDataLoader(x); | ||
return userLoader.LoadAsync(x.Page(x.Source.UserIds)).Then(itemResultSet => | ||
{ | ||
return itemResultSet.SelectMany(x => x); | ||
}); | ||
}); | ||
Field<UserType, User>("user") | ||
.Type(userType) | ||
.Description("the first user") | ||
.ResolveAsync(x => | ||
{ | ||
var userLoader = GetOrAddUserProfileByIdDataLoader(x); | ||
return userLoader.LoadAsync(x.Source.UserIds.FirstOrDefault()).Then(itemResultSet => | ||
{ | ||
return itemResultSet.FirstOrDefault(); | ||
}); | ||
}); | ||
} | ||
|
||
public static IDataLoader<string, IEnumerable<User>> GetOrAddUserProfileByIdDataLoader<T>(IResolveFieldContext<T> context) | ||
{ | ||
IDataLoaderContextAccessor requiredService = context.RequestServices.GetRequiredService<IDataLoaderContextAccessor>(); | ||
var session = context.RequestServices.GetService<ISession>(); | ||
return requiredService.Context.GetOrAddCollectionBatchLoader("GetOrAddUserByIds", async (IEnumerable<string> userIds) => | ||
{ | ||
if (userIds == null || !userIds.Any()) | ||
{ | ||
return null; | ||
} | ||
var users = await session.Query<User, UserIndex>(y => y.UserId.IsIn(userIds)).ListAsync(); | ||
|
||
return users.ToLookup((User k) => k.UserId, (User user) => user); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters