public void ShouldSerializeErrorExtensionsAccordingToOptions() { var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; options.Converters.Add(new ExecutionErrorJsonConverter(new TestErrorInfoProvider())); var executionResult = new ExecutionResult(); executionResult.AddError(new ExecutionError("An error occurred.")); var writer = new GraphQLSerializer(options); string json = writer.Serialize(executionResult); json.ShouldBeCrossPlatJson(@"{ ""errors"": [{ ""message"":""An error occurred."", ""extensions"": { ""violations"": { ""message"":""An error occurred on field Email."", ""field"":""Email"" } } }] }"); }
public void Bug2839Test() { var schema = new Schema { Query = new TestQuery() }; schema.ReplaceScalar(new MyDateTimeGraphType()); var exec = new DocumentExecuter(); var result = exec.ExecuteAsync(options => { options.Schema = schema; options.Query = "{ test { thisIsAString, thisIsADateTime } }"; } ).Result; var writer = new GraphQLSerializer(options => { options.PropertyNamingPolicy = new MyNamingPolicy(); options.Converters.Add(new MyDateTimeConverter()); }); var str = writer.Serialize(result); str.ShouldBeCrossPlatJson("{\"data\":{\"TEST\":{\"THISISASTRING\":\"String Value\",\"THISISADATETIME\":\"2022-Jan-04\"}}}"); }
private JObject FromObject(object value) { var serializer = new GraphQLSerializer(); var data = serializer.Serialize(value); return(serializer.Deserialize <JObject>(data)); }
public async Task OverrideAutoGraphTypeWithinDI() { // set up service collection and default services var services = new ServiceCollection(); services.AddGraphQL(b => b .AddSchema <Schema1>() .AddAutoClrMappings(true, true)); services.AddTransient <Query1>(); // this test works without the next line of code here services.AddTransient(typeof(AutoRegisteringObjectGraphType <>), typeof(MyAutoGraphType <>)); // run the sample query var provider = services.BuildServiceProvider(); var executer = provider.GetRequiredService <IDocumentExecuter <ISchema> >(); var serializer = new GraphQLSerializer(); var result = await executer.ExecuteAsync(new ExecutionOptions { Query = "{class2{id}}", RequestServices = provider, }).ConfigureAwait(false); var actual = serializer.Serialize(result); // verify the result actual.ShouldBe(@"{""data"":{""class2"":[{""id"":""test""}]}}"); }
public void Bug2839Test() { var schema = new Schema { Query = new TestQuery() }; schema.ReplaceScalar(new MyDateTimeGraphType()); var exec = new DocumentExecuter(); var result = exec.ExecuteAsync(options => { options.Schema = schema; options.Query = "{ test { thisIsAString, thisIsADateTime } }"; } ).Result; var writer = new GraphQLSerializer(options => { options.Converters.Add(new IsoDateTimeConverter() { DateTimeFormat = "yyyy-MMM-dd", Culture = System.Globalization.CultureInfo.InvariantCulture, }); options.ContractResolver = new GraphQLContractResolver(new ErrorInfoProvider()) { NamingStrategy = new KebabCaseNamingStrategy(true, false, false) }; }); var str = writer.Serialize(result); str.ShouldBeCrossPlatJson("{\"data\":{\"test\":{\"this-is-a-string\":\"String Value\",\"this-is-a-date-time\":\"2022-Jan-04\"}}}"); }
public void GraphQLSerializer_Should_Be_Created_With_Same_Options_Multiple_Times() { var options = new JsonSerializerOptions(); var serializer = new GraphQLSerializer(options); options.Converters.Count.ShouldBe(8); _ = serializer.Serialize <object>(new { name = "Tom", age = 42 }); new GraphQLSerializer(options); options.Converters.Count.ShouldBe(8); }
public void DoesNotModifyOptions_2() { var options = new JsonSerializerOptions(); _ = JsonSerializer.Serialize("hello", options); options.Converters.Any(x => x.CanConvert(typeof(Inputs))).ShouldBeFalse(); var serializer = new GraphQLSerializer(options, new ErrorInfoProvider()); _ = serializer.Serialize("hello"); options.Converters.Any(x => x.CanConvert(typeof(Inputs))).ShouldBeFalse(); }