Skip to content

Commit

Permalink
Move UserType to OC.Users.Core
Browse files Browse the repository at this point in the history
  • Loading branch information
hishamco committed Apr 30, 2024
1 parent 7e25b3e commit 0f1de97
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
<ProjectReference Include="..\..\OrchardCore\OrchardCore.Shortcodes.Abstractions\OrchardCore.Shortcodes.Abstractions.csproj" />
<ProjectReference Include="..\..\OrchardCore\OrchardCore.ResourceManagement\OrchardCore.ResourceManagement.csproj" />
<ProjectReference Include="..\..\OrchardCore\OrchardCore.Users.Core\OrchardCore.Users.Core.csproj" />
<ProjectReference Include="..\OrchardCore.Users\OrchardCore.Users.csproj" />
</ItemGroup>

</Project>
53 changes: 53 additions & 0 deletions src/OrchardCore/OrchardCore.Users.Core/GraphQL/UserType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System.Linq;
using GraphQL.Types;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
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 a 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."]);
}

public override void Initialize(ISchema schema)
{
// Add custom user settings by reusing previously registered content types with the
// stereotype "CustomUserSettings".
foreach (var contentItemType in schema.AdditionalTypeInstances.Where(t => t.Metadata.TryGetValue("Stereotype", out var stereotype) && stereotype as string == "CustomUserSettings"))
{
Field(contentItemType.Name, contentItemType)
.Description(S["Custom user settings of {0}.", contentItemType.Name])
.ResolveAsync(static 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;
});
}

base.Initialize(schema);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\OrchardCore.ContentManagement.GraphQL\OrchardCore.ContentManagement.GraphQL.csproj" />
<ProjectReference Include="..\OrchardCore.Data.YesSql\OrchardCore.Data.YesSql.csproj" />
<ProjectReference Include="..\OrchardCore.DisplayManagement.Abstractions\OrchardCore.DisplayManagement.Abstractions.csproj" />
<ProjectReference Include="..\OrchardCore.Infrastructure.Abstractions\OrchardCore.Infrastructure.Abstractions.csproj" />
Expand Down

0 comments on commit 0f1de97

Please sign in to comment.