|
| 1 | + |
| 2 | +using System.Collections.Immutable; |
| 3 | + |
| 4 | +namespace HotChocolate.Types; |
| 5 | + |
| 6 | +public static class DirectiveLocationExtensions |
| 7 | +{ |
| 8 | + private static readonly Dictionary<DirectiveLocation, Language.DirectiveLocation> _typeToLang = |
| 9 | + new() |
| 10 | + { |
| 11 | + { |
| 12 | + DirectiveLocation.Query, |
| 13 | + Language.DirectiveLocation.Query |
| 14 | + }, |
| 15 | + { |
| 16 | + DirectiveLocation.Mutation, |
| 17 | + Language.DirectiveLocation.Mutation |
| 18 | + }, |
| 19 | + { |
| 20 | + DirectiveLocation.Subscription, |
| 21 | + Language.DirectiveLocation.Subscription |
| 22 | + }, |
| 23 | + { |
| 24 | + DirectiveLocation.Field, |
| 25 | + Language.DirectiveLocation.Field |
| 26 | + }, |
| 27 | + { |
| 28 | + DirectiveLocation.FragmentDefinition, |
| 29 | + Language.DirectiveLocation.FragmentDefinition |
| 30 | + }, |
| 31 | + { |
| 32 | + DirectiveLocation.FragmentSpread, |
| 33 | + Language.DirectiveLocation.FragmentSpread |
| 34 | + }, |
| 35 | + { |
| 36 | + DirectiveLocation.InlineFragment, |
| 37 | + Language.DirectiveLocation.InlineFragment |
| 38 | + }, |
| 39 | + { |
| 40 | + DirectiveLocation.VariableDefinition, |
| 41 | + Language.DirectiveLocation.VariableDefinition |
| 42 | + }, |
| 43 | + { |
| 44 | + DirectiveLocation.Schema, |
| 45 | + Language.DirectiveLocation.Schema |
| 46 | + }, |
| 47 | + { |
| 48 | + DirectiveLocation.Scalar, |
| 49 | + Language.DirectiveLocation.Scalar |
| 50 | + }, |
| 51 | + { |
| 52 | + DirectiveLocation.Object, |
| 53 | + Language.DirectiveLocation.Object |
| 54 | + }, |
| 55 | + { |
| 56 | + DirectiveLocation.FieldDefinition, |
| 57 | + Language.DirectiveLocation.FieldDefinition |
| 58 | + }, |
| 59 | + { |
| 60 | + DirectiveLocation.ArgumentDefinition, |
| 61 | + Language.DirectiveLocation.ArgumentDefinition |
| 62 | + }, |
| 63 | + { |
| 64 | + DirectiveLocation.Interface, |
| 65 | + Language.DirectiveLocation.Interface |
| 66 | + }, |
| 67 | + { |
| 68 | + DirectiveLocation.Union, |
| 69 | + Language.DirectiveLocation.Union |
| 70 | + }, |
| 71 | + { |
| 72 | + DirectiveLocation.Enum, |
| 73 | + Language.DirectiveLocation.Enum |
| 74 | + }, |
| 75 | + { |
| 76 | + DirectiveLocation.EnumValue, |
| 77 | + Language.DirectiveLocation.EnumValue |
| 78 | + }, |
| 79 | + { |
| 80 | + DirectiveLocation.InputObject, |
| 81 | + Language.DirectiveLocation.InputObject |
| 82 | + }, |
| 83 | + { |
| 84 | + DirectiveLocation.InputFieldDefinition, |
| 85 | + Language.DirectiveLocation.InputFieldDefinition |
| 86 | + }, |
| 87 | + }; |
| 88 | + |
| 89 | + private static readonly Dictionary<Language.DirectiveLocation, DirectiveLocation> _langToType = |
| 90 | + new() |
| 91 | + { |
| 92 | + { |
| 93 | + Language.DirectiveLocation.Query, |
| 94 | + DirectiveLocation.Query |
| 95 | + }, |
| 96 | + { |
| 97 | + Language.DirectiveLocation.Mutation, |
| 98 | + DirectiveLocation.Mutation |
| 99 | + }, |
| 100 | + { |
| 101 | + Language.DirectiveLocation.Subscription, |
| 102 | + DirectiveLocation.Subscription |
| 103 | + }, |
| 104 | + { |
| 105 | + Language.DirectiveLocation.Field, |
| 106 | + DirectiveLocation.Field |
| 107 | + }, |
| 108 | + { |
| 109 | + Language.DirectiveLocation.FragmentDefinition, |
| 110 | + DirectiveLocation.FragmentDefinition |
| 111 | + }, |
| 112 | + { |
| 113 | + Language.DirectiveLocation.FragmentSpread, |
| 114 | + DirectiveLocation.FragmentSpread |
| 115 | + }, |
| 116 | + { |
| 117 | + Language.DirectiveLocation.InlineFragment, |
| 118 | + DirectiveLocation.InlineFragment |
| 119 | + }, |
| 120 | + { |
| 121 | + Language.DirectiveLocation.VariableDefinition, |
| 122 | + DirectiveLocation.VariableDefinition |
| 123 | + }, |
| 124 | + { |
| 125 | + Language.DirectiveLocation.Schema, |
| 126 | + DirectiveLocation.Schema |
| 127 | + }, |
| 128 | + { |
| 129 | + Language.DirectiveLocation.Scalar, |
| 130 | + DirectiveLocation.Scalar |
| 131 | + }, |
| 132 | + { |
| 133 | + Language.DirectiveLocation.Object, |
| 134 | + DirectiveLocation.Object |
| 135 | + }, |
| 136 | + { |
| 137 | + Language.DirectiveLocation.FieldDefinition, |
| 138 | + DirectiveLocation.FieldDefinition |
| 139 | + }, |
| 140 | + { |
| 141 | + Language.DirectiveLocation.ArgumentDefinition, |
| 142 | + DirectiveLocation.ArgumentDefinition |
| 143 | + }, |
| 144 | + { |
| 145 | + Language.DirectiveLocation.Interface, |
| 146 | + DirectiveLocation.Interface |
| 147 | + }, |
| 148 | + { |
| 149 | + Language.DirectiveLocation.Union, |
| 150 | + DirectiveLocation.Union |
| 151 | + }, |
| 152 | + { |
| 153 | + Language.DirectiveLocation.Enum, |
| 154 | + DirectiveLocation.Enum |
| 155 | + }, |
| 156 | + { |
| 157 | + Language.DirectiveLocation.EnumValue, |
| 158 | + DirectiveLocation.EnumValue |
| 159 | + }, |
| 160 | + { |
| 161 | + Language.DirectiveLocation.InputObject, |
| 162 | + DirectiveLocation.InputObject |
| 163 | + }, |
| 164 | + { |
| 165 | + Language.DirectiveLocation.InputFieldDefinition, |
| 166 | + DirectiveLocation.InputFieldDefinition |
| 167 | + }, |
| 168 | + }; |
| 169 | + |
| 170 | + public static DirectiveLocation ToLocation(this Language.DirectiveLocation location) |
| 171 | + => _langToType[location]; |
| 172 | + |
| 173 | + public static IEnumerable<DirectiveLocation> AsEnumerable( |
| 174 | + this DirectiveLocation locations) |
| 175 | + { |
| 176 | + if ((locations & DirectiveLocation.Query) == DirectiveLocation.Query) |
| 177 | + { |
| 178 | + yield return DirectiveLocation.Query; |
| 179 | + } |
| 180 | + |
| 181 | + if ((locations & DirectiveLocation.Mutation) == DirectiveLocation.Mutation) |
| 182 | + { |
| 183 | + yield return DirectiveLocation.Mutation; |
| 184 | + } |
| 185 | + |
| 186 | + if ((locations & DirectiveLocation.Subscription) == DirectiveLocation.Subscription) |
| 187 | + { |
| 188 | + yield return DirectiveLocation.Subscription; |
| 189 | + } |
| 190 | + |
| 191 | + if ((locations & DirectiveLocation.Field) == DirectiveLocation.Field) |
| 192 | + { |
| 193 | + yield return DirectiveLocation.Field; |
| 194 | + } |
| 195 | + |
| 196 | + if ((locations & DirectiveLocation.FragmentDefinition) == |
| 197 | + DirectiveLocation.FragmentDefinition) |
| 198 | + { |
| 199 | + yield return DirectiveLocation.FragmentDefinition; |
| 200 | + } |
| 201 | + |
| 202 | + if ((locations & DirectiveLocation.FragmentSpread) == DirectiveLocation.FragmentSpread) |
| 203 | + { |
| 204 | + yield return DirectiveLocation.FragmentSpread; |
| 205 | + } |
| 206 | + |
| 207 | + if ((locations & DirectiveLocation.InlineFragment) == DirectiveLocation.InlineFragment) |
| 208 | + { |
| 209 | + yield return DirectiveLocation.InlineFragment; |
| 210 | + } |
| 211 | + |
| 212 | + if ((locations & DirectiveLocation.VariableDefinition) == |
| 213 | + DirectiveLocation.VariableDefinition) |
| 214 | + { |
| 215 | + yield return DirectiveLocation.VariableDefinition; |
| 216 | + } |
| 217 | + |
| 218 | + if ((locations & DirectiveLocation.Schema) == DirectiveLocation.Schema) |
| 219 | + { |
| 220 | + yield return DirectiveLocation.Schema; |
| 221 | + } |
| 222 | + |
| 223 | + if ((locations & DirectiveLocation.Scalar) == DirectiveLocation.Scalar) |
| 224 | + { |
| 225 | + yield return DirectiveLocation.Scalar; |
| 226 | + } |
| 227 | + |
| 228 | + if ((locations & DirectiveLocation.Object) == DirectiveLocation.Object) |
| 229 | + { |
| 230 | + yield return DirectiveLocation.Object; |
| 231 | + } |
| 232 | + |
| 233 | + if ((locations & DirectiveLocation.FieldDefinition) == |
| 234 | + DirectiveLocation.FieldDefinition) |
| 235 | + { |
| 236 | + yield return DirectiveLocation.FieldDefinition; |
| 237 | + } |
| 238 | + |
| 239 | + if ((locations & DirectiveLocation.ArgumentDefinition) == |
| 240 | + DirectiveLocation.ArgumentDefinition) |
| 241 | + { |
| 242 | + yield return DirectiveLocation.ArgumentDefinition; |
| 243 | + } |
| 244 | + |
| 245 | + if ((locations & DirectiveLocation.Interface) == DirectiveLocation.Interface) |
| 246 | + { |
| 247 | + yield return DirectiveLocation.Interface; |
| 248 | + } |
| 249 | + |
| 250 | + if ((locations & DirectiveLocation.Union) == DirectiveLocation.Union) |
| 251 | + { |
| 252 | + yield return DirectiveLocation.Union; |
| 253 | + } |
| 254 | + |
| 255 | + if ((locations & DirectiveLocation.Enum) == DirectiveLocation.Enum) |
| 256 | + { |
| 257 | + yield return DirectiveLocation.Enum; |
| 258 | + } |
| 259 | + |
| 260 | + if ((locations & DirectiveLocation.EnumValue) == DirectiveLocation.EnumValue) |
| 261 | + { |
| 262 | + yield return DirectiveLocation.EnumValue; |
| 263 | + } |
| 264 | + |
| 265 | + if ((locations & DirectiveLocation.InputObject) == DirectiveLocation.InputObject) |
| 266 | + { |
| 267 | + yield return DirectiveLocation.InputObject; |
| 268 | + } |
| 269 | + |
| 270 | + if ((locations & DirectiveLocation.InputFieldDefinition) == |
| 271 | + DirectiveLocation.InputFieldDefinition) |
| 272 | + { |
| 273 | + yield return DirectiveLocation.InputFieldDefinition; |
| 274 | + } |
| 275 | + } |
| 276 | + |
| 277 | + public static ImmutableArray<Language.NameNode> ToNameNodes( |
| 278 | + this DirectiveLocation locations) |
| 279 | + => AsEnumerable(locations) |
| 280 | + .Select(t => new Language.NameNode(null, _typeToLang[t].Value)) |
| 281 | + .ToImmutableArray(); |
| 282 | +} |
0 commit comments