From 0b517038d88ed5355719d0543c94dad9f1918fcd Mon Sep 17 00:00:00 2001 From: Manh Nguyen Date: Thu, 14 Nov 2024 10:05:58 +0700 Subject: [PATCH] Refactor for common methods --- .../Api/Querying/BaseTypeQueryBuilder.cs | 54 ++++++++++++++----- .../Api/Querying/FragmentBuilder.cs | 43 +++++++-------- .../Api/Querying/GraphQueryBuilder.cs | 17 +++--- .../Api/Querying/IQuery.cs | 2 +- .../Api/Querying/SubTypeQueryBuilder.cs | 6 +-- .../Api/Querying/TypeQueryBuilder.cs | 44 +++------------ .../Extensions/TypeSearchExtension.cs | 10 ++-- .../GenerateFragmentTests.cs | 10 ++-- 8 files changed, 93 insertions(+), 93 deletions(-) diff --git a/APIs/src/EpiServer.ContentGraph/Api/Querying/BaseTypeQueryBuilder.cs b/APIs/src/EpiServer.ContentGraph/Api/Querying/BaseTypeQueryBuilder.cs index 6c6b395b..79f3af4d 100644 --- a/APIs/src/EpiServer.ContentGraph/Api/Querying/BaseTypeQueryBuilder.cs +++ b/APIs/src/EpiServer.ContentGraph/Api/Querying/BaseTypeQueryBuilder.cs @@ -1,7 +1,9 @@ using EPiServer.ContentGraph.Helpers; +using EPiServer.ContentGraph.Helpers.Reflection; using EPiServer.ContentGraph.Helpers.Text; using GraphQL.Transport; using System; +using System.Linq.Expressions; namespace EPiServer.ContentGraph.Api.Querying { @@ -157,7 +159,42 @@ public virtual BaseTypeQueryBuilder Children(ITypeQueryBuilder children) return this; } - public virtual BaseTypeQueryBuilder AddFragments(params FragmentBuilder[] fragments) + protected virtual void AppendItem(string item) + { + if (!item.IsNullOrEmpty()) + { + graphObject.SelectItems.Append(graphObject.SelectItems.Length > 0 ? $" {item}": item); + } + } + } + + public class BaseTypeQueryBuilder : BaseTypeQueryBuilder + { + public BaseTypeQueryBuilder():base() { } + public BaseTypeQueryBuilder(GraphQLRequest query): base(query) { } + /// + /// Select properties of an IEnumerable of + /// + /// + /// IEnumerable property of + /// Fields of type + /// + public virtual BaseTypeQueryBuilder 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; + } + + public virtual BaseTypeQueryBuilder AddFragments(params IFragmentBuilder[] fragments) { fragments.ValidateNotNullArgument("fragments"); foreach (var fragment in fragments) @@ -166,12 +203,12 @@ public virtual BaseTypeQueryBuilder AddFragments(params FragmentBuilder[] fragme } return this; } - protected virtual BaseTypeQueryBuilder AddFragment(FragmentBuilder fragment) + protected virtual BaseTypeQueryBuilder AddFragment(IFragmentBuilder fragment) { AddFragment(null, fragment); return this; } - public virtual BaseTypeQueryBuilder AddFragment(string fieldPath, FragmentBuilder fragment) + public virtual BaseTypeQueryBuilder AddFragment(string fieldPath, IFragmentBuilder fragment) { fragment.ValidateNotNullArgument("fragment"); string propName; @@ -200,11 +237,11 @@ public virtual BaseTypeQueryBuilder AddFragment(string fieldPath, FragmentBuilde } return this; } - private IEnumerable GetAllChildren(FragmentBuilder fragment) + private IEnumerable GetAllChildren(IFragmentBuilder fragment) { if (fragment.HasChildren) { - foreach (var child in fragment.ChildrenFragments) + foreach (var child in fragment.GetChildren()) { yield return child; if (child.HasChildren) @@ -218,12 +255,5 @@ private IEnumerable GetAllChildren(FragmentBuilder fragment) } } - protected virtual void AppendItem(string item) - { - if (!item.IsNullOrEmpty()) - { - graphObject.SelectItems.Append(graphObject.SelectItems.Length > 0 ? $" {item}": item); - } - } } } diff --git a/APIs/src/EpiServer.ContentGraph/Api/Querying/FragmentBuilder.cs b/APIs/src/EpiServer.ContentGraph/Api/Querying/FragmentBuilder.cs index 6468533e..2100eaf1 100644 --- a/APIs/src/EpiServer.ContentGraph/Api/Querying/FragmentBuilder.cs +++ b/APIs/src/EpiServer.ContentGraph/Api/Querying/FragmentBuilder.cs @@ -10,6 +10,13 @@ namespace EPiServer.ContentGraph.Api.Querying { + public interface IFragmentBuilder + { + public string GetName(); + public GraphQLRequest GetQuery(); + public bool HasChildren { get; } + public IEnumerable GetChildren(); + } public class Recursion { public string FieldName { get; set; } @@ -25,14 +32,13 @@ public override string ToString() return ConvertNestedFieldToString.ConvertNestedFieldForQuery($"{FieldName} @recursive(depth:{RecursiveDepth.Value})"); } } - } - public class FragmentBuilder : BaseTypeQueryBuilder + } + public class FragmentBuilder : BaseTypeQueryBuilder, IFragmentBuilder { - private List _childrenFragments; - public IEnumerable ChildrenFragments => _childrenFragments; + private List _childrenFragments; + public IEnumerable GetChildren() => _childrenFragments; public bool HasChildren => _childrenFragments != null && _childrenFragments.Any(); - public FragmentBuilder() : base() - { + public FragmentBuilder() : base() { _query.OperationName = "sampleFragment"; } public void OperationName(string name) @@ -43,43 +49,38 @@ public void OperationName(string name) _query.OperationName = name; } } + public FragmentBuilder(string name) + { + OperationName(name); + } public string GetName() { return _query.OperationName; } - public override FragmentBuilder AddFragments(params FragmentBuilder[] fragments) + public override FragmentBuilder AddFragments(params IFragmentBuilder[] fragments) { if (fragments.IsNotNull() && fragments.Length > 0) { if (_childrenFragments.IsNull()) { - _childrenFragments = new List(); + _childrenFragments = new List(); } base.AddFragments(fragments); _childrenFragments.AddRange(fragments); } return this; } - public FragmentBuilder AddFragment(string path, FragmentBuilder fragment) + public override FragmentBuilder AddFragment(string path, IFragmentBuilder fragment) { if (_childrenFragments.IsNull()) { - _childrenFragments = new List(); + _childrenFragments = new List(); } base.AddFragment(path, fragment); _childrenFragments.Add(fragment); return this; } - } - - public class FragmentBuilder : FragmentBuilder - { - public FragmentBuilder() : base() { } - public FragmentBuilder(string name) - { - base.OperationName(name); - } private FragmentBuilder Field(Expression> fieldSelector) { fieldSelector.ValidateNotNullArgument("fieldSelector"); @@ -112,7 +113,7 @@ public FragmentBuilder Children(TypeQueryBuilder childr base.Children(children); return this; } - private FragmentBuilder Recursive(params Recursion[] recursives) where TSub : T + private FragmentBuilder Recursive(params Recursion[] recursives) where TSub : T { recursives.ValidateNotNullArgument("recursives"); if (!recursives.Any()) throw new ArgumentException("recursives can not be empty"); @@ -206,7 +207,7 @@ public FragmentBuilder AddFragment(Expression> fieldSel fragment.ValidateNotNullArgument(nameof(fragment)); var fieldPath = fieldSelector.GetFieldPath(); - base.AddFragment(fieldPath, fragment); + AddFragment(fieldPath, fragment); return this; } public override GraphQLRequest GetQuery() diff --git a/APIs/src/EpiServer.ContentGraph/Api/Querying/GraphQueryBuilder.cs b/APIs/src/EpiServer.ContentGraph/Api/Querying/GraphQueryBuilder.cs index 8577fb23..69539730 100644 --- a/APIs/src/EpiServer.ContentGraph/Api/Querying/GraphQueryBuilder.cs +++ b/APIs/src/EpiServer.ContentGraph/Api/Querying/GraphQueryBuilder.cs @@ -27,7 +27,7 @@ public class GraphQueryBuilder : IQuery private readonly OptiGraphOptions _optiGraphOptions; private const string RequestMethod = "POST"; private const string UnCachedPath = "?cache=false"; - private Dictionary _fragmentBuilders; + private Dictionary _fragmentBuilders; private readonly List typeQueries = new List(); public GraphQueryBuilder() { @@ -92,18 +92,15 @@ public TypeQueryBuilder ForType(TypeQueryBuilder typeQueryBuilder) typeQueryBuilder.Parent = this; return typeQueryBuilder; } - public void AddFragment(FragmentBuilder fragmentBuilder) + public void AddFragment(IFragmentBuilder fragmentBuilder) { if (_fragmentBuilders == null) { - _fragmentBuilders = new Dictionary(); - } - if (!_fragmentBuilders.TryAdd(fragmentBuilder.GetName(), fragmentBuilder)) - { - throw new ArgumentException($"Fragment [{fragmentBuilder.GetName()}] had added already."); + _fragmentBuilders = new Dictionary(); } + _fragmentBuilders.TryAdd(fragmentBuilder.GetName(), fragmentBuilder); } - public IEnumerable GetFragments() + public IEnumerable GetFragments() { return _fragmentBuilders?.Values; } @@ -139,12 +136,12 @@ private string GetHmacHeader(byte[] requestBody) DefaultHmacDeclarationFactory hmacDeclarationFactory = new DefaultHmacDeclarationFactory(new Sha256HmacAlgorithm(Convert.FromBase64String(_optiGraphOptions.Secret))); HmacMessage hmacMessage = GetHmacMessage(requestBody); - HmacDeclaration? hmacDeclaration = hmacDeclarationFactory.Create(hmacMessage); + HmacDeclaration hmacDeclaration = hmacDeclarationFactory.Create(hmacMessage); return $"{hmacDeclaration}"; } private HmacMessage GetHmacMessage(byte[] requestBody) { - DefaultHmacMessageBuilder? messageBuilder = new DefaultHmacMessageBuilder() + DefaultHmacMessageBuilder messageBuilder = new DefaultHmacMessageBuilder() .AddApplicationKey(_optiGraphOptions.AppKey) .AddTarget(new Uri(GetServiceUrl()).PathAndQuery) .AddMethod(RequestMethod) diff --git a/APIs/src/EpiServer.ContentGraph/Api/Querying/IQuery.cs b/APIs/src/EpiServer.ContentGraph/Api/Querying/IQuery.cs index beea4b81..0bb18b13 100644 --- a/APIs/src/EpiServer.ContentGraph/Api/Querying/IQuery.cs +++ b/APIs/src/EpiServer.ContentGraph/Api/Querying/IQuery.cs @@ -7,7 +7,7 @@ public interface IQuery { public Task> GetResultAsync(); public Task GetResultAsync(); - internal void AddFragment(FragmentBuilder fragmentBuilder); + internal void AddFragment(IFragmentBuilder fragmentBuilder); internal void AddQuery(string typeQuery); } } diff --git a/APIs/src/EpiServer.ContentGraph/Api/Querying/SubTypeQueryBuilder.cs b/APIs/src/EpiServer.ContentGraph/Api/Querying/SubTypeQueryBuilder.cs index b9c1266c..447d2e3c 100644 --- a/APIs/src/EpiServer.ContentGraph/Api/Querying/SubTypeQueryBuilder.cs +++ b/APIs/src/EpiServer.ContentGraph/Api/Querying/SubTypeQueryBuilder.cs @@ -6,7 +6,7 @@ namespace EPiServer.ContentGraph.Api.Querying { - public class SubTypeQueryBuilder : BaseTypeQueryBuilder + public class SubTypeQueryBuilder : BaseTypeQueryBuilder { public SubTypeQueryBuilder():base() { @@ -68,12 +68,12 @@ public SubTypeQueryBuilder Children(TypeQueryBuilder ch base.Children(children); return this; } - public override SubTypeQueryBuilder AddFragments(params FragmentBuilder[] fragments) + public override SubTypeQueryBuilder AddFragments(params IFragmentBuilder[] fragments) { base.AddFragments(fragments); return this; } - protected override SubTypeQueryBuilder AddFragment(FragmentBuilder fragment) + protected override SubTypeQueryBuilder AddFragment(IFragmentBuilder fragment) { base.AddFragment(fragment); return this; diff --git a/APIs/src/EpiServer.ContentGraph/Api/Querying/TypeQueryBuilder.cs b/APIs/src/EpiServer.ContentGraph/Api/Querying/TypeQueryBuilder.cs index 8c18e334..aad84456 100644 --- a/APIs/src/EpiServer.ContentGraph/Api/Querying/TypeQueryBuilder.cs +++ b/APIs/src/EpiServer.ContentGraph/Api/Querying/TypeQueryBuilder.cs @@ -12,9 +12,9 @@ namespace EPiServer.ContentGraph.Api.Querying { - public partial class TypeQueryBuilder : TypeQueryBuilder + public partial class TypeQueryBuilder : BaseTypeQueryBuilder { - private static IEnumerable? _filters; + private static IEnumerable _filters; public TypeQueryBuilder(GraphQLRequest request) : base(request) { } @@ -52,23 +52,23 @@ public TypeQueryBuilder Children(TypeQueryBuilder child return this; } [Obsolete("Use AddFragments method instead")] - public TypeQueryBuilder Fragments(params FragmentBuilder[] fragments) + public TypeQueryBuilder Fragments(params IFragmentBuilder[] fragments) { base.AddFragments(fragments); return this; } - public override TypeQueryBuilder AddFragments(params FragmentBuilder[] fragments) + public override TypeQueryBuilder AddFragments(params IFragmentBuilder[] fragments) { base.AddFragments(fragments); return this; } [Obsolete("Use AddFragment method instead")] - public TypeQueryBuilder Fragment(FragmentBuilder fragment) + public TypeQueryBuilder Fragment(IFragmentBuilder fragment) { base.AddFragment(fragment); return this; } - protected override TypeQueryBuilder AddFragment(FragmentBuilder fragment) + protected override TypeQueryBuilder AddFragment(IFragmentBuilder fragment) { base.AddFragment(fragment); return this; @@ -125,26 +125,9 @@ 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 ) + public override 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; + return (TypeQueryBuilder)base.NestedFields(enumSelector, fieldSelectors); } [Obsolete("Obsoleted. Use InlineFragment instead")] /// @@ -896,15 +879,4 @@ public override GraphQueryBuilder ToQuery() return this.Parent != null ? (GraphQueryBuilder)this.Parent : new GraphQueryBuilder(_query, this); } } - - public class TypeQueryBuilder : BaseTypeQueryBuilder - { - public TypeQueryBuilder(GraphQLRequest request) : base(request) - { - } - public TypeQueryBuilder() : base() - { - } - - } } diff --git a/APIs/src/EpiServer.ContentGraph/Extensions/TypeSearchExtension.cs b/APIs/src/EpiServer.ContentGraph/Extensions/TypeSearchExtension.cs index c95302fe..07585160 100644 --- a/APIs/src/EpiServer.ContentGraph/Extensions/TypeSearchExtension.cs +++ b/APIs/src/EpiServer.ContentGraph/Extensions/TypeSearchExtension.cs @@ -100,15 +100,15 @@ public static GraphQueryBuilder EndType(this TypeQueryBuilder typeQueryBui #endregion #region Get results - public static async Task> GetResultAsync(this TypeQueryBuilder typeQueryBuilder) + public static async Task> GetResultAsync(this TypeQueryBuilder typeQueryBuilder) { - return await typeQueryBuilder.ToQuery().BuildQueries().GetResultAsync(); - } - public static async Task GetResultAsync(this TypeQueryBuilder typeQueryBuilder) + return await typeQueryBuilder.ToQuery().BuildQueries().GetResultAsync(); + } + public static async Task GetResultAsync(this TypeQueryBuilder typeQueryBuilder) { return await typeQueryBuilder.ToQuery().BuildQueries().GetResultAsync(); } - public static async Task GetRawResultAsync(this TypeQueryBuilder typeQueryBuilder) + public static async Task GetRawResultAsync(this TypeQueryBuilder typeQueryBuilder) { return await typeQueryBuilder.ToQuery().BuildQueries().GetRawResultAsync(); } diff --git a/APIs/src/Testing/EpiServer.ContentGraph.UnitTests/GenerateFragmentTests.cs b/APIs/src/Testing/EpiServer.ContentGraph.UnitTests/GenerateFragmentTests.cs index 0c03be2c..a9eee8a9 100644 --- a/APIs/src/Testing/EpiServer.ContentGraph.UnitTests/GenerateFragmentTests.cs +++ b/APIs/src/Testing/EpiServer.ContentGraph.UnitTests/GenerateFragmentTests.cs @@ -11,7 +11,7 @@ public class GenerateFragmentTests public void set_name_for_fragment_should_generate_correct_name() { const string fragmentName = "my_fragment"; - FragmentBuilder fragmentBuilder = new FragmentBuilder(); + FragmentBuilder fragmentBuilder = new FragmentBuilder(); fragmentBuilder.OperationName(fragmentName); Assert.True(fragmentBuilder.GetName().Equals(fragmentName)); @@ -109,7 +109,7 @@ public void nested_fragments_should_generate_correct_query() //expect children in secondary fragment Assert.Equal(graphQueryBuilder.GetFragments().First().GetName(), "SecondFragment"); Assert.True(graphQueryBuilder.GetFragments().First().HasChildren); - Assert.Equal(graphQueryBuilder.GetFragments().First().ChildrenFragments.First().GetQuery().Query, expectedFistFragment); + Assert.Equal(graphQueryBuilder.GetFragments().First().GetChildren().First().GetQuery().Query, expectedFistFragment); //expect secondary fragment Assert.Equal(graphQueryBuilder.GetFragments().First().GetQuery().Query, expectedSecondFragmment); //expect full query @@ -146,7 +146,7 @@ public void fragment_with_inlinefragment_should_generate_correct_query() //expect children in secondary fragment Assert.Equal(graphQueryBuilder.GetFragments().First().GetName(), "SecondFragment"); Assert.True(graphQueryBuilder.GetFragments().First().HasChildren); - Assert.Equal(graphQueryBuilder.GetFragments().First().ChildrenFragments.First().GetQuery().Query, expectedFistFragment); + Assert.Equal(graphQueryBuilder.GetFragments().First().GetChildren().First().GetQuery().Query, expectedFistFragment); //expect secondary fragment Assert.Equal(graphQueryBuilder.GetFragments().First().GetQuery().Query, expectedSecondFragmment); //expect full query @@ -183,7 +183,7 @@ public void fragment_with_single_recursive_should_generate_correct_query() //expect children in secondary fragment Assert.Equal(graphQueryBuilder.GetFragments().First().GetName(), "SecondFragment"); Assert.True(graphQueryBuilder.GetFragments().First().HasChildren); - Assert.Equal(graphQueryBuilder.GetFragments().First().ChildrenFragments.First().GetQuery().Query, expectedFistFragment); + Assert.Equal(graphQueryBuilder.GetFragments().First().GetChildren().First().GetQuery().Query, expectedFistFragment); //expect secondary fragment Assert.Equal(graphQueryBuilder.GetFragments().First().GetQuery().Query, expectedSecondFragmment); //expect full query @@ -219,7 +219,7 @@ public void fragment_on_field_should_generate_correct_query() //expect children in secondary fragment Assert.Equal(graphQueryBuilder.GetFragments().First().GetName(), "SecondFragment"); Assert.True(graphQueryBuilder.GetFragments().First().HasChildren); - Assert.Equal(graphQueryBuilder.GetFragments().First().ChildrenFragments.First().GetQuery().Query, expectedFistFragment); + Assert.Equal(graphQueryBuilder.GetFragments().First().GetChildren().First().GetQuery().Query, expectedFistFragment); //expect secondary fragment Assert.Equal(graphQueryBuilder.GetFragments().First().GetQuery().Query, expectedSecondFragmment); //expect full query