Skip to content

Commit d131e4b

Browse files
committed
Add published content and composition graph types
1 parent 9b6f074 commit d131e4b

9 files changed

+123
-120
lines changed

src/Our.Umbraco.GraphQL/GraphQLAuthenticationExtensions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ public static void SetDoctypeMetadata(this FieldType type, string doctypeAlias)
9494
}
9595
}
9696

97+
public static FieldBuilder<TSourceType, TReturnType> SetDoctypeMetadata<TSourceType, TReturnType>(
98+
this FieldBuilder<TSourceType, TReturnType> builder, string doctypeAlias)
99+
{
100+
SetDoctypeMetadata(builder.FieldType, doctypeAlias);
101+
return builder;
102+
}
103+
97104
public static FieldBuilder<TSourceType, TReturnType> RequirePermission<TSourceType, TReturnType>(
98105
this FieldBuilder<TSourceType, TReturnType> builder, string permission)
99106
{

src/Our.Umbraco.GraphQL/Schema/Conventions.cs

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/Our.Umbraco.GraphQL/Schema/UmbracoSchema.cs

Lines changed: 5 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ public UmbracoSchema(
2727

2828
FieldNameConverter = new DefaultFieldNameConverter();
2929

30-
var resolveName = options.PublishedContentNameResolver;
31-
var documentTypes = CreateGraphTypes(contentTypeService.GetAllContentTypes(), PublishedItemType.Content, resolveName).ToList();
32-
var mediaTypes = CreateGraphTypes(contentTypeService.GetAllMediaTypes(), PublishedItemType.Media, resolveName);
30+
var documentTypes = CreateGraphTypes(contentTypeService.GetAllContentTypes(), PublishedItemType.Content).ToList();
31+
var mediaTypes = CreateGraphTypes(contentTypeService.GetAllMediaTypes(), PublishedItemType.Media).ToList();
3332

3433
//foreach (var documentType in documentTypes.OfType<ComplexGraphType<IPublishedContent>>())
3534
//{
@@ -82,14 +81,8 @@ public UmbracoSchema(
8281

8382
public static IEnumerable<IGraphType> CreateGraphTypes(
8483
IEnumerable<IContentTypeComposition> contentTypes,
85-
PublishedItemType publishedItemType,
86-
Func<IContentTypeBase, string> resolveName = null)
84+
PublishedItemType publishedItemType)
8785
{
88-
if (resolveName == null)
89-
{
90-
resolveName = Conventions.NameResolvers.PascalCase;
91-
}
92-
9386
var interfaceGraphTypes = new Dictionary<string, IInterfaceGraphType>();
9487

9588
//TODO: Whitelist/blacklist content types
@@ -99,49 +92,15 @@ public static IEnumerable<IGraphType> CreateGraphTypes(
9992

10093
foreach (var contentType in compositions)
10194
{
102-
var graphType = new InterfaceGraphType<IPublishedContent>
103-
{
104-
Name = resolveName(contentType),
105-
Description = contentType.Description,
106-
Metadata =
107-
{
108-
["documentTypeAlias"] = contentType.Alias,
109-
}
110-
};
111-
112-
graphType.AddUmbracoContentPropeties(contentType, publishedItemType);
11395

96+
var graphType = new PublishedContentCompositionGraphType(contentType, publishedItemType);
11497
yield return graphType;
11598
interfaceGraphTypes.Add(contentType.Alias, graphType);
11699
}
117100

118101
foreach (var contentType in contentTypeList.Except(compositions))
119102
{
120-
var graphType = new ObjectGraphType<IPublishedContent>
121-
{
122-
Name = resolveName(contentType),
123-
Description = contentType.Description,
124-
IsTypeOf = content => ((IPublishedContent) content).DocumentTypeAlias == contentType.Alias,
125-
Metadata =
126-
{
127-
["documentTypeAlias"] = contentType.Alias,
128-
["allowedAtRoot"] = contentType.AllowedAsRoot,
129-
["allowedChildren"] = contentType.AllowedContentTypes.Select(x => x.Alias).ToArray(),
130-
}
131-
};
132-
133-
graphType.Interface<PublishedContentInterfaceGraphType>();
134-
foreach (var composition in contentType.ContentTypeComposition)
135-
{
136-
if (interfaceGraphTypes.TryGetValue(composition.Alias, out IInterfaceGraphType interfaceType))
137-
{
138-
graphType.AddResolvedInterface(interfaceType);
139-
}
140-
}
141-
142-
graphType.AddUmbracoBuiltInProperties();
143-
graphType.AddUmbracoContentPropeties(contentType, publishedItemType);
144-
103+
var graphType = new PublishedContentGraphType(contentType, publishedItemType, interfaceGraphTypes);
145104
yield return graphType;
146105
}
147106
}

src/Our.Umbraco.GraphQL/Types/ComplexGraphTypeOfIPublishedContentExtensions.cs

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -10,60 +10,6 @@ namespace Our.Umbraco.GraphQL.Types
1010
{
1111
internal static class ComplexGraphTypeOfIPublishedContentExtensions
1212
{
13-
public static ComplexGraphType<IPublishedContent> AddUmbracoBuiltInProperties(this ComplexGraphType<IPublishedContent> graphType)
14-
{
15-
// TODO: set this field name as a reserved property alias
16-
graphType.Field<PublishedContentDataGraphType>("_contentData", "Built in published content data.", resolve: context => context.Source).SetDoctypeMetadata(graphType.GetMetadata<string>("documentTypeAlias"));
17-
18-
return graphType;
19-
}
20-
21-
public static ComplexGraphType<IPublishedContent> AddContentDataProperties(this ComplexGraphType<IPublishedContent> graphType)
22-
{
23-
//TODO: black/whitelist properties
24-
graphType.Field<NonNullGraphType<DateGraphType>>("createDate", "Create date of the content.").SetPermissions(graphType, true);
25-
graphType.Field<NonNullGraphType<StringGraphType>>("creatorName", "Name of the content creator.").SetPermissions(graphType, true);
26-
graphType.Field<NonNullGraphType<StringGraphType>>("documentTypeAlias", "Document type alias of the content.").SetPermissions(graphType, true);
27-
graphType.Field<NonNullGraphType<IdGraphType>>("id", "Unique id of the content.").SetPermissions(graphType, true);
28-
graphType.Field<NonNullGraphType<IntGraphType>>("index", "Index of the content.", resolve: context => context.Source.GetIndex()).SetPermissions(graphType, true);
29-
graphType.Field<NonNullGraphType<IntGraphType>>("level", "Level of the content.").SetPermissions(graphType, true);
30-
graphType.Field<NonNullGraphType<BooleanGraphType>>("isFirst", "Is the content first in the list.", resolve: context => context.Source.IsFirst()).SetPermissions(graphType, true);
31-
graphType.Field<NonNullGraphType<BooleanGraphType>>("isLast", "Is the content last in the list.", resolve: context => context.Source.IsLast()).SetPermissions(graphType, true);
32-
graphType.Field<NonNullGraphType<BooleanGraphType>>("isVisible", "Is the content visible.", resolve: context => context.Source.IsVisible()).SetPermissions(graphType, true);
33-
graphType.Field<NonNullGraphType<StringGraphType>>("name", "Name of the content.").SetPermissions(graphType, true);
34-
graphType.Field<PublishedContentInterfaceGraphType>("parent", "Parent of the content.").SetPermissions(graphType, true);
35-
graphType.Field<NonNullGraphType<IntGraphType>>("sortOrder", "SortOrder of the content.").SetPermissions(graphType, true);
36-
graphType.Field<NonNullGraphType<DateGraphType>>("updateDate", "Update date of the content.").SetPermissions(graphType, true);
37-
graphType.Field<NonNullGraphType<StringGraphType>>("url", "Url of the content.").SetPermissions(graphType, true);
38-
graphType.Field<NonNullGraphType<StringGraphType>>("urlAbsolute", "Absolute url of the content.", resolve: context => context.Source.UrlAbsolute()).SetPermissions(graphType, true);
39-
graphType.Field<NonNullGraphType<StringGraphType>>("writerName", "Name of the content writer.").SetPermissions(graphType, true);
40-
41-
graphType.FilteredConnection<PublishedContentInterfaceGraphType, IPublishedContent>()
42-
.Name("ancestors")
43-
.Description("Ancestors of the content.")
44-
.Argument<BooleanGraphType>("includeSelf", "include self in list")
45-
.Bidirectional()
46-
.Resolve(context =>
47-
(context.GetArgument<bool?>("includeSelf") == true
48-
? context.Source.AncestorsOrSelf()
49-
: context.Source.Ancestors()).Filter(context).ToConnection(context)
50-
);
51-
52-
graphType.FilteredConnection<PublishedContentInterfaceGraphType, IPublishedContent>()
53-
.Name("siblings")
54-
.Description("Siblings of the content.")
55-
.Bidirectional()
56-
.Resolve(context => context.Source.Siblings().Filter(context).ToConnection(context));
57-
58-
graphType.FilteredConnection<PublishedContentInterfaceGraphType, IPublishedContent>()
59-
.Name("children")
60-
.Description("Children of the content.")
61-
.Bidirectional()
62-
.Resolve(context => context.Source.Children.Filter(context).ToConnection(context));
63-
64-
return graphType;
65-
}
66-
6713
public static ComplexGraphType<IPublishedContent> AddUmbracoContentPropeties(
6814
this ComplexGraphType<IPublishedContent> graphType,
6915
IContentTypeComposition contentType,
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using GraphQL;
2+
using System.Collections.Generic;
3+
using Umbraco.Core.Models;
4+
5+
namespace Our.Umbraco.GraphQL.Types
6+
{
7+
public class PublishedContentCompositionGraphType : PublishedContentInterfaceGraphType
8+
{
9+
public PublishedContentCompositionGraphType(IContentTypeComposition contentType, PublishedItemType itemType)
10+
{
11+
Name = contentType.Alias.ToPascalCase();
12+
Description = contentType.Description;
13+
Metadata = new Dictionary<string, object>
14+
{
15+
["contentTypeAlias"] = contentType.Alias,
16+
};
17+
18+
this.AddUmbracoContentPropeties(contentType, itemType);
19+
}
20+
}
21+
}

src/Our.Umbraco.GraphQL/Types/PublishedContentDataGraphType.cs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using GraphQL.Types;
22
using Umbraco.Core.Models;
3+
using Umbraco.Web;
34

45
namespace Our.Umbraco.GraphQL.Types
56
{
@@ -9,7 +10,46 @@ public PublishedContentDataGraphType()
910
{
1011
Name = "PublishedContentData";
1112

12-
this.AddContentDataProperties();
13+
//TODO: black/whitelist properties
14+
Field<NonNullGraphType<DateGraphType>>("createDate", "Create date of the content.").SetPermissions(this, true);
15+
Field<NonNullGraphType<StringGraphType>>("creatorName", "Name of the content creator.").SetPermissions(this, true);
16+
Field<NonNullGraphType<StringGraphType>>("documentTypeAlias", "Document type alias of the content.").SetPermissions(this, true);
17+
Field<NonNullGraphType<IdGraphType>>("id", "Unique id of the content.").SetPermissions(this, true);
18+
Field<NonNullGraphType<IntGraphType>>("index", "Index of the content.", resolve: context => context.Source.GetIndex()).SetPermissions(this, true);
19+
Field<NonNullGraphType<IntGraphType>>("level", "Level of the content.").SetPermissions(this, true);
20+
Field<NonNullGraphType<BooleanGraphType>>("isFirst", "Is the content first in the list.", resolve: context => context.Source.IsFirst()).SetPermissions(this, true);
21+
Field<NonNullGraphType<BooleanGraphType>>("isLast", "Is the content last in the list.", resolve: context => context.Source.IsLast()).SetPermissions(this, true);
22+
Field<NonNullGraphType<BooleanGraphType>>("isVisible", "Is the content visible.", resolve: context => context.Source.IsVisible()).SetPermissions(this, true);
23+
Field<NonNullGraphType<StringGraphType>>("name", "Name of the content.").SetPermissions(this, true);
24+
Field<PublishedContentInterfaceGraphType>("parent", "Parent of the content.").SetPermissions(this, true);
25+
Field<NonNullGraphType<IntGraphType>>("sortOrder", "SortOrder of the content.").SetPermissions(this, true);
26+
Field<NonNullGraphType<DateGraphType>>("updateDate", "Update date of the content.").SetPermissions(this, true);
27+
Field<NonNullGraphType<StringGraphType>>("url", "Url of the content.").SetPermissions(this, true);
28+
Field<NonNullGraphType<StringGraphType>>("urlAbsolute", "Absolute url of the content.", resolve: context => context.Source.UrlAbsolute()).SetPermissions(this, true);
29+
Field<NonNullGraphType<StringGraphType>>("writerName", "Name of the content writer.").SetPermissions(this, true);
30+
31+
this.FilteredConnection<PublishedContentInterfaceGraphType, IPublishedContent>()
32+
.Name("ancestors")
33+
.Description("Ancestors of the content.")
34+
.Argument<BooleanGraphType>("includeSelf", "include self in list")
35+
.Bidirectional()
36+
.Resolve(context =>
37+
(context.GetArgument<bool?>("includeSelf") == true
38+
? context.Source.AncestorsOrSelf()
39+
: context.Source.Ancestors()).Filter(context).ToConnection(context)
40+
);
41+
42+
this.FilteredConnection<PublishedContentInterfaceGraphType, IPublishedContent>()
43+
.Name("siblings")
44+
.Description("Siblings of the content.")
45+
.Bidirectional()
46+
.Resolve(context => context.Source.Siblings().Filter(context).ToConnection(context));
47+
48+
this.FilteredConnection<PublishedContentInterfaceGraphType, IPublishedContent>()
49+
.Name("children")
50+
.Description("Children of the content.")
51+
.Bidirectional()
52+
.Resolve(context => context.Source.Children.Filter(context).ToConnection(context));
1353
}
1454
}
1555
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using GraphQL;
2+
using GraphQL.Types;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using Umbraco.Core.Models;
6+
7+
namespace Our.Umbraco.GraphQL.Types
8+
{
9+
public class PublishedContentGraphType : ObjectGraphType<IPublishedContent>
10+
{
11+
public PublishedContentGraphType(IContentTypeComposition contentType, PublishedItemType itemType, IDictionary<string, IInterfaceGraphType> interfaceGraphTypes)
12+
{
13+
Name = contentType.Alias.ToPascalCase();
14+
Description = contentType.Description;
15+
IsTypeOf = content => ((IPublishedContent)content).ContentType.Alias == contentType.Alias;
16+
Metadata = new Dictionary<string, object>
17+
{
18+
["contentTypeAlias"] = contentType.Alias,
19+
["allowedAtRoot"] = contentType.AllowedAsRoot,
20+
["allowedChildren"] = contentType.AllowedContentTypes.Select(x => x.Alias).ToArray(),
21+
};
22+
23+
Interface<PublishedContentInterfaceGraphType>();
24+
25+
foreach (IContentTypeComposition composition in contentType.ContentTypeComposition)
26+
{
27+
if (interfaceGraphTypes.TryGetValue(composition.Alias, out IInterfaceGraphType interfaceType))
28+
{
29+
AddResolvedInterface(interfaceType);
30+
}
31+
}
32+
33+
// TODO: set this field name as a reserved property alias
34+
Field<NonNullGraphType<PublishedContentDataGraphType>>()
35+
.Name("_contentData")
36+
.Description("Built in published content data.")
37+
.Resolve(context => context.Source)
38+
.SetDoctypeMetadata(GetMetadata<string>("documentTypeAlias"));
39+
40+
this.AddUmbracoContentPropeties(contentType, itemType);
41+
}
42+
}
43+
}

src/Our.Umbraco.GraphQL/Types/PublishedContentInterfaceGraphType.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ public PublishedContentInterfaceGraphType()
99
{
1010
Name = "PublishedContent";
1111

12-
this.AddUmbracoBuiltInProperties();
12+
// TODO: set this field name as a reserved property alias
13+
Field<NonNullGraphType<PublishedContentDataGraphType>>()
14+
.Name("_contentData")
15+
.Description("Built in published content data.")
16+
.Resolve(context => context.Source)
17+
.SetDoctypeMetadata(GetMetadata<string>("documentTypeAlias"));
1318
}
1419
}
1520
}

src/Our.Umbraco.GraphQL/Web/GraphQLServerOptions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,5 @@ public GraphQLServerOptions(ICorsPolicyProvider corsPolicyProvder)
5151
//public bool EnableLogin { get; set; } = false;
5252
public bool EnableMetrics { get; set; } = false;
5353
//public bool ExposeSchema { get; set; } = true;
54-
public Func<IContentTypeBase, string> PublishedContentNameResolver { get; set; } = Conventions.NameResolvers.PascalCase;
5554
}
5655
}

0 commit comments

Comments
 (0)