-
Notifications
You must be signed in to change notification settings - Fork 134
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
Add PolymorphicTypenameConverter class to handle __typename deserialization #343
base: master
Are you sure you want to change the base?
Conversation
|
||
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
var clone = reader; // cause its a struct |
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.
?
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.
It was not obvious for me too. But the Utf8JsonReader is a struct and many contains pointers to data to read. So to get the __typename we need to read ahead the data and then move back and deserialize the object. But the reader can only read forward. So we make use of the struct and clone it by assign it to another variable. So both clone and reader and read to differenc positions from each others.
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.
Ok. Better to put a comment into sources.
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.
By itself, this class without descendants carries little benefit. You can make at least one descendant, which discovers all the classes in the declaring assembly.
src/GraphQL.Client.Serializer.SystemTextJson/PolymorphicTypenameConverter.cs
Outdated
Show resolved
Hide resolved
if(clone.TokenType == JsonTokenType.StartObject) | ||
clone.Read(); | ||
if(clone.TokenType != JsonTokenType.PropertyName || clone.GetString() != "__typename") |
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.
So it works only if I query __typename
meta-field.
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.
Right. its designed to help with using Graphql Fragments. No other purpose.
…meConverter.cs Co-authored-by: Ivan Maximov <[email protected]>
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.
Hi @lanwin, thanks for your contribution!
I think this can be very handy if you need to work with GraphQL interfaces and union types.
We'd need a similar converter for Newtonsoft.Json, too.
src/GraphQL.Client.Serializer.SystemTextJson/PolymorphicTypenameConverter.cs
Outdated
Show resolved
Hide resolved
|
||
namespace GraphQL.Client.Serializer.SystemTextJson | ||
{ | ||
public abstract class PolymorphicTypenameConverter<T> : JsonConverter<T> |
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.
Perhaps we can find a class name which makes the use case for this converter a little bit more obvious (in that it's tied to the builtin GraphQL __typename
field).
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.
FromTypenameConverter
?
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.
GraphQLTypenameConverter
? 😉
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.
It is already inside GraphQL.*
namespace.
@lanwin Would you like to continue working on it? |
…meConverter.cs Co-authored-by: Alexander Rose <[email protected]>
Yes but I am not sure yet whats expected from here. |
@rose-a ? |
It would be great to see this or something like this supported! It is a great idea, though it looks like it has lost some momentum over the past year... |
Works like a charm when using unions for error handling. Would like to see it merged into the library. Cheers! |
Attached a helper class I found very useful to handle fragments and __typename deserialization:
Usage:
I think there should be also a example within the readme cause its not obvious how to use it.
But for now I want to know what you think first and if its a nice addition.