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.
Implemented GraphQL query for retrieving current user data and its cu…
…stom settings. (OrchardCMS#15215)
- Loading branch information
Showing
4 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
src/OrchardCore.Modules/OrchardCore.Users/GraphQL/CurrentUserQuery.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,81 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using GraphQL.Resolvers; | ||
using GraphQL.Types; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Localization; | ||
using Microsoft.Extensions.Options; | ||
using OrchardCore.Apis.GraphQL; | ||
using OrchardCore.ContentManagement.GraphQL.Options; | ||
using OrchardCore.ContentManagement.Metadata; | ||
using OrchardCore.ContentManagement.Metadata.Models; | ||
using OrchardCore.Users.Models; | ||
using OrchardCore.Users.Services; | ||
|
||
namespace OrchardCore.Users.GraphQL; | ||
|
||
/// <summary> | ||
/// Registers the current user including its custom user settings as a query. | ||
/// </summary> | ||
internal class CurrentUserQuery : ISchemaBuilder | ||
{ | ||
private readonly IHttpContextAccessor _httpContextAccessor; | ||
private readonly IOptions<GraphQLContentOptions> _contentOptionsAccessor; | ||
protected readonly IStringLocalizer S; | ||
|
||
public CurrentUserQuery( | ||
IHttpContextAccessor httpContextAccessor, | ||
IOptions<GraphQLContentOptions> contentOptionsAccessor, | ||
IStringLocalizer<CurrentUserQuery> localizer) | ||
{ | ||
_httpContextAccessor = httpContextAccessor; | ||
_contentOptionsAccessor = contentOptionsAccessor; | ||
S = localizer; | ||
} | ||
|
||
public async Task BuildAsync(ISchema schema) | ||
{ | ||
// Build a user type that includes all custom user settings. | ||
var serviceProvider = _httpContextAccessor.HttpContext.RequestServices; | ||
var contentDefinitionManager = serviceProvider.GetRequiredService<IContentDefinitionManager>(); | ||
var contentTypes = await contentDefinitionManager.ListTypeDefinitionsAsync(); | ||
|
||
var userType = serviceProvider.GetRequiredService<UserType>(); | ||
|
||
// Note: The content types are already added to GraphQL by the ContentTypeQuery. Just add them to the user type. | ||
foreach (var typeDefinition in contentTypes.Where(t => t.StereotypeEquals("CustomUserSettings"))) | ||
{ | ||
// Skip hidden types | ||
if (_contentOptionsAccessor.Value.ShouldHide(typeDefinition)) | ||
{ | ||
continue; | ||
} | ||
|
||
userType.AddField(schema, typeDefinition); | ||
} | ||
|
||
var currentUserField = new FieldType | ||
{ | ||
Name = "me", | ||
Description = S["Gets the currently authenticated user."], | ||
ResolvedType = userType, | ||
Resolver = new AsyncFieldResolver<User>(async context => | ||
{ | ||
var userService = context.RequestServices!.GetRequiredService<IUserService>(); | ||
var user = await userService.GetAuthenticatedUserAsync(((GraphQLUserContext)context.UserContext).User); | ||
return user as User; | ||
}), | ||
}; | ||
|
||
schema.Query.AddField(currentUserField); | ||
} | ||
|
||
public Task<string> GetIdentifierAsync() | ||
{ | ||
var contentDefinitionManager = _httpContextAccessor.HttpContext!.RequestServices.GetRequiredService<IContentDefinitionManager>(); | ||
return contentDefinitionManager.GetIdentifierAsync(); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/OrchardCore.Modules/OrchardCore.Users/GraphQL/Startup.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,15 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using OrchardCore.Apis.GraphQL; | ||
using OrchardCore.Modules; | ||
|
||
namespace OrchardCore.Users.GraphQL; | ||
|
||
[RequireFeatures("OrchardCore.Apis.GraphQL", "OrchardCore.Contents")] | ||
public class Startup : StartupBase | ||
{ | ||
public override void ConfigureServices(IServiceCollection services) | ||
{ | ||
services.AddSingleton<ISchemaBuilder, CurrentUserQuery>(); | ||
services.AddTransient<UserType>(); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/OrchardCore.Modules/OrchardCore.Users/GraphQL/UserType.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,54 @@ | ||
using System; | ||
using System.Linq; | ||
using GraphQL.Types; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Localization; | ||
using OrchardCore.ContentManagement.Metadata.Models; | ||
using OrchardCore.Users.Models; | ||
using OrchardCore.Users.Services; | ||
|
||
namespace OrchardCore.Users.GraphQL; | ||
|
||
public class UserType : ObjectGraphType<User> | ||
{ | ||
protected readonly IStringLocalizer<UserType> S; | ||
|
||
public UserType(IStringLocalizer<UserType> localizer) | ||
{ | ||
S = localizer; | ||
|
||
Name = "User"; | ||
Description = S["Represents the currently authenticated user."]; | ||
|
||
Field(u => u.UserId).Description(S["The id of the user."]); | ||
Field(u => u.UserName).Description(S["The name of the user."]); | ||
Field(u => u.Email, nullable: true).Description(S["The email of the user."]); | ||
Field(u => u.PhoneNumber, nullable: true).Description(S["The phone number of the user."]); | ||
} | ||
|
||
// Adds a custom user settings field | ||
internal void AddField(ISchema schema, ContentTypeDefinition typeDefinition) | ||
{ | ||
var contentItemType = schema.AdditionalTypeInstances.SingleOrDefault(t => t.Name == typeDefinition.Name); | ||
if (contentItemType == null) | ||
{ | ||
// This error would indicate that this graph type is build too early. | ||
throw new InvalidOperationException("ContentTypeDefinition has not been registered in GraphQL"); | ||
} | ||
|
||
this.FieldAsync(typeDefinition.Name, contentItemType, S["Custom user settings of {0}.", typeDefinition.DisplayName], resolve: async context => | ||
{ | ||
// We don't want to create an empty content item if it does not exist. | ||
if (context.Source is User user && | ||
user.Properties.ContainsKey(context.FieldDefinition.ResolvedType.Name)) | ||
{ | ||
var customUserSettingsService = context.RequestServices!.GetRequiredService<CustomUserSettingsService>(); | ||
var settingsType = await customUserSettingsService.GetSettingsTypeAsync(context.FieldDefinition.ResolvedType.Name); | ||
return await customUserSettingsService.GetSettingsAsync(user, settingsType); | ||
} | ||
return null; | ||
}); | ||
} | ||
} |
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