|
| 1 | +using GraphQL.Language.AST; |
| 2 | +using GraphQL.Types; |
| 3 | +using Our.Umbraco.GraphQL.Filters; |
| 4 | +using Our.Umbraco.GraphQL.Models; |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Linq; |
| 8 | +using System.Threading.Tasks; |
| 9 | + |
| 10 | +namespace Our.Umbraco.GraphQL |
| 11 | +{ |
| 12 | + internal static class FilterUtils |
| 13 | + { |
| 14 | + public static IEnumerable<TSource> Filter<TSource, TParent>( |
| 15 | + this IEnumerable<TSource> source, |
| 16 | + ResolveFieldContext<TParent> context) |
| 17 | + { |
| 18 | + var orderByArg = context.GetArgument<IEnumerable<object>>("orderBy")?.OfType<OrderBy>(); |
| 19 | + |
| 20 | + if (context.Arguments.TryGetValue("filter", out object filterArg) && filterArg != null) |
| 21 | + { |
| 22 | + var rootFilter = ((IDictionary<string, object>)filterArg).First(); |
| 23 | + var filterType = (IComplexGraphType)context.FieldDefinition.Arguments.Find("filter").ResolvedType; |
| 24 | + var filter = ResolveFilter(filterType, rootFilter, context); |
| 25 | + |
| 26 | + source = source.Where(x => filter.IsSatisfiedBy(x)); |
| 27 | + } |
| 28 | + if (orderByArg != null) |
| 29 | + { |
| 30 | + foreach (var order in orderByArg) |
| 31 | + { |
| 32 | + FieldType fieldType = context.ParentType.Fields.Single(x => x.Name == order.Field); |
| 33 | + |
| 34 | + object KeySelector(TSource item) |
| 35 | + { |
| 36 | + var path = (context.Path ?? new[] { "order" }).Concat(new[] { fieldType.Name }); |
| 37 | + var value = fieldType.Resolver.Resolve(new ResolveFieldContext |
| 38 | + { |
| 39 | + FieldName = order.Field, |
| 40 | + FieldAst = new Field(null, new NameNode(order.Field)), |
| 41 | + FieldDefinition = fieldType, |
| 42 | + ParentType = context.ParentType, |
| 43 | + Source = item, |
| 44 | + Schema = context.Schema, |
| 45 | + Document = context.Document, |
| 46 | + Fragments = context.Fragments, |
| 47 | + RootValue = context.RootValue, |
| 48 | + UserContext = context.UserContext, |
| 49 | + Operation = context.Operation, |
| 50 | + Variables = context.Variables, |
| 51 | + CancellationToken = context.CancellationToken, |
| 52 | + Metrics = context.Metrics, |
| 53 | + Errors = context.Errors, |
| 54 | + Path = path |
| 55 | + }); |
| 56 | + if (value is Task<object> task) |
| 57 | + { |
| 58 | + return task.Result; |
| 59 | + } |
| 60 | + return value; |
| 61 | + } |
| 62 | + |
| 63 | + if (source is IOrderedEnumerable<TSource> ordered) |
| 64 | + { |
| 65 | + source = order.Order == SortOrder.Ascending |
| 66 | + ? ordered.ThenBy(KeySelector) |
| 67 | + : ordered.ThenByDescending(KeySelector); |
| 68 | + } |
| 69 | + else |
| 70 | + { |
| 71 | + source = order.Order == SortOrder.Ascending |
| 72 | + ? source.OrderBy(KeySelector) |
| 73 | + : source.OrderByDescending(KeySelector); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + } |
| 78 | + return source; |
| 79 | + } |
| 80 | + |
| 81 | + internal static IFilter ResolveFilter<TParentType>( |
| 82 | + this IComplexGraphType filterType, |
| 83 | + KeyValuePair<string, object> value, |
| 84 | + ResolveFieldContext<TParentType> context) |
| 85 | + { |
| 86 | + var path = (context.Path ?? new[] { "filter" }).Concat(new[] { value.Key }); |
| 87 | + var field = filterType.Fields.First(x => x.Name == value.Key); |
| 88 | + |
| 89 | + return (IFilter)field.Resolver.Resolve( |
| 90 | + new ResolveFieldContext |
| 91 | + { |
| 92 | + FieldName = field.Name, |
| 93 | + FieldAst = new Field(null, new NameNode(field.Name)), |
| 94 | + FieldDefinition = field, |
| 95 | + ParentType = context.ParentType, |
| 96 | + Source = value.Value, |
| 97 | + Schema = context.Schema, |
| 98 | + Document = context.Document, |
| 99 | + Fragments = context.Fragments, |
| 100 | + RootValue = context.RootValue, |
| 101 | + UserContext = context.UserContext, |
| 102 | + Operation = context.Operation, |
| 103 | + Variables = context.Variables, |
| 104 | + CancellationToken = context.CancellationToken, |
| 105 | + Metrics = context.Metrics, |
| 106 | + Errors = context.Errors, |
| 107 | + Path = path, |
| 108 | + } |
| 109 | + ); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments