Add PolymorphicTypenameConverter class to handle __typename deserialization #343
Add PolymorphicTypenameConverter class to handle __typename deserialization #343lanwin wants to merge 3 commits intographql-dotnet:masterfrom
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.
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.
Ok. Better to put a comment into sources.
There was a problem hiding this comment.
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.
So it works only if I query __typename meta-field.
There was a problem hiding this comment.
Right. its designed to help with using Graphql Fragments. No other purpose.
…meConverter.cs Co-authored-by: Ivan Maximov <sungam3r@yandex.ru>
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.
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.
GraphQLTypenameConverter? 😉
There was a problem hiding this comment.
It is already inside GraphQL.* namespace.
|
@lanwin Would you like to continue working on it? |
…meConverter.cs Co-authored-by: Alexander Rose <alex@rose-a.de>
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.