|
1 |
| -using EPiServer.ContentGraph.Helpers; |
| 1 | +using EPiServer.ContentGraph.ExpressionHelper; |
| 2 | +using EPiServer.ContentGraph.Extensions; |
| 3 | +using EPiServer.ContentGraph.Helpers; |
2 | 4 | using EPiServer.ContentGraph.Helpers.Reflection;
|
3 | 5 | using GraphQL.Transport;
|
4 | 6 | using System;
|
5 | 7 | using System.Linq.Expressions;
|
| 8 | +using System.Text; |
6 | 9 | using System.Text.RegularExpressions;
|
7 | 10 |
|
8 | 11 | namespace EPiServer.ContentGraph.Api.Querying
|
9 | 12 | {
|
| 13 | + public class Recursion |
| 14 | + { |
| 15 | + public string FieldName { get; set; } |
| 16 | + public int? RecursiveDepth { get; set; } |
| 17 | + public override string ToString() |
| 18 | + { |
| 19 | + if (RecursiveDepth.IsNull() || RecursiveDepth.Value < 0) |
| 20 | + { |
| 21 | + return ConvertNestedFieldToString.ConvertNestedFieldForQuery($"{FieldName} @recursive"); |
| 22 | + } |
| 23 | + else |
| 24 | + { |
| 25 | + return ConvertNestedFieldToString.ConvertNestedFieldForQuery($"{FieldName} @recursive(depth:{RecursiveDepth.Value})"); |
| 26 | + } |
| 27 | + } |
| 28 | + } |
| 29 | + public class FragmentBuilder : BaseTypeQueryBuilder |
| 30 | + { |
| 31 | + private List<FragmentBuilder> _childrenFragments; |
| 32 | + public IEnumerable<FragmentBuilder> ChildrenFragments => _childrenFragments; |
| 33 | + public bool HasChildren => _childrenFragments != null && _childrenFragments.Any(); |
| 34 | + public FragmentBuilder() : base() |
| 35 | + { |
| 36 | + _query.OperationName = "sampleFragment"; |
| 37 | + } |
| 38 | + public void OperationName(string name) |
| 39 | + { |
| 40 | + Regex reg = new Regex(@"^[a-zA-Z_]\w*$"); |
| 41 | + if (reg.IsMatch(name)) |
| 42 | + { |
| 43 | + _query.OperationName = name; |
| 44 | + } |
| 45 | + } |
| 46 | + public string GetName() |
| 47 | + { |
| 48 | + return _query.OperationName; |
| 49 | + } |
| 50 | + public override FragmentBuilder AddFragments(params FragmentBuilder[] fragments) |
| 51 | + { |
| 52 | + if (fragments.IsNotNull() && fragments.Length > 0) |
| 53 | + { |
| 54 | + if (_childrenFragments.IsNull()) |
| 55 | + { |
| 56 | + _childrenFragments = new List<FragmentBuilder>(); |
| 57 | + } |
| 58 | + base.AddFragments(fragments); |
| 59 | + _childrenFragments.AddRange(fragments); |
| 60 | + } |
| 61 | + return this; |
| 62 | + } |
| 63 | + } |
| 64 | + |
10 | 65 | public class FragmentBuilder<T> : FragmentBuilder
|
11 | 66 | {
|
12 | 67 | public FragmentBuilder() : base() { }
|
@@ -46,46 +101,107 @@ public FragmentBuilder<T> Children<TChildren>(TypeQueryBuilder<TChildren> childr
|
46 | 101 | base.Children(children);
|
47 | 102 | return this;
|
48 | 103 | }
|
49 |
| - public override GraphQLRequest GetQuery() |
| 104 | + private FragmentBuilder Recursive<TSub>(params Recursion[] recursives) where TSub : T |
50 | 105 | {
|
51 |
| - _query.Query = $"fragment {_query.OperationName} on {typeof(T).Name} {graphObject}"; |
52 |
| - return _query; |
53 |
| - } |
54 |
| - } |
| 106 | + recursives.ValidateNotNullArgument("recursives"); |
| 107 | + if (!recursives.Any()) throw new ArgumentException("recursives can not be empty"); |
| 108 | + string recursiveNess = string.Empty; |
| 109 | + foreach (var recursive in recursives) |
| 110 | + { |
| 111 | + recursiveNess += recursive.ToString(); |
| 112 | + } |
55 | 113 |
|
56 |
| - public class FragmentBuilder : BaseTypeQueryBuilder |
57 |
| - { |
58 |
| - private List<FragmentBuilder> _childrenFragments; |
59 |
| - public IEnumerable<FragmentBuilder> ChildrenFragments => _childrenFragments; |
60 |
| - public bool HasChildren => _childrenFragments != null && _childrenFragments.Any(); |
61 |
| - public FragmentBuilder() : base() |
| 114 | + string subTypeName = typeof(TSub).Name; |
| 115 | + graphObject.SelectItems.Append(graphObject.SelectItems.Length == 0 ? |
| 116 | + $"... on {subTypeName}{{{recursiveNess}}}" : |
| 117 | + $" ... on {subTypeName}{{{recursiveNess}}}" |
| 118 | + ); |
| 119 | + return this; |
| 120 | + } |
| 121 | + private FragmentBuilder<T> Recursive<TSub>(string fieldName, int? depth = null) where TSub : T |
62 | 122 | {
|
63 |
| - _query.OperationName = "sampleFragment"; |
| 123 | + Recursive<TSub>(new Recursion() { FieldName = fieldName, RecursiveDepth = depth }); ; |
| 124 | + return this; |
64 | 125 | }
|
65 |
| - public void OperationName(string name) |
| 126 | + /// <summary> |
| 127 | + /// Recursive directive for a subtype |
| 128 | + /// </summary> |
| 129 | + /// <typeparam name="TSub">Subtype must inherits from type <typeparamref name="T"/></typeparam> |
| 130 | + /// <param name="fieldSelectors">Select field must inherits from <typeparamref name="T"/></param> |
| 131 | + /// <returns>FragmentBuilder</returns> |
| 132 | + public FragmentBuilder<T> Recursive<TSub>(Expression<Func<TSub, T>> fieldSelector, int? depth = null) where TSub : T |
66 | 133 | {
|
67 |
| - Regex reg = new Regex(@"^[a-zA-Z_]\w*$"); |
68 |
| - if (reg.IsMatch(name)) |
69 |
| - { |
70 |
| - _query.OperationName = name; |
71 |
| - } |
| 134 | + var fieldName = fieldSelector.GetFieldPath(); |
| 135 | + Recursive<TSub>(fieldName, depth); |
| 136 | + return this; |
72 | 137 | }
|
73 |
| - public string GetName() |
| 138 | + /// <summary> |
| 139 | + /// Inline fragment on a subtype |
| 140 | + /// </summary> |
| 141 | + /// <typeparam name="TSub"></typeparam> |
| 142 | + /// <param name="fieldSelectors"></param> |
| 143 | + /// <returns></returns> |
| 144 | + public FragmentBuilder<T> Inline<TSub>(params string[] fields) |
74 | 145 | {
|
75 |
| - return _query.OperationName; |
| 146 | + fields.ValidateNotNullArgument("fields"); |
| 147 | + StringBuilder propertyBuilder = new StringBuilder(); |
| 148 | + foreach (var field in fields) |
| 149 | + { |
| 150 | + if (propertyBuilder.Length > 0) |
| 151 | + { |
| 152 | + propertyBuilder.Append(" "); |
| 153 | + } |
| 154 | + propertyBuilder.Append(ConvertNestedFieldToString.ConvertNestedFieldForQuery(field)); |
| 155 | + } |
| 156 | + string subTypeName = typeof(TSub).Name; |
| 157 | + graphObject.SelectItems.Append(graphObject.SelectItems.Length == 0 ? |
| 158 | + $"... on {subTypeName}{{{propertyBuilder}}}" : |
| 159 | + $" ... on {subTypeName}{{{propertyBuilder}}}" |
| 160 | + ); |
| 161 | + return this; |
76 | 162 | }
|
77 |
| - public override FragmentBuilder Fragments(params FragmentBuilder[] fragments) |
| 163 | + /// <summary> |
| 164 | + /// Inline fragment on a subtype |
| 165 | + /// </summary> |
| 166 | + /// <typeparam name="TSub"></typeparam> |
| 167 | + /// <param name="fieldSelectors"></param> |
| 168 | + /// <returns></returns> |
| 169 | + public FragmentBuilder<T> Inline<TSub>(params Expression<Func<TSub, object>>[] fieldSelectors) where TSub : T |
78 | 170 | {
|
79 |
| - if (fragments.IsNotNull() && fragments.Length > 0) |
| 171 | + fieldSelectors.ValidateNotNullArgument("fieldSelectors"); |
| 172 | + StringBuilder propertyBuilder = new StringBuilder(); |
| 173 | + foreach (var fieldSelector in fieldSelectors) |
80 | 174 | {
|
81 |
| - if (_childrenFragments.IsNull()) |
| 175 | + if (propertyBuilder.Length > 0) |
82 | 176 | {
|
83 |
| - _childrenFragments = new List<FragmentBuilder>(); |
| 177 | + propertyBuilder.Append(" "); |
84 | 178 | }
|
85 |
| - base.Fragments(fragments); |
86 |
| - _childrenFragments.AddRange(fragments); |
| 179 | + propertyBuilder.Append(ConvertNestedFieldToString.ConvertNestedFieldForQuery(fieldSelector.GetFieldPath())); |
87 | 180 | }
|
| 181 | + string subTypeName = typeof(TSub).Name; |
| 182 | + graphObject.SelectItems.Append(graphObject.SelectItems.Length == 0 ? |
| 183 | + $"... on {subTypeName}{{{propertyBuilder}}}" : |
| 184 | + $" ... on {subTypeName}{{{propertyBuilder}}}" |
| 185 | + ); |
| 186 | + return this; |
| 187 | + } |
| 188 | + /// <summary> |
| 189 | + /// Inline fragment on a subtype |
| 190 | + /// </summary> |
| 191 | + /// <typeparam name="TSub"></typeparam> |
| 192 | + /// <param name="fieldSelectors"></param> |
| 193 | + /// <returns></returns> |
| 194 | + public FragmentBuilder<T> Inline<TSub>(params Expression<Func<TSub, Recursion>>[] fieldSelectors) where TSub : T |
| 195 | + { |
| 196 | + fieldSelectors.ValidateNotNullOrEmptyArgument("fieldSelectors"); |
| 197 | + var paser = new FragmentExpressionParser(); |
| 198 | + Recursive<TSub>(fieldSelectors.Select(selector => paser.GetReturnType(selector)).ToArray()); |
88 | 199 | return this;
|
89 | 200 | }
|
| 201 | + public override GraphQLRequest GetQuery() |
| 202 | + { |
| 203 | + _query.Query = $"fragment {_query.OperationName} on {typeof(T).Name} {graphObject}"; |
| 204 | + return _query; |
| 205 | + } |
90 | 206 | }
|
91 | 207 | }
|
0 commit comments