Skip to content

Instantly share code, notes, and snippets.

@MNie
Created May 7, 2018 19:59
Show Gist options
  • Save MNie/d8e8e1e5e3825766050c6f956eb35d53 to your computer and use it in GitHub Desktop.
Save MNie/d8e8e1e5e3825766050c6f956eb35d53 to your computer and use it in GitHub Desktop.
private static QueryTest BuildQuery(string type, TArguments args, IEnumerable<string> fields)
{
var arguments = args == null ? "" : GetArguments(args);
var query = $"{{{type}{arguments}{{{string.Join(' ', fields)}}}}}";
return new QueryTest(query);
}
 
private static string GetArguments(TArguments args)
{
var values = args.GetType()
.GetFields()
.Select(x => new { name = x.Name, value = x.GetValue(args) })
.Where(x => x.value != null)
.Select(x => $"{x.name}:{FormatArgument(x.value)}");
return $"({string.Join(',', values)})";
}
private static string FormatArgument(object value) =>
value is string
? $"\"{value}\""
: $"{value}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment