| | | 1 | | using System.Reflection; |
| | | 2 | | |
| | | 3 | | namespace NGql.Core.Builders; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Handles expansion of navigation properties (getter-only computed properties) |
| | | 7 | | /// to their underlying settable properties. |
| | | 8 | | /// </summary> |
| | | 9 | | internal static class NavigationPropertyExpander |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Expands a field name if it's a navigation property (getter-only computed property). |
| | | 13 | | /// For navigation properties, returns all settable properties from the parameter type. |
| | | 14 | | /// Handles nested paths like "profile.name" by only checking the first segment. |
| | | 15 | | /// </summary> |
| | | 16 | | public static HashSet<string> ExpandNavigationProperty(string fieldName, Type? parameterType) |
| | | 17 | | { |
| | 420 | 18 | | var result = new HashSet<string>(StringComparer.OrdinalIgnoreCase); |
| | | 19 | | |
| | 420 | 20 | | if (parameterType == null) |
| | | 21 | | { |
| | 30 | 22 | | result.Add(fieldName); |
| | 30 | 23 | | return result; |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | try |
| | | 27 | | { |
| | | 28 | | // Handle nested paths - only check the first segment for navigation properties |
| | 390 | 29 | | var (firstSegment, remainingPath) = SplitPath(fieldName); |
| | | 30 | | |
| | | 31 | | // Get the property from the type (only first segment) |
| | 390 | 32 | | var property = parameterType.GetProperty(firstSegment, BindingFlags.Public | BindingFlags.Instance); |
| | 375 | 33 | | if (property == null) |
| | | 34 | | { |
| | 108 | 35 | | result.Add(fieldName); |
| | 108 | 36 | | return result; |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | // Check if this is a navigation property (getter-only, no setter) |
| | 267 | 40 | | if (IsNavigationProperty(property)) |
| | | 41 | | { |
| | 51 | 42 | | ExpandNavigationPropertyFields(parameterType, remainingPath, result); |
| | | 43 | | } |
| | | 44 | | else |
| | | 45 | | { |
| | 216 | 46 | | HandleRegularProperty(fieldName, firstSegment, remainingPath, property, result); |
| | | 47 | | } |
| | 267 | 48 | | } |
| | 12 | 49 | | catch (InvalidOperationException) |
| | | 50 | | { |
| | | 51 | | // Reflection failed due to ambiguous or invalid operation |
| | 12 | 52 | | result.Add(fieldName); |
| | 12 | 53 | | } |
| | 3 | 54 | | catch (AmbiguousMatchException) |
| | | 55 | | { |
| | | 56 | | // Multiple matches found for property - use original field name |
| | 3 | 57 | | result.Add(fieldName); |
| | 3 | 58 | | } |
| | | 59 | | |
| | 282 | 60 | | return result; |
| | 108 | 61 | | } |
| | | 62 | | |
| | | 63 | | private static (string FirstSegment, string? RemainingPath) SplitPath(string fieldName) |
| | | 64 | | { |
| | 390 | 65 | | var dotIndex = fieldName.IndexOf('.'); |
| | 390 | 66 | | if (dotIndex > 0) |
| | | 67 | | { |
| | 90 | 68 | | return (fieldName.Substring(0, dotIndex), fieldName.Substring(dotIndex + 1)); |
| | | 69 | | } |
| | 300 | 70 | | return (fieldName, null); |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | private static bool IsNavigationProperty(PropertyInfo property) |
| | | 74 | | { |
| | | 75 | | // Properties surfaced by GetProperty(BindingFlags.Public | BindingFlags.Instance) |
| | | 76 | | // always have a public getter, so GetGetMethod() never returns null in this scope. |
| | 267 | 77 | | return property.SetMethod == null && property.GetGetMethod()!.IsPublic; |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | private static void ExpandNavigationPropertyFields(Type parameterType, string? remainingPath, HashSet<string> result |
| | | 81 | | { |
| | | 82 | | // This is a navigation property - get all SETTABLE properties |
| | 414 | 83 | | foreach (var prop in parameterType.GetProperties(BindingFlags.Public | BindingFlags.Instance)) |
| | | 84 | | { |
| | | 85 | | // Skip navigation properties themselves (getter-only) |
| | 156 | 86 | | if (prop.SetMethod != null) |
| | | 87 | | { |
| | | 88 | | // If there was a remaining path, append it to each expanded property |
| | 105 | 89 | | if (remainingPath != null) |
| | | 90 | | { |
| | 18 | 91 | | result.Add($"{prop.Name}.{remainingPath}"); |
| | | 92 | | } |
| | | 93 | | else |
| | | 94 | | { |
| | 87 | 95 | | result.Add(prop.Name); |
| | | 96 | | } |
| | | 97 | | } |
| | | 98 | | } |
| | 51 | 99 | | } |
| | | 100 | | |
| | | 101 | | private static void HandleRegularProperty( |
| | | 102 | | string fieldName, |
| | | 103 | | string firstSegment, |
| | | 104 | | string? remainingPath, |
| | | 105 | | PropertyInfo property, |
| | | 106 | | HashSet<string> result) |
| | | 107 | | { |
| | | 108 | | // Not a navigation property - check if we need to recurse for nested path |
| | 216 | 109 | | if (remainingPath != null && property.PropertyType != null) |
| | | 110 | | { |
| | | 111 | | // Recurse on the remaining path with the property's type |
| | 57 | 112 | | var nestedExpanded = ExpandNavigationProperty(remainingPath, property.PropertyType); |
| | 288 | 113 | | foreach (var nestedField in nestedExpanded) |
| | | 114 | | { |
| | 87 | 115 | | result.Add($"{firstSegment}.{nestedField}"); |
| | | 116 | | } |
| | | 117 | | } |
| | | 118 | | else |
| | | 119 | | { |
| | | 120 | | // Just return the field name |
| | 159 | 121 | | result.Add(fieldName); |
| | | 122 | | } |
| | 216 | 123 | | } |
| | | 124 | | } |