diff --git a/backend/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/GraphQLExecutionContext.cs b/backend/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/GraphQLExecutionContext.cs index b6311e0ce..077f70b54 100644 --- a/backend/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/GraphQLExecutionContext.cs +++ b/backend/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/GraphQLExecutionContext.cs @@ -15,6 +15,7 @@ using Squidex.Domain.Apps.Core; using Squidex.Domain.Apps.Entities.Assets; using Squidex.Domain.Apps.Entities.Contents.GraphQL.Types; using Squidex.Domain.Apps.Entities.Contents.Queries; +using Squidex.Infrastructure; using Squidex.Infrastructure.Json.Objects; using Squidex.Infrastructure.Log; @@ -70,37 +71,37 @@ namespace Squidex.Domain.Apps.Entities.Contents.GraphQL return await dataLoader.LoadAsync(id); } - public async Task> GetReferencedAssetsAsync(IJsonValue value) + public Task> GetReferencedAssetsAsync(IJsonValue value) { var ids = ParseIds(value); if (ids == null) { - return EmptyAssets; + return Task.FromResult>(EmptyAssets); } var dataLoader = GetAssetsLoader(); - return await dataLoader.LoadManyAsync(ids); + return LoadManyAsync(dataLoader, ids); } - public async Task> GetReferencedContentsAsync(IJsonValue value) + public Task> GetReferencedContentsAsync(IJsonValue value) { var ids = ParseIds(value); if (ids == null) { - return EmptyContents; + return Task.FromResult>(EmptyContents); } var dataLoader = GetContentsLoader(); - return await dataLoader.LoadManyAsync(ids); + return LoadManyAsync(dataLoader, ids); } private IDataLoader GetAssetsLoader() { - return dataLoaderContextAccessor.Context.GetOrAddBatchLoader("Assets", + return dataLoaderContextAccessor.Context.GetOrAddBatchLoader(nameof(GetAssetsLoader), async batch => { var result = await GetReferencedAssetsAsync(new List(batch)); @@ -111,7 +112,7 @@ namespace Squidex.Domain.Apps.Entities.Contents.GraphQL private IDataLoader GetContentsLoader() { - return dataLoaderContextAccessor.Context.GetOrAddBatchLoader("References", + return dataLoaderContextAccessor.Context.GetOrAddBatchLoader(nameof(GetContentsLoader), async batch => { var result = await GetReferencedContentsAsync(new List(batch)); @@ -120,6 +121,13 @@ namespace Squidex.Domain.Apps.Entities.Contents.GraphQL }); } + private static async Task> LoadManyAsync(IDataLoader dataLoader, ICollection keys) where T : class + { + var contents = await Task.WhenAll(keys.Select(dataLoader.LoadAsync)); + + return contents.NotNull().ToList(); + } + private static ICollection? ParseIds(IJsonValue value) { try diff --git a/backend/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/GraphQLModel.cs b/backend/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/GraphQLModel.cs index cd7912a4e..ab066b73b 100644 --- a/backend/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/GraphQLModel.cs +++ b/backend/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/GraphQLModel.cs @@ -153,7 +153,7 @@ namespace Squidex.Domain.Apps.Entities.Contents.GraphQL public IObjectGraphType GetAssetType() { - return assetType as IObjectGraphType; + return assetType; } public IObjectGraphType GetContentType(Guid schemaId) diff --git a/backend/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/Extensions.cs b/backend/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/Extensions.cs index 39835928a..a36e6ec6f 100644 --- a/backend/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/Extensions.cs +++ b/backend/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/Extensions.cs @@ -7,10 +7,7 @@ using System.Collections.Generic; using System.Linq; -using System.Threading.Tasks; -using GraphQL.DataLoader; using Squidex.Domain.Apps.Core.Schemas; -using Squidex.Infrastructure; using Squidex.Text; namespace Squidex.Domain.Apps.Entities.Contents.GraphQL.Types @@ -47,12 +44,5 @@ namespace Squidex.Domain.Apps.Entities.Contents.GraphQL.Types return value; } - - public static async Task> LoadManyAsync(this IDataLoader dataLoader, ICollection keys) where T : class - { - var contents = await Task.WhenAll(keys.Select(dataLoader.LoadAsync)); - - return contents.NotNull().ToList(); - } } } diff --git a/backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/HandleRules/RuleEventFormatterTests.cs b/backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/HandleRules/RuleEventFormatterTests.cs index 4f3e5bbc7..d7b1bbfca 100644 --- a/backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/HandleRules/RuleEventFormatterTests.cs +++ b/backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/HandleRules/RuleEventFormatterTests.cs @@ -279,5 +279,28 @@ namespace Squidex.Domain.Apps.Core.Operations.HandleRules Assert.Equal("Created", result); } + + [Fact] + public async Task Should_return_json_string_when_array() + { + var @event = new EnrichedContentEvent + { + Data = + new NamedContentData() + .AddField("categories", + new ContentFieldData() + .AddJsonValue(JsonValue.Array("ref1", "ref2", "ref3"))) + }; + + var script = @" +Script(JSON.stringify( +{ + 'categories': event.data.categories.iv +}))"; + + var result = await sut.FormatAsync(script, @event); + + Assert.Equal("{'categories':['ref1','ref2','ref3']}", result?.Replace(" ", string.Empty).Replace("\"", "'")); + } } }