|
| 1 | +using System; |
| 2 | +using System.Globalization; |
| 3 | +using System.Linq; |
| 4 | +using EntityGraphQL.Schema; |
| 5 | +using EntityGraphQL.Schema.FieldExtensions; |
| 6 | +using Xunit; |
| 7 | + |
| 8 | +namespace EntityGraphQL.Tests; |
| 9 | + |
| 10 | +public class ArgumentErrorMessageTests |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Stand in for a CLR type (e.g. NodaTime's Instant) that a scalar is registered against while another CLR |
| 14 | + /// type is mapped onto the same GQL type name with AddTypeMapping. |
| 15 | + /// </summary> |
| 16 | + public readonly struct MyInstant(DateTime utc) |
| 17 | + { |
| 18 | + public DateTime Utc { get; } = utc; |
| 19 | + } |
| 20 | + |
| 21 | +#nullable disable |
| 22 | + internal class HistoricalSightingArgs : OffsetArgs |
| 23 | + { |
| 24 | + public RequiredField<Guid> FloorId { get; set; } |
| 25 | + public RequiredField<DateTime> From { get; set; } |
| 26 | + public RequiredField<DateTime> To { get; set; } |
| 27 | + } |
| 28 | + |
| 29 | +#nullable enable |
| 30 | + |
| 31 | + private static SchemaProvider<TestDataContext> BuildSchema(bool isDevelopment = true) |
| 32 | + { |
| 33 | + var schema = SchemaBuilder.FromObject<TestDataContext>(new SchemaProviderOptions { IsDevelopment = isDevelopment }); |
| 34 | + schema |
| 35 | + .Query() |
| 36 | + .AddField("historicalSightings", new HistoricalSightingArgs(), "Historical sightings") |
| 37 | + .Resolve((db, args) => db.Projects.Where(p => p.Id > 0).Skip(args.Skip ?? 0).Take(args.Take ?? 10).ToList()); |
| 38 | + return schema; |
| 39 | + } |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// Registers "Date" against MyInstant and maps DateTime onto the same GQL type name, so a variable declared |
| 43 | + /// as Date! materializes as a MyInstant while the field's argument is a DateTime. |
| 44 | + /// </summary> |
| 45 | + private static SchemaProvider<TestDataContext> BuildSchemaWithMismatchedDateScalar(bool isDevelopment) |
| 46 | + { |
| 47 | + var schema = BuildSchema(isDevelopment); |
| 48 | + schema.AddCustomTypeConverter( |
| 49 | + (TypeConverterTryFrom<string>)( |
| 50 | + (string str, Type toType, ISchemaProvider _, out object? result) => |
| 51 | + { |
| 52 | + if (toType == typeof(MyInstant) || toType == typeof(MyInstant?)) |
| 53 | + { |
| 54 | + result = new MyInstant(DateTime.Parse(str, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal)); |
| 55 | + return true; |
| 56 | + } |
| 57 | + result = null; |
| 58 | + return false; |
| 59 | + } |
| 60 | + ) |
| 61 | + ); |
| 62 | + schema.AddScalarType<MyInstant>("Date", "Date/time scalar"); |
| 63 | + schema.RemoveType("DateTime"); |
| 64 | + schema.AddTypeMapping<DateTime>("Date!"); |
| 65 | + schema.AddTypeMapping<DateTime?>("Date"); |
| 66 | + return schema; |
| 67 | + } |
| 68 | + |
| 69 | + private static QueryRequest HistoricalSightingsQuery() => |
| 70 | + new() |
| 71 | + { |
| 72 | + Query = |
| 73 | + @"query GetHistoricalSightings($floorId: ID!, $from: Date!, $to: Date!) { |
| 74 | + historicalSightings(floorId: $floorId, from: $from, to: $to) { id name } |
| 75 | + }", |
| 76 | + Variables = new QueryVariables |
| 77 | + { |
| 78 | + { "floorId", "cccccccc-bbbb-4444-1111-ccddeeff0033" }, |
| 79 | + { "from", "2026-05-03T22:00:00.000Z" }, |
| 80 | + { "to", "2026-05-03T23:00:00.000Z" }, |
| 81 | + }, |
| 82 | + }; |
| 83 | + |
| 84 | + [Fact] |
| 85 | + public void ArgumentsFromAnArgsClassUseTheSchemaFieldNamer() |
| 86 | + { |
| 87 | + var sdl = BuildSchema().ToGraphQLSchemaString(); |
| 88 | + Assert.Contains("floorId: ID!", sdl); |
| 89 | + Assert.Contains("from: DateTime!", sdl); |
| 90 | + Assert.Contains("to: DateTime!", sdl); |
| 91 | + } |
| 92 | + |
| 93 | + [Fact] |
| 94 | + public void MissingRequiredArgumentFromAnArgsClassReportsTheSchemaArgumentName() |
| 95 | + { |
| 96 | + var gql = new QueryRequest { Query = @"{ historicalSightings { id } }" }; |
| 97 | + |
| 98 | + var result = BuildSchema().ExecuteRequestWithContext(gql, new TestDataContext().FillWithTestData(), null, null); |
| 99 | + |
| 100 | + Assert.NotNull(result.Errors); |
| 101 | + // the dotnet properties are FloorId/From/To - each must be reported once, by its schema name |
| 102 | + Assert.Equal(3, result.Errors.Count); |
| 103 | + Assert.Contains("Field 'historicalSightings' - missing required argument 'floorId'", result.Errors.Select(e => e.Message)); |
| 104 | + Assert.Contains("Field 'historicalSightings' - missing required argument 'from'", result.Errors.Select(e => e.Message)); |
| 105 | + Assert.Contains("Field 'historicalSightings' - missing required argument 'to'", result.Errors.Select(e => e.Message)); |
| 106 | + } |
| 107 | + |
| 108 | + [Fact] |
| 109 | + public void ArgumentBuildFailureCarriesTheCauseInDevelopment() |
| 110 | + { |
| 111 | + var schema = BuildSchemaWithMismatchedDateScalar(isDevelopment: true); |
| 112 | + |
| 113 | + var result = schema.ExecuteRequestWithContext(HistoricalSightingsQuery(), new TestDataContext().FillWithTestData(), null, null); |
| 114 | + |
| 115 | + Assert.NotNull(result.Errors); |
| 116 | + var expectedPrefix = "Variable or value used for argument 'from' does not match argument type 'Date!' on field 'historicalSightings' - "; |
| 117 | + var message = Assert.Single(result.Errors.Select(e => e.Message), m => m.StartsWith(expectedPrefix)); |
| 118 | + // the GQL types alone say nothing - the cause is a MyInstant value that can not become a DateTime |
| 119 | + Assert.Contains("IConvertible", message); |
| 120 | + } |
| 121 | + |
| 122 | + [Fact] |
| 123 | + public void ArgumentBuildFailureHidesTheCauseWhenNotInDevelopment() |
| 124 | + { |
| 125 | + var schema = BuildSchemaWithMismatchedDateScalar(isDevelopment: false); |
| 126 | + |
| 127 | + var result = schema.ExecuteRequestWithContext(HistoricalSightingsQuery(), new TestDataContext().FillWithTestData(), null, null); |
| 128 | + |
| 129 | + Assert.NotNull(result.Errors); |
| 130 | + Assert.Contains("Variable or value used for argument 'from' does not match argument type 'Date!' on field 'historicalSightings'", result.Errors.Select(e => e.Message)); |
| 131 | + } |
| 132 | +} |
0 commit comments