|
| 1 | + |
| 2 | +using System; |
| 3 | +using System.Linq; |
| 4 | +using GraphQL.Http; |
| 5 | +using GraphQL.StarWars.IoC; |
| 6 | +using GraphQL.Types; |
| 7 | +using GraphQL.Utilities; |
| 8 | +using Shouldly; |
| 9 | +using Xunit; |
| 10 | + |
| 11 | +namespace GraphQL.Tests.Bugs |
| 12 | +{ |
| 13 | + public enum EDataType |
| 14 | + { |
| 15 | + String, |
| 16 | + Int |
| 17 | + } |
| 18 | + |
| 19 | + public class HtTag |
| 20 | + { |
| 21 | + public string Alias { get; set; } |
| 22 | + public EDataType DataType { get; set; } |
| 23 | + public object Value { get; set;} |
| 24 | + } |
| 25 | + |
| 26 | + public class IntegerTag : ObjectGraphType |
| 27 | + { |
| 28 | + public IntegerTag() |
| 29 | + { |
| 30 | + Field<IdGraphType>( |
| 31 | + "alias", |
| 32 | + "the unique alias of the tag" |
| 33 | + ); |
| 34 | + Field<IntGraphType>("value"); |
| 35 | + Interface<TagInterface>(); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + public class StringTag : ObjectGraphType |
| 40 | + { |
| 41 | + public StringTag() |
| 42 | + { |
| 43 | + Field<IdGraphType>( |
| 44 | + "alias", |
| 45 | + "the unique alias of the tag" |
| 46 | + ); |
| 47 | + Field<StringGraphType>("value"); |
| 48 | + Interface<TagInterface>(); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + public class TagInterface : InterfaceGraphType |
| 53 | + { |
| 54 | + public TagInterface( |
| 55 | + IntegerTag integerTag, |
| 56 | + StringTag stringTag) |
| 57 | + { |
| 58 | + Name = "Tag"; |
| 59 | + Description = "A resource which points to a value"; |
| 60 | + |
| 61 | + Field<IdGraphType>( |
| 62 | + "alias", |
| 63 | + "the unique alias of the tag" |
| 64 | + ); |
| 65 | + |
| 66 | + ResolveType = obj => |
| 67 | + { |
| 68 | + var types = this.PossibleTypes.ToList(); |
| 69 | + |
| 70 | + if (obj is HtTag) |
| 71 | + { |
| 72 | + switch ((obj as HtTag).DataType) |
| 73 | + { |
| 74 | + case EDataType.Int: |
| 75 | + return integerTag; |
| 76 | + |
| 77 | + default: |
| 78 | + return stringTag; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + throw new ArgumentOutOfRangeException($"Could not resolve graph type for {obj.GetType().Name}"); |
| 83 | + }; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + public class TagQuery : ObjectGraphType |
| 88 | + { |
| 89 | + public TagQuery() |
| 90 | + { |
| 91 | + Name = "Query"; |
| 92 | + Field<ListGraphType<NonNullGraphType<TagInterface>>>("allTags", resolve: ctx => |
| 93 | + { |
| 94 | + return ctx.RootValue; |
| 95 | + }); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + public class TagSchema : Schema |
| 100 | + { |
| 101 | + public TagSchema(IDependencyResolver resolver) : base(resolver) |
| 102 | + { |
| 103 | + RegisterType<StringTag>(); |
| 104 | + RegisterType<IntegerTag>(); |
| 105 | + Query = resolver.Resolve<TagQuery>(); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + public class ResolveTypeBugTests |
| 110 | + { |
| 111 | + [Fact] |
| 112 | + public void resolve_type_works() |
| 113 | + { |
| 114 | + var container = new SimpleContainer(); |
| 115 | + container.Register<TagQuery>(); |
| 116 | + container.Singleton<StringTag>(); |
| 117 | + container.Singleton<IntegerTag>(); |
| 118 | + container.Register<TagInterface>(); |
| 119 | + container.Singleton<ISchema>(new TagSchema(new FuncDependencyResolver(type => container.Get(type)))); |
| 120 | + |
| 121 | + var schema = container.Get<ISchema>(); |
| 122 | + |
| 123 | + var jsonSchema = new SchemaPrinter(schema).Print(); |
| 124 | + |
| 125 | + var result = schema.Execute(_=> |
| 126 | + { |
| 127 | + _.ExposeExceptions = true; |
| 128 | + _.Root = new [] |
| 129 | + { |
| 130 | + new HtTag { Alias = "one", DataType = EDataType.String, Value = "A Name" }, |
| 131 | + new HtTag { Alias = "two", DataType = EDataType.Int, Value = 123 } |
| 132 | + }; |
| 133 | + _.Query = @" |
| 134 | + { |
| 135 | + allTags { |
| 136 | + __typename |
| 137 | + alias |
| 138 | + ... on StringTag { |
| 139 | + value |
| 140 | + } |
| 141 | + ... on IntegerTag { |
| 142 | + value |
| 143 | + } |
| 144 | + } |
| 145 | + }"; |
| 146 | + }); |
| 147 | + |
| 148 | + result.Replace(Environment.NewLine, "").ShouldBe("{ \"data\": { \"allTags\": [ { \"__typename\": \"StringTag\", \"alias\": \"one\", \"value\": \"A Name\" }, { \"__typename\": \"IntegerTag\", \"alias\": \"two\", \"value\": 123 } ] }}"); |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments