Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 94409ca

Browse files
Fix block list and unknown type handling
1 parent 08e0021 commit 94409ca

File tree

8 files changed

+56
-8
lines changed

8 files changed

+56
-8
lines changed
1.85 MB
Loading[フレーム]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using GraphQL.Types;
2+
using Our.Umbraco.GraphQL.Adapters.Types;
3+
using Umbraco.Cms.Core.Models.Blocks;
4+
5+
namespace Our.Umbraco.GraphQL.Adapters.PublishedContent.Types
6+
{
7+
public class BlockListItemGraphType : ObjectGraphType<BlockListItem>
8+
{
9+
public BlockListItemGraphType()
10+
{
11+
Name = "BlockListItem";
12+
13+
Field<PublishedElementInterfaceGraphType>("content", resolve: ctx => ctx.Source?.Content);
14+
Field<UdiGraphType>("contentUdi", resolve: ctx => ctx.Source?.ContentUdi);
15+
Field<PublishedElementInterfaceGraphType>("settings", resolve: ctx => ctx.Source?.Settings);
16+
Field<UdiGraphType>("settingsUdi", resolve: ctx => ctx.Source?.SettingsUdi);
17+
}
18+
}
19+
}

‎src/Our.Umbraco.GraphQL/Adapters/PublishedContent/Types/PublishedPropertyFieldType.cs‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
using System;
2+
using System.Collections;
3+
using System.Linq;
24
using System.Reflection;
35
using GraphQL;
46
using GraphQL.Resolvers;
57
using GraphQL.Types;
68
using Microsoft.AspNetCore.Http;
79
using Microsoft.AspNetCore.Http.Extensions;
10+
using Newtonsoft.Json;
811
using Newtonsoft.Json.Linq;
912
using Our.Umbraco.GraphQL.Adapters.Types.Resolution;
1013
using Our.Umbraco.GraphQL.Reflection;
1114
using Umbraco.Cms.Core.Models;
15+
using Umbraco.Cms.Core.Models.Blocks;
1216
using Umbraco.Cms.Core.Models.PublishedContent;
1317
using Umbraco.Cms.Core.Routing;
1418
using Umbraco.Cms.Core.Web;
@@ -30,11 +34,14 @@ public PublishedPropertyFieldType(IPublishedContentType contentType, IPropertyTy
3034
unwrappedTypeInfo = typeof(IPublishedContent).GetTypeInfo();
3135
else if (typeof(IPublishedElement).IsAssignableFrom(unwrappedTypeInfo))
3236
unwrappedTypeInfo = typeof(IPublishedElement).GetTypeInfo();
37+
else if (typeof(BlockListItem).IsAssignableFrom(unwrappedTypeInfo))
38+
unwrappedTypeInfo = typeof(BlockListItem).GetTypeInfo();
3339

34-
var propertyGraphType = typeRegistry.Get(unwrappedTypeInfo) ?? typeof(StringGraphType).GetTypeInfo();
40+
var mappedType = typeRegistry.Get(unwrappedTypeInfo);
41+
var propertyGraphType = mappedType ?? typeof(StringGraphType).GetTypeInfo();
3542
// The Grid data type declares its return type as a JToken, but is actually a JObject. The result is that without this check,
3643
// it is cast as an IEnumerable<JProperty> which causes problems when trying to serialize the graph to send to the client
37-
propertyGraphType = propertyGraphType.Wrap(type, propertyType.Mandatory, false, propertyType.PropertyEditorAlias != global::Umbraco.Cms.Core.Constants.PropertyEditors.Aliases.Grid);
44+
propertyGraphType = propertyGraphType.Wrap(type, propertyType.Mandatory, false, propertyType.PropertyEditorAlias != global::Umbraco.Cms.Core.Constants.PropertyEditors.Aliases.Grid,outvarisEnumerable);
3845

3946
if (propertyType.VariesByCulture())
4047
{
@@ -49,7 +56,7 @@ public PublishedPropertyFieldType(IPublishedContentType contentType, IPropertyTy
4956
Description = propertyType.Description;
5057
Resolver = new AsyncFieldResolver<IPublishedElement, object>(async context =>
5158
{
52-
object ResolveProperty() => context.Source.Value(propertyType.Alias, context.GetArgument<string>("culture"), fallback: Fallback.ToLanguage);
59+
object ResolveProperty() => NormalizeResult(mappedType,isEnumerable,context.Source.Value(propertyType.Alias, context.GetArgument<string>("culture"), fallback: Fallback.ToLanguage));
5360
if (umbracoContextFactory == null || publishedRouter == null) return ResolveProperty();
5461

5562
var pc = context.Source as IPublishedContent;
@@ -71,5 +78,16 @@ public PublishedPropertyFieldType(IPublishedContentType contentType, IPropertyTy
7178
}
7279
});
7380
}
81+
82+
private object NormalizeResult(TypeInfo mappedType, bool isEnumerable, object result)
83+
{
84+
if (mappedType != null) return result;
85+
if (result == null) return "null";
86+
87+
if (isEnumerable && result is IEnumerable en) return en.Cast<object>().Select(JsonString);
88+
else return JsonString(result);
89+
}
90+
91+
private string JsonString(object value) => value != null ? JsonConvert.SerializeObject(value, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }) : "null";
7492
}
7593
}

‎src/Our.Umbraco.GraphQL/Adapters/Types/IdGraphType.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public class IdGraphType : global::GraphQL.Types.IdGraphType
77
{
88
public override object ParseValue(object value)
99
{
10+
if (value is Id id) return id;
1011
var parsed = base.ParseValue(value);
1112
return parsed == null ? (Id?)null : new Id(parsed.ToString());
1213
}

‎src/Our.Umbraco.GraphQL/Adapters/Types/Resolution/TypeRegistry.cs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
using GraphQL;
66
using GraphQL.Types;
77
using Microsoft.AspNetCore.Html;
8+
using Our.Umbraco.GraphQL.Adapters.PublishedContent.Types;
89
using Our.Umbraco.GraphQL.Adapters.Types.Relay;
910
using Our.Umbraco.GraphQL.Reflection;
1011
using Our.Umbraco.GraphQL.Types;
1112
using Our.Umbraco.GraphQL.Types.Relay;
13+
using Umbraco.Cms.Core.Models.Blocks;
1214

1315
namespace Our.Umbraco.GraphQL.Adapters.Types.Resolution
1416
{
@@ -39,10 +41,11 @@ public TypeRegistry()
3941
Add<Uri, UriGraphType>();
4042
Add<Id, IdGraphType>();
4143
Add<PageInfo, PageInfoGraphType>();
44+
Add<BlockListItem, BlockListItemGraphType>();
4245
}
4346

4447
public void Add<TType, TGraphType>() where TGraphType : IGraphType =>
45-
_types.Add(typeof(TType).GetTypeInfo(),typeof(TGraphType).GetTypeInfo());
48+
_types[typeof(TType).GetTypeInfo()]=typeof(TGraphType).GetTypeInfo();
4649

4750
public TypeInfo Get(TypeInfo type)
4851
{

‎src/Our.Umbraco.GraphQL/Compose/GraphQLComposer.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public override void Compose(IUmbracoBuilder builder)
6464
builder.Services.AddTransient<PublishedContentInterfaceGraphType>();
6565
builder.Services.AddTransient<PublishedItemTypeGraphType>();
6666
builder.Services.AddTransient<ContentVariationGraphType>();
67+
builder.Services.AddTransient<BlockListItemGraphType>();
6768
builder.Services.AddTransient<UrlModeGraphType>();
6869
builder.Services.AddTransient<UdiGraphType>();
6970
builder.Services.AddTransient<LinkGraphType>();

‎src/Our.Umbraco.GraphQL/Reflection/TypeInfoExtensions.cs‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,24 @@ public static TypeInfo Unwrap(this TypeInfo typeInfo)
3939
return enumerableArgument != null ? enumerableArgument.GetTypeInfo() : typeInfo;
4040
}
4141

42-
public static TypeInfo Wrap(this TypeInfo graphType, TypeInfo typeInfo, bool isNonNull, bool isNonNullItem, bool checkEnumerable = true)
42+
public static TypeInfo Wrap(this TypeInfo graphType, TypeInfo typeInfo, bool isNonNull, bool isNonNullItem)
43+
=> Wrap(graphType, typeInfo, isNonNull, isNonNullItem, true, out _);
44+
45+
public static TypeInfo Wrap(this TypeInfo graphType, TypeInfo typeInfo, bool isNonNull, bool isNonNullItem, bool checkEnumerable, out bool isEnumerable)
4346
{
47+
isEnumerable = false;
48+
4449
if (graphType == null)
4550
return null;
4651

4752
var enumerableArgument = checkEnumerable ? GetEnumerableArgument(typeInfo) : null;
53+
isEnumerable = enumerableArgument != null;
4854

4955
if (typeInfo.IsValueType && typeInfo.IsNullable() == false || enumerableArgument != null &&
5056
(enumerableArgument.IsValueType && enumerableArgument.IsNullable() == false || isNonNullItem))
5157
graphType = typeof(NonNullGraphType<>).MakeGenericType(graphType).GetTypeInfo();
5258

53-
if (enumerableArgument!=null)
59+
if (isEnumerable)
5460
graphType = typeof(ListGraphType<>).MakeGenericType(graphType).GetTypeInfo();
5561

5662
if (isNonNull && typeof(NonNullGraphType).IsAssignableFrom(graphType) == false)

‎src/Our.Umbraco.GraphQL/Web/Middleware/GraphQLMiddleware.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ public async Task Invoke(HttpContext context,
7474
opts.ComplexityConfiguration = options.Complexity;
7575
});
7676

77-
if (request?.OperationName=="IntrospectionQuery"&&result.Errors != null && result.Errors.Count > 0)
77+
if (result.Errors != null && result.Errors.Count > 0)
7878
{
7979
foreach (var error in result.Errors)
8080
{
81-
_logger.LogError(error.GetBaseException(), "Could not introspect schema");
81+
_logger.LogError(error.GetBaseException(), "There was an error"+(error.Path==null?"":" at ["+string.Join(", ",error.Path)+"]"));
8282
}
8383
}
8484

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /