-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathContentItemQuery.cs
More file actions
69 lines (56 loc) · 2.22 KB
/
ContentItemQuery.cs
File metadata and controls
69 lines (56 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using GraphQL;
using GraphQL.Resolvers;
using GraphQL.Types;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using OrchardCore.Apis.GraphQL;
using OrchardCore.ContentManagement.GraphQL.Queries.Types;
namespace OrchardCore.ContentManagement.GraphQL.Queries;
public sealed class ContentItemQuery : ISchemaBuilder
{
internal readonly IStringLocalizer S;
public ContentItemQuery(IStringLocalizer<ContentItemQuery> localizer)
{
S = localizer;
}
public Task<string> GetIdentifierAsync()
=> Task.FromResult(string.Empty);
public Task BuildAsync(ISchema schema)
{
var field = new FieldType
{
Name = "ContentItem",
Description = S["Content items are instances of content types, just like objects are instances of classes."],
Type = typeof(ContentItemInterface),
Arguments = new QueryArguments(
new QueryArgument<NonNullGraphType<StringGraphType>>
{
Name = "contentItemId",
Description = S["Content item id"],
}
),
Resolver = new FuncFieldResolver<ContentItem>(ResolveAsync),
};
field.RequirePermission(GraphQLPermissions.ExecuteGraphQL);
schema.Query.AddField(field);
return Task.CompletedTask;
}
private async ValueTask<ContentItem> ResolveAsync(IResolveFieldContext context)
{
var contentItemId = context.GetArgument<string>("contentItemId");
var contentManager = context.RequestServices.GetService<IContentManager>();
var authorizationService = context.RequestServices.GetService<IAuthorizationService>();
var contentItem = await contentManager.GetAsync(contentItemId);
if (contentItem == null)
{
return null;
}
if (!await authorizationService.AuthorizeAsync(context.User, Contents.CommonPermissions.ViewContent, contentItem))
{
// Return null if the user doesn't have permission to view the content item, so that it doesn't appear in the GraphQL response.
return null;
}
return contentItem;
}
}