1
1
using System . Buffers ;
2
- using System . Buffers . Text ;
3
2
using System . Diagnostics . CodeAnalysis ;
4
- using System . Linq . Expressions ;
5
- using System . Runtime . CompilerServices ;
6
- using System . Text ;
7
3
using HotChocolate . Execution ;
8
4
using HotChocolate . Execution . Processing ;
9
- using HotChocolate . Execution . Projections ;
10
5
using HotChocolate . Pagination ;
11
6
using HotChocolate . Types ;
12
7
using HotChocolate . Types . Descriptors . Definitions ;
@@ -21,8 +16,6 @@ namespace GreenDonut.Projections;
21
16
[ Experimental ( Experiments . Projections ) ]
22
17
public static class HotChocolateExecutionDataLoaderExtensions
23
18
{
24
- private static readonly SelectionExpressionBuilder _builder = new ( ) ;
25
-
26
19
/// <summary>
27
20
/// Selects the fields that where selected in the GraphQL selection tree.
28
21
/// </summary>
@@ -46,7 +39,7 @@ public static ISelectionDataLoader<TKey, TValue> Select<TKey, TValue>(
46
39
ISelection selection )
47
40
where TKey : notnull
48
41
{
49
- var expression = GetOrCreateExpression < TKey , TValue > ( selection ) ;
42
+ var expression = selection . ToSelectorExpression < TValue > ( ) ;
50
43
return dataLoader . Select ( expression ) ;
51
44
}
52
45
@@ -81,8 +74,8 @@ public static IPagingDataLoader<TKey, Page<TValue>> Select<TKey, TValue>(
81
74
var count = GetConnectionSelections ( selection , buffer ) ;
82
75
for ( var i = 0 ; i < count ; i ++ )
83
76
{
84
- var expression = GetOrCreateExpression < TKey , TValue > ( buffer [ i ] ) ;
85
- HotChocolatePaginationBatchingDataLoaderExtensions . Select ( dataLoader , expression ) ;
77
+ var expression = buffer [ i ] . ToSelectorExpression < TValue > ( ) ;
78
+ dataLoader . Select ( expression ) ;
86
79
}
87
80
ArrayPool < ISelection > . Shared . Return ( buffer ) ;
88
81
}
@@ -92,15 +85,15 @@ public static IPagingDataLoader<TKey, Page<TValue>> Select<TKey, TValue>(
92
85
var count = GetCollectionSelections ( selection , buffer ) ;
93
86
for ( var i = 0 ; i < count ; i ++ )
94
87
{
95
- var expression = GetOrCreateExpression < TKey , TValue > ( buffer [ i ] ) ;
96
- HotChocolatePaginationBatchingDataLoaderExtensions . Select ( dataLoader , expression ) ;
88
+ var expression = buffer [ i ] . ToSelectorExpression < TValue > ( ) ;
89
+ dataLoader . Select ( expression ) ;
97
90
}
98
91
ArrayPool < ISelection > . Shared . Return ( buffer ) ;
99
92
}
100
93
else
101
94
{
102
- var expression = GetOrCreateExpression < TKey , TValue > ( selection ) ;
103
- HotChocolatePaginationBatchingDataLoaderExtensions . Select ( dataLoader , expression ) ;
95
+ var expression = selection . ToSelectorExpression < TValue > ( ) ;
96
+ dataLoader . Select ( expression ) ;
104
97
}
105
98
106
99
return dataLoader ;
@@ -167,45 +160,4 @@ private static int GetCollectionSelections(ISelection selection, Span<ISelection
167
160
168
161
return count ;
169
162
}
170
-
171
- private static Expression < Func < TValue , TValue > > GetOrCreateExpression < TKey , TValue > (
172
- ISelection selection )
173
- where TKey : notnull
174
- {
175
- return selection . DeclaringOperation . GetOrAddState (
176
- CreateExpressionKey ( selection . Id ) ,
177
- static ( _ , ctx ) => ctx . _builder . BuildExpression < TValue > ( ctx . selection ) ,
178
- ( _builder , selection ) ) ;
179
- }
180
-
181
- private static string CreateExpressionKey ( int key )
182
- {
183
- var keyPrefix = GetKeyPrefix ( ) ;
184
- var requiredBufferSize = EstimateIntLength ( key ) + keyPrefix . Length ;
185
- Span < byte > span = stackalloc byte [ requiredBufferSize ] ;
186
- keyPrefix . CopyTo ( span ) ;
187
- Utf8Formatter . TryFormat ( key , span . Slice ( keyPrefix . Length ) , out var written , 'D' ) ;
188
- return Encoding . UTF8 . GetString ( span . Slice ( 0 , written + keyPrefix . Length ) ) ;
189
- }
190
-
191
- private static ReadOnlySpan < byte > GetKeyPrefix ( )
192
- => "hc-dataloader-expr-"u8 ;
193
-
194
- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
195
- private static int EstimateIntLength ( int value )
196
- {
197
- if ( value == 0 )
198
- {
199
- // to print 0 we need still 1 digit
200
- return 1 ;
201
- }
202
-
203
- // if the number is negative we need one more digit for the sign
204
- var length = ( value < 0 ) ? 1 : 0 ;
205
-
206
- // we add the number of digits the number has to the length of the number.
207
- length += ( int ) Math . Floor ( Math . Log10 ( Math . Abs ( value ) ) + 1 ) ;
208
-
209
- return length ;
210
- }
211
163
}
0 commit comments