Created
May 29, 2018 21:59
-
-
Save MNie/6bd8fc7f851ad865544dcaa8b8152b73 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static string ToString(IContainer container, IFieldType field, Type toResolve, int deep) => | |
toResolve.IsAssignableTo<IComplexGraphType>() | |
? ToStringAsField(container, field, toResolve, deep, FieldFormat) | |
: ToStringAsUnion(container, field, toResolve, deep); | |
private static string FormatWithPropertiesForUnions(IContainer container, IFieldType field, Type toResolve, int deep) | |
{ | |
var unionTypes = (UnionGraphType) container.Resolve(toResolve); | |
var unions = unionTypes.Types | |
.Select(type => FormatAsField(container, field, type, deep, UnionFormat)); | |
return string.Join("", unions); | |
} | |
private static string FormatAsField( | |
IContainer container, | |
IFieldType field, | |
Type toResolve, | |
int deep, | |
Func<string, string, string, string> format | |
) | |
{ | |
var type = (IComplexGraphType) container.Resolve(toResolve); | |
var fields = string.Join(' ', type.Fields.Select(y => ToString(y, container, deep 1))); | |
return string.IsNullOrWhiteSpace(fields) | |
? string.Empty | |
: format(field.Name, type.Name, fields); | |
} | |
private static string FieldFormat(string fieldName, string _, string fields) => | |
$"{fieldName}{{{fields}}}"; | |
private static string UnionFormat(string fieldName, string unionName, string fields) => | |
$"{fieldName}{{ ... on {unionName}{{{fields}}}}}"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment