|
5 | 5 | using System.Collections.Generic; |
6 | 6 | using System.Text.Json; |
7 | 7 | using System.Threading.Tasks; |
| 8 | +using Newtonsoft.Json; |
| 9 | +using Newtonsoft.Json.Linq; |
8 | 10 |
|
9 | 11 | namespace Azure.DataGateway.Services |
10 | 12 | { |
@@ -42,54 +44,86 @@ public async Task InvokeAsync(IMiddlewareContext context) |
42 | 44 | if (context.Selection.Field.Coordinate.TypeName.Value == "Query") |
43 | 45 | { |
44 | 46 | IDictionary<string, object> parameters = GetParametersFromContext(context); |
| 47 | + string queryId = context.Selection.Field.Name.Value; |
| 48 | + bool isContinuationQuery = IsContinuationQuery(parameters); |
45 | 49 |
|
46 | 50 | if (context.Selection.Type.IsListType()) |
47 | 51 | { |
48 | | - context.Result = await _queryEngine.ExecuteListAsync(context.Selection.Field.Name.Value, parameters); |
| 52 | + context.Result = await _queryEngine.ExecuteListAsync(queryId, parameters); |
49 | 53 | } |
50 | 54 | else |
51 | 55 | { |
52 | | - context.Result = await _queryEngine.ExecuteAsync(context.Selection.Field.Name.Value, parameters); |
| 56 | + context.Result = await _queryEngine.ExecuteAsync(context.Selection.Field.Name.Value, parameters, isContinuationQuery); |
53 | 57 | } |
54 | 58 | } |
55 | | - |
56 | | - if (IsInnerObject(context)) |
| 59 | + else |
57 | 60 | { |
58 | 61 | JsonDocument result = context.Parent<JsonDocument>(); |
59 | 62 |
|
60 | 63 | JsonElement jsonElement; |
61 | 64 | bool hasProperty = |
62 | 65 | result.RootElement.TryGetProperty(context.Selection.Field.Name.Value, out jsonElement); |
63 | | - if (result != null && hasProperty) |
| 66 | + |
| 67 | + if (IsInnerObject(context)) |
64 | 68 | { |
65 | | - //TODO: Try to avoid additional deserialization/serialization here. |
66 | | - context.Result = JsonDocument.Parse(jsonElement.ToString()); |
| 69 | + |
| 70 | + if (result != null && hasProperty) |
| 71 | + { |
| 72 | + //TODO: Try to avoid additional deserialization/serialization here. |
| 73 | + context.Result = JsonDocument.Parse(jsonElement.ToString()); |
| 74 | + } |
| 75 | + else |
| 76 | + { |
| 77 | + context.Result = null; |
| 78 | + } |
67 | 79 | } |
68 | | - else |
| 80 | + else if (context.Selection.Field.Type.IsListType()) |
69 | 81 | { |
70 | | - context.Result = null; |
71 | | - } |
72 | | - } |
| 82 | + if (result != null && hasProperty) |
| 83 | + { |
| 84 | + //TODO: System.Text.Json seem to to have very limited capabilities. This will be moved to Newtonsoft |
| 85 | + IEnumerable<JObject> resultArray = JsonConvert.DeserializeObject<JObject[]>(jsonElement.ToString()); |
| 86 | + List<JsonDocument> resultList = new(); |
| 87 | + IEnumerator<JObject> enumerator = resultArray.GetEnumerator(); |
| 88 | + while (enumerator.MoveNext()) |
| 89 | + { |
| 90 | + resultList.Add(JsonDocument.Parse(enumerator.Current.ToString())); |
| 91 | + } |
| 92 | + context.Result = resultList; |
| 93 | + } |
| 94 | + else |
| 95 | + { |
| 96 | + context.Result = null; |
| 97 | + } |
73 | 98 |
|
74 | | - if (context.Selection.Field.Type.IsLeafType()) |
75 | | - { |
76 | | - JsonDocument result = context.Parent<JsonDocument>(); |
77 | | - JsonElement jsonElement; |
78 | | - bool hasProperty = |
79 | | - result.RootElement.TryGetProperty(context.Selection.Field.Name.Value, out jsonElement); |
80 | | - if (result != null && hasProperty) |
81 | | - { |
82 | | - context.Result = jsonElement.ToString(); |
83 | 99 | } |
84 | | - else |
| 100 | + |
| 101 | + if (context.Selection.Field.Type.IsLeafType()) |
85 | 102 | { |
86 | | - context.Result = null; |
| 103 | + if (result != null && hasProperty) |
| 104 | + { |
| 105 | + context.Result = jsonElement.ToString(); |
| 106 | + } |
| 107 | + else |
| 108 | + { |
| 109 | + context.Result = null; |
| 110 | + } |
87 | 111 | } |
88 | 112 | } |
89 | 113 |
|
90 | 114 | await _next(context); |
91 | 115 | } |
92 | 116 |
|
| 117 | + private static bool IsContinuationQuery(IDictionary<string, object> parameters) |
| 118 | + { |
| 119 | + if (parameters.ContainsKey("after")) |
| 120 | + { |
| 121 | + return true; |
| 122 | + } |
| 123 | + |
| 124 | + return false; |
| 125 | + } |
| 126 | + |
93 | 127 | private static bool IsInnerObject(IMiddlewareContext context) |
94 | 128 | { |
95 | 129 | return context.Selection.Field.Type.IsObjectType() && context.Parent<JsonDocument>() != default; |
|
0 commit comments