-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathGlobalFilterSnippets.cs
More file actions
370 lines (290 loc) · 11.3 KB
/
GlobalFilterSnippets.cs
File metadata and controls
370 lines (290 loc) · 11.3 KB
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// ReSharper disable UnusedParameter.Local
public class GlobalFilterSnippets
{
#region add-filter
public class MyEntity
{
public Guid Id { get; set; }
public string? Property { get; set; }
public int Quantity { get; set; }
public bool IsActive { get; set; }
}
#endregion
public static void Add(ServiceCollection services)
{
#region filter-all-fields
var filters = new Filters<MyDbContext>();
filters.For<MyEntity>().Add(
projection: _ => new
{
_.Property,
_.Quantity,
_.IsActive
},
filter: (userContext, dbContext, userPrincipal, projected) =>
projected.Property != "Ignore" &&
projected.Quantity > 0 &&
projected.IsActive);
EfGraphQLConventions.RegisterInContainer<MyDbContext>(
services,
resolveFilters: _ => filters);
#endregion
}
public class MyDbContext :
DbContext
{
public DbSet<Category> Categories { get; set; } = null!;
public DbSet<UserPermission> UserPermissions { get; set; } = null!;
}
public class UserPermission
{
public Guid Id { get; set; }
public string UserId { get; set; } = null!;
public string Permission { get; set; } = null!;
}
#region projection-filter
public class ChildEntity
{
public Guid Id { get; set; }
public Guid? ParentId { get; set; }
public string? Property { get; set; }
}
#endregion
public static void AddProjectionFilter(ServiceCollection services)
{
#region projection-filter
var filters = new Filters<MyDbContext>();
filters.For<ChildEntity>().Add(
projection: _ => new
{
_.ParentId
},
filter: (userContext, data, userPrincipal, projected) =>
{
var allowedParentId = GetAllowedParentId(userContext);
return projected.ParentId == allowedParentId;
});
EfGraphQLConventions.RegisterInContainer<MyDbContext>(
services,
resolveFilters: _ => filters);
#endregion
}
static Guid GetAllowedParentId(object userContext) =>
Guid.Empty;
#region value-type-projections
public class Product
{
public Guid Id { get; set; }
public string? Name { get; set; }
public int Stock { get; set; }
public bool IsActive { get; set; }
public DateTime CreatedAt { get; set; }
public Guid CategoryId { get; set; }
}
#endregion
public static void AddValueTypeProjections(ServiceCollection services)
{
#region value-type-projections
var filters = new Filters<MyDbContext>();
// Filter using a string property
filters.For<Product>().Add(
projection: _ => _.Name!,
filter: (_, _, _, name) => name != "Discontinued");
// Filter using an int property
filters.For<Product>().Add(
projection: _ => _.Stock,
filter: (_, _, _, stock) => stock > 0);
// Filter using a bool property
filters.For<Product>().Add(
projection: _ => _.IsActive,
filter: (_, _, _, isActive) => isActive);
// Filter using a DateTime property
filters.For<Product>().Add(
projection: _ => _.CreatedAt,
filter: (_, _, _, createdAt) => createdAt >= new DateTime(2024, 1, 1));
EfGraphQLConventions.RegisterInContainer<MyDbContext>(
services,
resolveFilters: _ => filters);
#endregion
}
#region nullable-value-type-projections
public class Order
{
public Guid Id { get; set; }
public int? Quantity { get; set; }
public bool? IsApproved { get; set; }
public DateTime? ShippedAt { get; set; }
public string? Notes { get; set; }
public decimal TotalAmount { get; set; }
public Customer Customer { get; set; } = null!;
}
public class Customer
{
public Guid Id { get; set; }
public bool IsActive { get; set; }
}
public class Category
{
public Guid Id { get; set; }
public bool IsVisible { get; set; }
}
#endregion
public static void AddNullableValueTypeProjections(ServiceCollection services)
{
#region nullable-value-type-projections
var filters = new Filters<MyDbContext>();
// Filter nullable int - only include if has value and meets condition
filters.For<Order>().Add(
projection: _ => _.Quantity,
filter: (_, _, _, quantity) => quantity is > 0);
// Filter nullable bool - only include if explicitly approved
filters.For<Order>().Add(
projection: _ => _.IsApproved,
filter: (_, _, _, isApproved) => isApproved == true);
// Filter nullable DateTime - only include if shipped after date
filters.For<Order>().Add(
projection: _ => _.ShippedAt,
filter: (_, _, _, shippedAt) =>
shippedAt.HasValue && shippedAt.Value >= new DateTime(2024, 1, 1));
// Filter nullable string - only include non-null values
filters.For<Order>().Add(
projection: _ => _.Notes,
filter: (_, _, _, notes) => notes != null);
// Filter nullable int - only include null values
filters.For<Order>().Add(
projection: _ => _.Quantity,
filter: (_, _, _, quantity) => !quantity.HasValue);
EfGraphQLConventions.RegisterInContainer<MyDbContext>(
services,
resolveFilters: _ => filters);
#endregion
}
public static void AddAsyncFilter(ServiceCollection services)
{
#region async-filter
var filters = new Filters<MyDbContext>();
filters.For<Product>().Add(
projection: _ => _.CategoryId,
filter: async (_, dbContext, _, categoryId) =>
{
var category = await dbContext.Categories.FindAsync(categoryId);
return category?.IsVisible == true;
});
EfGraphQLConventions.RegisterInContainer<MyDbContext>(
services,
resolveFilters: _ => filters);
#endregion
}
public static void AddNavigationPropertyFilter(ServiceCollection services)
{
#region navigation-property-filter
var filters = new Filters<MyDbContext>();
filters.For<Order>().Add(
projection: _ => new { _.TotalAmount, _.Customer.IsActive },
filter: (_, _, _, x) => x.TotalAmount >= 100 && x.IsActive);
EfGraphQLConventions.RegisterInContainer<MyDbContext>(
services,
resolveFilters: _ => filters);
#endregion
}
public static void AddBooleanExpressionFilter(ServiceCollection services)
{
#region boolean-expression-filter
var filters = new Filters<MyDbContext>();
// Simplified syntax for boolean properties
filters.For<Product>().Add(filter: _ => _.IsActive);
// Equivalent to:
// filters.For<Product>().Add(
// projection: _ => _.IsActive,
// filter: (_, _, _, isActive) => isActive);
EfGraphQLConventions.RegisterInContainer<MyDbContext>(
services,
resolveFilters: _ => filters);
#endregion
}
public static void AddFilterWithoutProjection(ServiceCollection services)
{
#region filter-without-projection
var filters = new Filters<MyDbContext>();
// Filter without projection - eg for authorization checks
filters.For<Product>().Add(
filter: (_, _, user) => user!.HasClaim("Permission", "ViewProducts"));
// Equivalent to:
// filters.For<Product>().Add(
// projection: null, // No projection needed
// filter: (_, _, user, _) => user!.HasClaim("Permission", "ViewProducts"));
EfGraphQLConventions.RegisterInContainer<MyDbContext>(
services,
resolveFilters: _ => filters);
#endregion
}
public static void AddAsyncFilterWithoutProjection(ServiceCollection services)
{
#region async-filter-without-projection
var filters = new Filters<MyDbContext>();
// Async filter without projection - eg for database permission checks
filters.For<Product>().Add(
filter: async (_, dbContext, user) =>
{
var userId = user?.FindFirst("UserId")?.Value;
if (userId == null)
return false;
var permissions = await dbContext.UserPermissions
.Where(_ => _.UserId == userId)
.AnyAsync(_ => _.Permission == "ViewProducts");
return permissions;
});
EfGraphQLConventions.RegisterInContainer<MyDbContext>(
services,
resolveFilters: _ => filters);
#endregion
}
#region simplified-filter-api
public class Accommodation
{
public Guid Id { get; set; }
public Guid? LocationId { get; set; }
public string? City { get; set; }
public int Capacity { get; set; }
}
#endregion
public static void AddSimplifiedFilterApi(ServiceCollection services)
{
#region simplified-filter-api
var filters = new Filters<MyDbContext>();
// VALID: Simplified API with primary key access
filters.For<Accommodation>().Add(
filter: (_, _, _, a) => a.Id != Guid.Empty);
// VALID: Simplified API with foreign key access
var allowedLocationId = Guid.NewGuid();
filters.For<Accommodation>().Add(
filter: (_, _, _, a) => a.LocationId == allowedLocationId);
// VALID: Simplified API with nullable foreign key check
filters.For<Accommodation>().Add(
filter: (_, _, _, a) => a.LocationId != null);
// INVALID: Simplified API accessing scalar property (will cause runtime error!)
// filters.For<Accommodation>().Add(
// filter: (_, _, _, a) => a.City == "London"); // ERROR: City is not a key
// INVALID: Simplified API accessing scalar property (will cause runtime error!)
// filters.For<Accommodation>().Add(
// filter: (_, _, _, a) => a.Capacity > 10); // ERROR: Capacity is not a key
// For non-key properties, use the full API with projection:
filters.For<Accommodation>().Add(
projection: a => a.City,
filter: (_, _, _, city) => city == "London");
filters.For<Accommodation>().Add(
projection: a => new { a.City, a.Capacity },
filter: (_, _, _, x) => x.City == "London" && x.Capacity > 10);
// COMPARISON: These are equivalent when filter only accesses keys
filters.For<Accommodation>().Add(
filter: (_, _, _, a) => a.Id != Guid.Empty);
// Equivalent to:
filters.For<Accommodation>().Add(
projection: _ => _, // Identity projection
filter: (_, _, _, a) => a.Id != Guid.Empty);
EfGraphQLConventions.RegisterInContainer<MyDbContext>(
services,
resolveFilters: _ => filters);
#endregion
}
}