-
-
Notifications
You must be signed in to change notification settings - Fork 722
/
OffsetPagingObjectFieldDescriptorExtensions.cs
353 lines (318 loc) · 12.3 KB
/
OffsetPagingObjectFieldDescriptorExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
using System;
using System.Reflection;
using HotChocolate.Internal;
using HotChocolate.Language;
using HotChocolate.Types.Descriptors;
using HotChocolate.Types.Pagination;
using Microsoft.Extensions.DependencyInjection;
using static HotChocolate.Types.Pagination.PagingDefaults;
// ReSharper disable once CheckNamespace
namespace HotChocolate.Types;
/// <summary>
/// Provides offset paging extensions to <see cref="IObjectFieldDescriptor"/> and
/// <see cref="IInterfaceFieldDescriptor"/>.
/// </summary>
public static class OffsetPagingObjectFieldDescriptorExtensions
{
/// <summary>
/// Applies the offset paging middleware to the current field.
/// </summary>
/// <param name="descriptor">
/// The object field descriptor.
/// </param>
/// <param name="itemType">
/// The item type.
/// </param>
/// <param name="resolvePagingProvider">
/// A delegate allowing to dynamically define a paging resolver for a field.
/// </param>
/// <param name="collectionSegmentName">
/// The name of the collection segment.
/// </param>
/// <param name="options">
/// The paging settings that shall be applied to this field.
/// </param>
/// <typeparam name="TSchemaType">
/// The schema type representation of the item type.
/// </typeparam>
/// <returns>
/// Returns the field descriptor for chaining in other configurations.
/// </returns>
public static IObjectFieldDescriptor UseOffsetPaging<TSchemaType>(
this IObjectFieldDescriptor descriptor,
Type? itemType = null,
GetOffsetPagingProvider? resolvePagingProvider = null,
string? collectionSegmentName = null,
PagingOptions options = default)
where TSchemaType : IOutputType =>
UseOffsetPaging(
descriptor,
typeof(TSchemaType),
itemType,
resolvePagingProvider,
collectionSegmentName,
options);
/// <summary>
/// Applies the offset paging middleware to the current field.
/// </summary>
/// <param name="descriptor">
/// The object field descriptor.
/// </param>
/// <param name="itemType">
/// The schema type representation of the item.
/// </param>
/// <param name="entityType">
/// The entity type represents the runtime type of the item.
/// </param>
/// <param name="resolvePagingProvider">
/// A delegate allowing to dynamically define a paging resolver for a field.
/// </param>
/// <param name="collectionSegmentName">
/// The name of the collection segment.
/// </param>
/// <param name="options">
/// The paging settings that shall be applied to this field.
/// </param>
/// <returns>
/// Returns the field descriptor for chaining in other configurations.
/// </returns>
public static IObjectFieldDescriptor UseOffsetPaging(
this IObjectFieldDescriptor descriptor,
Type? itemType = null,
Type? entityType = null,
GetOffsetPagingProvider? resolvePagingProvider = null,
string? collectionSegmentName = null,
PagingOptions options = default)
{
if (descriptor is null)
{
throw new ArgumentNullException(nameof(descriptor));
}
resolvePagingProvider ??= ResolvePagingProvider;
descriptor.AddOffsetPagingArguments();
PagingHelper.UsePaging(
descriptor,
entityType,
(services, source, name) => resolvePagingProvider(services, source, name),
options);
descriptor
.Extend()
.OnBeforeCreate((c, d) =>
{
var pagingOptions = c.GetSettings(options);
if (string.IsNullOrEmpty(collectionSegmentName))
{
collectionSegmentName =
pagingOptions.InferCollectionSegmentNameFromField ??
InferCollectionSegmentNameFromField
? EnsureCollectionSegmentNameCasing(d.Name)
: null;
}
TypeReference? typeRef = itemType is not null
? c.TypeInspector.GetTypeRef(itemType)
: null;
if (typeRef is null &&
d.Type is SyntaxTypeReference syntaxTypeRef &&
syntaxTypeRef.Type.IsListType())
{
typeRef = syntaxTypeRef.WithType(syntaxTypeRef.Type.ElementType());
}
var resolverMember = d.ResolverMember ?? d.Member;
d.Type = CreateTypeRef(c, resolverMember, collectionSegmentName, typeRef, options);
d.CustomSettings.Add(typeof(CollectionSegment));
});
return descriptor;
}
/// <summary>
/// Applies the offset paging middleware to the current field.
/// </summary>
/// <param name="descriptor">
/// The object field descriptor.
/// </param>
/// <param name="collectionSegmentName">
/// The name of the collection segment.
/// </param>
/// <param name="options">
/// The paging settings that shall be applied to this field.
/// </param>
/// <typeparam name="TSchemaType">
/// The schema type representation of the item type.
/// </typeparam>
/// <returns>
/// Returns the field descriptor for chaining in other configurations.
/// </returns>
public static IInterfaceFieldDescriptor UseOffsetPaging<TSchemaType>(
this IInterfaceFieldDescriptor descriptor,
string? collectionSegmentName = null,
PagingOptions options = default)
where TSchemaType : class, IOutputType =>
UseOffsetPaging(descriptor, typeof(TSchemaType), collectionSegmentName, options);
/// <summary>
/// Applies the offset paging middleware to the current field.
/// </summary>
/// <param name="descriptor">
/// The object field descriptor.
/// </param>
/// <param name="itemType">
/// The schema type representation of the item type.
/// </param>
/// <param name="collectionSegmentName">
/// The name of the collection segment.
/// </param>
/// <param name="options">
/// The paging settings that shall be applied to this field.
/// </param>
/// <returns>
/// Returns the field descriptor for chaining in other configurations.
/// </returns>
public static IInterfaceFieldDescriptor UseOffsetPaging(
this IInterfaceFieldDescriptor descriptor,
Type? itemType = null,
string? collectionSegmentName = null,
PagingOptions options = default)
{
if (descriptor is null)
{
throw new ArgumentNullException(nameof(descriptor));
}
descriptor.AddOffsetPagingArguments();
descriptor
.Extend()
.OnBeforeCreate((c, d) =>
{
var pagingOptions = c.GetSettings(options);
if (string.IsNullOrEmpty(collectionSegmentName))
{
collectionSegmentName =
pagingOptions.InferCollectionSegmentNameFromField ??
InferCollectionSegmentNameFromField
? EnsureCollectionSegmentNameCasing(d.Name)
: null;
}
TypeReference? typeRef = itemType is not null
? c.TypeInspector.GetTypeRef(itemType)
: null;
if (typeRef is null &&
d.Type is SyntaxTypeReference syntaxTypeRef &&
syntaxTypeRef.Type.IsListType())
{
typeRef = syntaxTypeRef.WithType(syntaxTypeRef.Type.ElementType());
}
var resolverMember = d.Member;
d.Type = CreateTypeRef(c, resolverMember, collectionSegmentName, typeRef, options);
});
return descriptor;
}
/// <summary>
/// Adds the offset paging arguments to an object field.
/// </summary>
public static IObjectFieldDescriptor AddOffsetPagingArguments(
this IObjectFieldDescriptor descriptor)
{
if (descriptor is null)
{
throw new ArgumentNullException(nameof(descriptor));
}
return descriptor
.Argument(OffsetPagingArgumentNames.Skip, a => a.Type<IntType>())
.Argument(OffsetPagingArgumentNames.Take, a => a.Type<IntType>());
}
/// <summary>
/// Adds the offset paging arguments to an interface field.
/// </summary>
public static IInterfaceFieldDescriptor AddOffsetPagingArguments(
this IInterfaceFieldDescriptor descriptor)
{
if (descriptor is null)
{
throw new ArgumentNullException(nameof(descriptor));
}
return descriptor
.Argument(OffsetPagingArgumentNames.Skip, a => a.Type<IntType>())
.Argument(OffsetPagingArgumentNames.Take, a => a.Type<IntType>());
}
private static TypeReference CreateTypeRef(
IDescriptorContext context,
MemberInfo? resolverMember,
string? collectionSegmentName,
TypeReference? itemsType,
PagingOptions options)
{
var typeInspector = context.TypeInspector;
itemsType ??= TypeReference.Create(
PagingHelper.GetSchemaType(context, resolverMember),
TypeContext.Output);
// if the node type is a syntax type reference we will try to preserve the actual
// runtime type for later usage.
if (itemsType.Kind == TypeReferenceKind.Syntax &&
PagingHelper.TryGetNamedType(typeInspector, resolverMember, out var namedType))
{
context.TryBindRuntimeType(
((SyntaxTypeReference)itemsType).Type.NamedType().Name.Value,
namedType);
}
options = context.GetSettings(options);
// last but not leas we create a type reference that can be put on the field definition
// to tell the type discovery that this field needs this result type.
return collectionSegmentName is null
? TypeReference.Create(
"HotChocolate_Types_CollectionSegment",
itemsType,
_ => new CollectionSegmentType(
null,
itemsType,
options.IncludeTotalCount ?? false),
TypeContext.Output)
: TypeReference.Create(
collectionSegmentName + "CollectionSegment",
TypeContext.Output,
factory: _ => new CollectionSegmentType(
collectionSegmentName,
itemsType,
options.IncludeTotalCount ?? false));
}
private static OffsetPagingProvider ResolvePagingProvider(
IServiceProvider services,
IExtendedType source,
string? providerName)
{
try
{
Func<PagingProviderEntry, bool> predicate =
providerName is null
? entry => entry.Provider.CanHandle(source)
: entry => providerName.Equals(entry.Name, StringComparison.Ordinal);
PagingProviderEntry? defaultEntry = null;
foreach (var entry in services.GetServices<PagingProviderEntry>())
{
// the first provider is expected to be the default provider.
defaultEntry ??= entry;
if (predicate(entry))
{
return entry.Provider;
}
}
if (defaultEntry is not null)
{
return defaultEntry.Provider;
}
}
catch (InvalidOperationException)
{
// some containers will except if a service does not exist.
// in this case we will ignore the exception and return the default provider.
}
// if no provider was added we will fallback to the queryable paging provider.
return new QueryableOffsetPagingProvider();
}
private static string EnsureCollectionSegmentNameCasing(string collectionSegmentName)
{
if (char.IsUpper(collectionSegmentName[0]))
{
return collectionSegmentName;
}
return string.Concat(
char.ToUpper(collectionSegmentName[0]),
collectionSegmentName.Substring(1));
}
}