2

Gonna love ChatGPT from time to time.

Best version of this method I wrote by hand was 7 lines. Now it's 3.

And it's perfectly readable.

private static DateOnly ParseDateArgument(GraphQLValue? argumentValue, IResolveFieldContext context)
{
return argumentValue switch
{
GraphQLStringValue strVal when DateOnly.TryParse(strVal.Value.ToString(), out var date) => date,
GraphQLVariable varVal when DateOnly.TryParse(context.Variables?.ValueFor(varVal.Name.StringValue)?.ToString(), out var date) => date,
_ => default
};
}

Comments
  • 0
    private static DateOnly ParseDateArgument(GraphQLValue? argumentValue, IResolveFieldContext context) => DateOnly.TryParse((argumentValue as GraphQLStringValue)?.Value?.ToString(), out var date) || DateOnly.TryParse(context.Variables?.ValueFor((argumentValue as GraphQLVariable)?.Name?.StringValue)?.ToString(), out date) ? date : default;
Add Comment