|
1 | 1 | using System;
|
2 | 2 | using System.Collections.Generic;
|
3 | 3 | using System.Linq;
|
| 4 | +using System.Reflection; |
4 | 5 | using System.Text;
|
5 | 6 | using System.Text.RegularExpressions;
|
6 | 7 | using GraphQL.Introspection;
|
7 | 8 | using GraphQL.Types;
|
8 | 9 |
|
9 | 10 | namespace GraphQL.Utilities
|
10 | 11 | {
|
11 |
| - public class SchemaPrinter |
| 12 | + public class SchemaPrinter //TODO: rewrite string concatenations to use buffer ? |
12 | 13 | {
|
13 | 14 | protected SchemaPrinterOptions Options { get; }
|
14 | 15 |
|
@@ -165,7 +166,7 @@ public string PrintType(IGraphType type)
|
165 | 166 | IObjectGraphType objectGraphType => PrintObject(objectGraphType),
|
166 | 167 | IInterfaceGraphType interfaceGraphType => PrintInterface(interfaceGraphType),
|
167 | 168 | UnionGraphType unionGraphType => PrintUnion(unionGraphType),
|
168 |
| - DirectiveGraphType directiveGraphType => PrintDirective(directiveGraphType), |
| 169 | + DirectiveGraphType directiveGraphType => PrintDirective(directiveGraphType), //TODO: DirectiveGraphType does not inherit IGraphType |
169 | 170 | IInputObjectGraphType input => PrintInputObject(input),
|
170 | 171 | _ => throw new InvalidOperationException($"Unknown GraphType '{type.GetType().Name}' with name '{type.Name}'")
|
171 | 172 | };
|
@@ -292,23 +293,59 @@ public string FormatDefaultValue(object value, IGraphType graphType)
|
292 | 293 | {
|
293 | 294 | return graphType switch
|
294 | 295 | {
|
295 |
| - NonNullGraphType nullable => FormatDefaultValue(value, nullable.ResolvedType), |
| 296 | + NonNullGraphType nonNull => FormatDefaultValue(value, nonNull.ResolvedType), |
296 | 297 | ListGraphType list => "[{0}]".ToFormat(string.Join(", ", ((IEnumerable<object>)value).Select(i => FormatDefaultValue(i, list.ResolvedType)))),
|
| 298 | + IInputObjectGraphType input => FormatInputObjectValue(value, input), |
297 | 299 | EnumerationGraphType enumeration => enumeration.Serialize(value).ToString(),
|
298 |
| - _ => value switch |
| 300 | + ScalarGraphType _ => value switch |
299 | 301 | {
|
300 | 302 | string s => $"\"{s}\"",
|
301 | 303 | bool b => b ? "true" : "false",
|
302 |
| - _ => value.ToString() |
303 |
| - } |
| 304 | + _ => value.ToString() // TODO: how to print custom scalars ("") ? |
| 305 | + }, |
| 306 | + _ => throw new NotSupportedException($"Unsopported graph type '{graphType}'") |
304 | 307 | };
|
305 | 308 | }
|
306 | 309 |
|
| 310 | + private string FormatInputObjectValue(object value, IInputObjectGraphType input) |
| 311 | + { |
| 312 | + var sb = new StringBuilder(); |
| 313 | + sb.Append("{ "); |
| 314 | + |
| 315 | + foreach (var field in input.Fields) |
| 316 | + { |
| 317 | + string propertyName = field.GetMetadata<string>(ComplexGraphType<object>.ORIGINAL_EXPRESSION_PROPERTY_NAME) ?? field.Name; |
| 318 | + PropertyInfo propertyInfo; |
| 319 | + |
| 320 | + try |
| 321 | + { |
| 322 | + propertyInfo = value.GetType().GetProperty(propertyName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); |
| 323 | + } |
| 324 | + catch (AmbiguousMatchException) |
| 325 | + { |
| 326 | + propertyInfo = value.GetType().GetProperty(propertyName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly); |
| 327 | + } |
| 328 | + |
| 329 | + object propertyValue = propertyInfo.GetValue(value); |
| 330 | + if (propertyValue != null) |
| 331 | + { |
| 332 | + sb.Append(field.Name) |
| 333 | + .Append(": ") |
| 334 | + .Append(FormatDefaultValue(propertyValue, field.ResolvedType)) |
| 335 | + .Append(", "); |
| 336 | + } |
| 337 | + } |
| 338 | + |
| 339 | + sb.Length -= 2; |
| 340 | + sb.Append(" }"); |
| 341 | + return sb.ToString(); |
| 342 | + } |
| 343 | + |
307 | 344 | public static string ResolveName(IGraphType type)
|
308 | 345 | {
|
309 | 346 | return type switch
|
310 | 347 | {
|
311 |
| - NonNullGraphType nullable => $"{ResolveName(nullable.ResolvedType)}!", |
| 348 | + NonNullGraphType nonNull => $"{ResolveName(nonNull.ResolvedType)}!", |
312 | 349 | ListGraphType list => $"[{ResolveName(list.ResolvedType)}]",
|
313 | 350 | _ => type?.Name
|
314 | 351 | };
|
|
0 commit comments