graphql-client icon indicating copy to clipboard operation
graphql-client copied to clipboard

Add PolymorphicTypenameConverter class to handle __typename deserialization

Open lanwin opened this issue 3 years ago • 5 comments

Attached a helper class I found very useful to handle fragments and __typename deserialization:

Usage:

            [JsonConverter(typeof(DiscriminateEvent))]
            public Event Event { get; set; }

            class DiscriminateEvent : PolymorphicTypenameConverter<Event>
            {
                protected override Type Descriminator(string typename) =>
                    typename switch
                    {
                        "EventA" => typeof(EventA),
                        "EventB" => typeof(EventB),
                        _ => typeof(Event)
                    };
            }

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.

lanwin avatar Apr 26 '21 07:04 lanwin

@lanwin Would you like to continue working on it?

sungam3r avatar Jul 21 '22 04:07 sungam3r

@lanwin Would you like to continue working on it?

Yes but I am not sure yet whats expected from here.

lanwin avatar Aug 29 '22 10:08 lanwin

@rose-a ?

sungam3r avatar Sep 19 '22 14:09 sungam3r

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...

StevenDewey avatar Oct 18 '23 13:10 StevenDewey

Works like a charm when using unions for error handling. Would like to see it merged into the library. Cheers!

Keyslam avatar Jan 15 '24 09:01 Keyslam