System.Text.Json source generator configuration for AOT #1179
-
Hi, in the README file I've found that GraphQL Server supports native AOT, so I've created a simple project to test it. When I tried to run it, I've got a lot of errors related to missing types. Based on the errors I had to add below types to [JsonSerializable(typeof(ExecutionResult))]
[JsonSerializable(typeof(IDictionary<string, object>))]
[JsonSerializable(typeof(List<string>))]
[JsonSerializable(typeof(IList<GraphQLRequest>))]
[JsonSerializable(typeof(IEnumerable))]
[JsonSerializable(typeof(OperationMessage))]
[JsonSerializable(typeof(JsonElement?))]
internal partial class AppJsonSerializerContext : JsonSerializerContext; Is there some documented list of types or solution which provides all required types for using System.Collections;
using System.Text.Json;
using System.Text.Json.Serialization;
using GraphQL;
using GraphQL.Transport;
using GraphQL.Types;
var builder = WebApplication.CreateSlimBuilder(args);
var services = builder.Services;
services.AddGraphQL(configure =>
{
configure.AddSchema<MySchema>();
configure.AddSystemTextJson(options => options.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default));
});
var app = builder.Build();
app.UseWebSockets();
app.UseGraphQL();
app.Run();
[JsonSerializable(typeof(ExecutionResult))]
[JsonSerializable(typeof(IDictionary<string, object>))]
[JsonSerializable(typeof(List<string>))]
[JsonSerializable(typeof(IList<GraphQLRequest>))]
[JsonSerializable(typeof(IEnumerable))]
[JsonSerializable(typeof(OperationMessage))]
[JsonSerializable(typeof(JsonElement?))]
internal partial class AppJsonSerializerContext : JsonSerializerContext;
public class Query : ObjectGraphType
{
public Query()
{
Field<string>("hello")
.Resolve(_ => "Hello, world!");
}
}
public class MySchema : Schema
{
public MySchema(IServiceProvider serviceProvider) : base(serviceProvider)
{
Query = new Query();
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
While GraphQL.NET Server “supports” AOT — meaning it doesn’t produce any AOT warnings during compilation — GraphQL.NET itself (which includes the serializer) is far from fully supporting AOT as you’ve seen. There is two samples application within the GraphQL.NET repository which uses AOT, but they do not use ASP.Net or GraphQL.NET Server. I can review your question further and probably add a sample project. |
Beta Was this translation helpful? Give feedback.
-
Thank you for the answer! I will take a look on the sample project and try to create more advanced scenarios. In case of any issues I will report an issue. |
Beta Was this translation helpful? Give feedback.
As for the source generator, GraphQL.NET includes JsonConverters which will be used for deserializing variables or serializing results. You will need to enable reflection-based serialization in order for the project to compile, but there will be no reflection-based serialization actually occurring. Perhaps in the future we can update the GraphQL.NET serializers to eliminate this problem/warning.
Also, GraphQL.NET was missing a number of attributes which were preventing proper trim analysis of the project. This has been updated in GraphQL.NET 8.3.0. I suggest you update to GraphQL.NET 8.3.0 to solve many of these trimming errors.
Finally, I've added a sample project within PR #1180 demonst…