|
1 | 1 | using System; |
2 | | -using System.Collections.Generic; |
| 2 | +using System.Text.Json; |
| 3 | +using System.Text.Json.Nodes; |
3 | 4 | using System.Text.Json.Serialization; |
4 | 5 | using Linq2GraphQL.Client; |
5 | 6 | using Linq2GraphQL.Client.Converters; |
6 | 7 |
|
7 | 8 | namespace StarWars.Client; |
8 | 9 |
|
9 | | -[JsonConverter(typeof(InterfaceConverter<Node__Concrete, Node>))] |
| 10 | +public static class NodeExtentions |
| 11 | +{ |
| 12 | + |
| 13 | + |
| 14 | + [GraphInterface] |
| 15 | + public static Film Film(this Node value) |
| 16 | + { |
| 17 | + if (value.__TypeName == "Film") |
| 18 | + { |
| 19 | + return (Film)value; |
| 20 | + } |
| 21 | + return null; |
| 22 | + } |
| 23 | + |
| 24 | + [GraphInterface] |
| 25 | + public static Person Person(this Node value) |
| 26 | + { |
| 27 | + if (value.__TypeName == "Person") |
| 28 | + { |
| 29 | + return (Person)value; |
| 30 | + } |
| 31 | + return null; |
| 32 | + } |
| 33 | + |
| 34 | + [GraphInterface] |
| 35 | + public static Planet Planet(this Node value) |
| 36 | + { |
| 37 | + if (value.__TypeName == "Planet") |
| 38 | + { |
| 39 | + return (Planet)value; |
| 40 | + } |
| 41 | + return null; |
| 42 | + } |
| 43 | + |
| 44 | + [GraphInterface] |
| 45 | + public static Species Species(this Node value) |
| 46 | + { |
| 47 | + if (value.__TypeName == "Species") |
| 48 | + { |
| 49 | + return (Species)value; |
| 50 | + } |
| 51 | + return null; |
| 52 | + } |
| 53 | + |
| 54 | + [GraphInterface] |
| 55 | + public static Starship Starship(this Node value) |
| 56 | + { |
| 57 | + if (value.__TypeName == "Starship") |
| 58 | + { |
| 59 | + return (Starship)value; |
| 60 | + } |
| 61 | + return null; |
| 62 | + } |
| 63 | + |
| 64 | + [GraphInterface] |
| 65 | + public static Vehicle Vehicle(this Node value) |
| 66 | + { |
| 67 | + if (value.__TypeName == "Vehicle") |
| 68 | + { |
| 69 | + return (Vehicle)value; |
| 70 | + } |
| 71 | + return null; |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | + |
| 76 | +internal class NodeConverter : InterfaceJsonConverter<Node> |
| 77 | +{ |
| 78 | + public override Node Deserialize(string typeName, JsonObject json) => typeName switch |
| 79 | + { |
| 80 | + "Film" => json.Deserialize<Film>(), |
| 81 | + "Person" => json.Deserialize<Person>(), |
| 82 | + "Planet" => json.Deserialize<Planet>(), |
| 83 | + "Species" => json.Deserialize<Species>(), |
| 84 | + "Starship" => json.Deserialize<Starship>(), |
| 85 | + "Vehicle" => json.Deserialize<Vehicle>(), |
| 86 | + _ => json.Deserialize< Node__Concrete>() |
| 87 | + }; |
| 88 | +} |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | +[JsonConverter(typeof(NodeConverter))] |
10 | 94 | public interface Node |
11 | 95 | { |
12 | 96 | [JsonPropertyName("id")] |
|
0 commit comments