Skip to content

Commit

Permalink
Add NestedFields method
Browse files Browse the repository at this point in the history
- Support for selecting properties in IEnumerable property
  • Loading branch information
ManhOptimizely committed Oct 21, 2024
1 parent dbcd1e3 commit 370a12e
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public class FragmentBuilder<T> : FragmentBuilder
public FragmentBuilder() : base() { }
public FragmentBuilder(string name)
{
_query.OperationName = name;
base.OperationName(name);
}
private FragmentBuilder<T> Field(Expression<Func<T, object>> fieldSelector)
{
Expand Down
21 changes: 21 additions & 0 deletions APIs/src/EpiServer.ContentGraph/Api/Querying/TypeQueryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,27 @@ public TypeQueryBuilder<T> Fields(params Expression<Func<T, object>>[] fieldSele
}
return this;
}
/// <summary>
/// Select properties of an IEnumerable of <typeparamref name="TField"/>
/// </summary>
/// <typeparam name="TField"></typeparam>
/// <param name="enumSelector">IEnumerable property of <typeparamref name="T"/></param>
/// <param name="fieldSelectors">Fields of type <typeparamref name="TField"/></param>
/// <returns></returns>
public TypeQueryBuilder<T> NestedFields<TField>(Expression<Func<T, IEnumerable<TField>>> enumSelector, params Expression<Func<TField, object>>[] 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")]
/// <summary>
/// Select fields in a subtype. The response of your query may need to convert data type. If then consider to use GetContent<TOriginal,TExpectedType> method.
Expand Down
3 changes: 2 additions & 1 deletion APIs/src/Templates/Alloy/Controllers/SearchPageController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ProxyModels.ArticlePage>(x=>x.MetaDescription, x=> x.MetaTitle)
.InlineFragment<ProxyModels.ArticlePage>(x=>x.MetaDescription, x=> x.MetaTitle)
.Search(q)
.FilterForVisitor()
.Facet(x=>x.ContentType.FacetFilters(t))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public void SelectNoneOfFieldsShouldThrowException()
}

[Fact]
[Category("Select Fields")]
public void SelectFields()
{
string expectedFields = @"{items{Property1 Property2}}";
Expand All @@ -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}}";
Expand All @@ -52,6 +67,7 @@ public void SelectFieldWithAlias()
}

[Fact]
[Category("Select Fields")]
public void SelectNestedFields()
{
string expectedFields = @"{items{Property1 Property2 Property3{NestedProperty}}}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ internal class RequestTypeObject
public string Property1 { get; set; }
public int Property2 { get; set; }
public NestedObject Property3 { get; set; }
public IEnumerable<NestedObject> NestedObjects { get; set; }
}
}

0 comments on commit 370a12e

Please sign in to comment.