-
Notifications
You must be signed in to change notification settings - Fork 162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Respect the configured serializer in Ui.Playground #1121
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #1121 +/- ##
=======================================
Coverage 93.88% 93.88%
=======================================
Files 49 49
Lines 2370 2370
Branches 421 421
=======================================
Hits 2225 2225
Misses 103 103
Partials 42 42 ☔ View full report in Codecov by Sentry. |
It seems that the purpose of this PR is either to (a) reduce dependencies, or (b) reduce code used in AOT scenarios. I don't think this PR really fulfills the intended purpose. For ASP.NET Core 2.x, Ui.Playground uses the Newtonsoft converter that ASP.NET Core directly references. Utilizing the GraphQL serializer won't reduce dependencies and AOT isn't applicable for this version of ASP.NET Core. Newer versions of ASP.NET Core rely on versions of .NET that include System.Text.Json. So if the purpose is to reduce dependencies, then this doesn't help. Newtonsoft.Json isn't designed for AOT, but ignoring this, you could say that using the same serializer as GraphQL.NET would allow System.Text.Json to be trimmed. But this argument doesn't hold when you look at the PR: the analyzer is going to see that System.Text.Json will be used if There is another possible reason to utilize the configured serializer, and that is if we wanted to allow the user to customize the serialization process. But the serialization format in this case is known and fixed -- for instance, it (probably) wouldn't work if the serializer was configured for pascal case. If you want to support AOT or other serializers, I suggest that you make a protected method to handle serialization. Then in your own implementation you can override the method and rewrite the serialization as desired. For AOT/trimming, the analyzer will see that the base method implementation is never referenced, and will be able to trim out the default serializer. Then if you wish to use the GraphQL.NET serializer, then it should be relatively easy to make a derived class, pull the serializer from DI, and use that within the protected method. Another idea specifically for NativeAOT in .NET 7 and 8 would be add .NET 7 TFM that uses source generation to eliminate reflection. |
if (_services.GetService(typeof(IGraphQLSerializer)) is IGraphQLSerializer serializer) | ||
{ | ||
using var stream = new MemoryStream(); | ||
serializer.WriteAsync(stream, value).Wait(); | ||
|
||
stream.Position = 0; | ||
|
||
using var reader = new StreamReader(stream, Encoding.UTF8); | ||
return reader.ReadToEnd(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI, GraphQL.NET provides a synchronous method as well:
if (_services.GetService(typeof(IGraphQLSerializer)) is IGraphQLSerializer serializer) | |
{ | |
using var stream = new MemoryStream(); | |
serializer.WriteAsync(stream, value).Wait(); | |
stream.Position = 0; | |
using var reader = new StreamReader(stream, Encoding.UTF8); | |
return reader.ReadToEnd(); | |
} | |
var serializer = _services.GetService<IGraphQLTextSerializer>(); | |
if (serializer != null) | |
{ | |
return serializer.Serialize(value); | |
} |
See #1124 for a PR to provide NativeAOT compatibility for this project. |
I think that would work for me. My original idea was to use the registered serializer because, in my case, it's a System.Text.Json serializer with a source generated But the approach in #1124 looks better, so I'm closing this. |
v7.7.1 has been published containing #1124 |
This should allow using AOT with Ui.Playground, but adds an extra dependency to
GraphQL
.