diff --git a/APIs/src/EpiServer.ContentGraph/Api/Querying/FragmentBuilder.cs b/APIs/src/EpiServer.ContentGraph/Api/Querying/FragmentBuilder.cs index a65c7f7f..30777835 100644 --- a/APIs/src/EpiServer.ContentGraph/Api/Querying/FragmentBuilder.cs +++ b/APIs/src/EpiServer.ContentGraph/Api/Querying/FragmentBuilder.cs @@ -67,7 +67,7 @@ public class FragmentBuilder : FragmentBuilder public FragmentBuilder() : base() { } public FragmentBuilder(string name) { - _query.OperationName = name; + base.OperationName(name); } private FragmentBuilder Field(Expression> fieldSelector) { diff --git a/APIs/src/EpiServer.ContentGraph/Api/Querying/TypeQueryBuilder.cs b/APIs/src/EpiServer.ContentGraph/Api/Querying/TypeQueryBuilder.cs index 7df0e5f1..9b68cdc1 100644 --- a/APIs/src/EpiServer.ContentGraph/Api/Querying/TypeQueryBuilder.cs +++ b/APIs/src/EpiServer.ContentGraph/Api/Querying/TypeQueryBuilder.cs @@ -115,6 +115,27 @@ public TypeQueryBuilder Fields(params Expression>[] fieldSele } return this; } + /// + /// Select properties of an IEnumerable of + /// + /// + /// IEnumerable property of + /// Fields of type + /// + public TypeQueryBuilder NestedFields(Expression>> enumSelector, params Expression>[] fieldSelectors ) + { + enumSelector.ValidateNotNullArgument("fieldSelector"); + fieldSelectors.ValidateNotNullArgument("fields"); + var enumPath = enumSelector.GetFieldPath(); + string fields = string.Empty; + foreach (var fieldSelector in fieldSelectors) + { + fields += fields.IsNullOrEmpty() ? fieldSelector.GetFieldPath(): $" {fieldSelector.GetFieldPath()}"; + } + var combinedPath = ConvertNestedFieldToString.ConvertNestedFieldForQuery($"{enumPath}.{fields}"); + Field(combinedPath); + return this; + } [Obsolete("Obsoleted. Use InlineFragment instead")] /// /// Select fields in a subtype. The response of your query may need to convert data type. If then consider to use GetContent method. diff --git a/APIs/src/Templates/Alloy/Controllers/SearchPageController.cs b/APIs/src/Templates/Alloy/Controllers/SearchPageController.cs index 65e3b087..27cb1c2e 100644 --- a/APIs/src/Templates/Alloy/Controllers/SearchPageController.cs +++ b/APIs/src/Templates/Alloy/Controllers/SearchPageController.cs @@ -25,8 +25,9 @@ public ViewResult Index(SearchPage currentPage, string q, string t, string p = " .Skip((int.Parse(p) -1) * 10) .Limit(10) .Fields(x=>x.Name, x=> x.Url) + .NestedFields(x=> x.ExistingLanguages, x=> x.Name) .Total() - .AsType(x=>x.MetaDescription, x=> x.MetaTitle) + .InlineFragment(x=>x.MetaDescription, x=> x.MetaTitle) .Search(q) .FilterForVisitor() .Facet(x=>x.ContentType.FacetFilters(t)) diff --git a/APIs/src/Testing/EpiServer.ContentGraph.UnitTests/GenerateQueryTests.cs b/APIs/src/Testing/EpiServer.ContentGraph.UnitTests/GenerateQueryTests.cs index 2f50767d..db841d7a 100644 --- a/APIs/src/Testing/EpiServer.ContentGraph.UnitTests/GenerateQueryTests.cs +++ b/APIs/src/Testing/EpiServer.ContentGraph.UnitTests/GenerateQueryTests.cs @@ -26,6 +26,7 @@ public void SelectNoneOfFieldsShouldThrowException() } [Fact] + [Category("Select Fields")] public void SelectFields() { string expectedFields = @"{items{Property1 Property2}}"; @@ -35,10 +36,24 @@ public void SelectFields() Assert.NotNull(query.GetQuery()); //check selected fields Assert.Contains(expectedFields, query.GetQuery().Query); - query = query.BuildQueries(); } [Fact] + [Category("Select Fields")] + public void SelectFieldsOfEnumerable() + { + string expectedFields = @"{items{Property1 Property2 NestedObjects{NestedProperty}}}"; + typeQueryBuilder.Fields(x => x.Property1, x => x.Property2); + typeQueryBuilder.NestedFields(x => x.NestedObjects, f => f.NestedProperty); + GraphQueryBuilder query = typeQueryBuilder.ToQuery(); + + Assert.NotNull(query.GetQuery()); + //check selected fields + Assert.Contains(expectedFields, query.GetQuery().Query); + } + + [Fact] + [Category("Select Fields")] public void SelectFieldWithAlias() { string expectedFields = @"{items{property1:Property1 property2:Property2}}"; @@ -52,6 +67,7 @@ public void SelectFieldWithAlias() } [Fact] + [Category("Select Fields")] public void SelectNestedFields() { string expectedFields = @"{items{Property1 Property2 Property3{NestedProperty}}}"; diff --git a/APIs/src/Testing/EpiServer.ContentGraph.UnitTests/QueryTypeObjects/RequestTypeObject.cs b/APIs/src/Testing/EpiServer.ContentGraph.UnitTests/QueryTypeObjects/RequestTypeObject.cs index a96dca3e..585c3965 100644 --- a/APIs/src/Testing/EpiServer.ContentGraph.UnitTests/QueryTypeObjects/RequestTypeObject.cs +++ b/APIs/src/Testing/EpiServer.ContentGraph.UnitTests/QueryTypeObjects/RequestTypeObject.cs @@ -11,5 +11,6 @@ internal class RequestTypeObject public string Property1 { get; set; } public int Property2 { get; set; } public NestedObject Property3 { get; set; } + public IEnumerable NestedObjects { get; set; } } }