|
1 | 1 | using HotChocolate.Transport; |
2 | 2 | using HotChocolate.Transport.Http; |
| 3 | +using HotChocolate.Types; |
3 | 4 | using HotChocolate.Types.Composite; |
4 | 5 | using Microsoft.Extensions.DependencyInjection; |
5 | 6 |
|
@@ -47,6 +48,96 @@ public async Task Fetch_From_Nested_Internal_Lookup() |
47 | 48 | await MatchSnapshotAsync(gateway, request, result); |
48 | 49 | } |
49 | 50 |
|
| 51 | + [Fact] |
| 52 | + public async Task Fetch_OneOf_Lookup_With_Name() |
| 53 | + { |
| 54 | + // arrange |
| 55 | + using var server1 = CreateSourceSchema( |
| 56 | + "a", |
| 57 | + b => b.AddQueryType<OneOfLookups.SourceSchema1.Query>()); |
| 58 | + |
| 59 | + using var server2 = CreateSourceSchema( |
| 60 | + "b", |
| 61 | + b => b.AddQueryType<OneOfLookups.SourceSchema2.Query>()); |
| 62 | + |
| 63 | + using var server3 = CreateSourceSchema( |
| 64 | + "c", |
| 65 | + b => b.AddQueryType<OneOfLookups.SourceSchema3.Query>()); |
| 66 | + |
| 67 | + using var gateway = await CreateCompositeSchemaAsync( |
| 68 | + [ |
| 69 | + ("a", server1), |
| 70 | + ("b", server2), |
| 71 | + ("c", server3) |
| 72 | + ]); |
| 73 | + |
| 74 | + // act |
| 75 | + using var client = GraphQLHttpClient.Create(gateway.CreateClient()); |
| 76 | + |
| 77 | + var request = new OperationRequest( |
| 78 | + """ |
| 79 | + { |
| 80 | + topAuthor { |
| 81 | + id |
| 82 | + name |
| 83 | + } |
| 84 | + } |
| 85 | + """); |
| 86 | + |
| 87 | + using var result = await client.PostAsync( |
| 88 | + request, |
| 89 | + new Uri("http://localhost:5000/graphql")); |
| 90 | + |
| 91 | + // assert |
| 92 | + await MatchSnapshotAsync(gateway, request, result); |
| 93 | + } |
| 94 | + |
| 95 | + [Fact] |
| 96 | + public async Task Fetch_OneOf_Lookup_With_Id() |
| 97 | + { |
| 98 | + // arrange |
| 99 | + using var server1 = CreateSourceSchema( |
| 100 | + "a", |
| 101 | + b => b.AddQueryType<OneOfLookups.SourceSchema1.Query>()); |
| 102 | + |
| 103 | + using var server2 = CreateSourceSchema( |
| 104 | + "b", |
| 105 | + b => b.AddQueryType<OneOfLookups.SourceSchema2.Query>()); |
| 106 | + |
| 107 | + using var server3 = CreateSourceSchema( |
| 108 | + "c", |
| 109 | + b => b.AddQueryType<OneOfLookups.SourceSchema3.Query>()); |
| 110 | + |
| 111 | + using var gateway = await CreateCompositeSchemaAsync( |
| 112 | + [ |
| 113 | + ("a", server1), |
| 114 | + ("b", server2), |
| 115 | + ("c", server3) |
| 116 | + ]); |
| 117 | + |
| 118 | + // act |
| 119 | + using var client = GraphQLHttpClient.Create(gateway.CreateClient()); |
| 120 | + |
| 121 | + var request = new OperationRequest( |
| 122 | + """ |
| 123 | + { |
| 124 | + books { |
| 125 | + author { |
| 126 | + id |
| 127 | + name |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + """); |
| 132 | + |
| 133 | + using var result = await client.PostAsync( |
| 134 | + request, |
| 135 | + new Uri("http://localhost:5000/graphql")); |
| 136 | + |
| 137 | + // assert |
| 138 | + await MatchSnapshotAsync(gateway, request, result); |
| 139 | + } |
| 140 | + |
50 | 141 | public static class NestedLookups |
51 | 142 | { |
52 | 143 | public static class SourceSchema1 |
@@ -103,4 +194,82 @@ public string IdAndTitle([Require] string title) |
103 | 194 | } |
104 | 195 | } |
105 | 196 | } |
| 197 | + |
| 198 | + public static class OneOfLookups |
| 199 | + { |
| 200 | + public static class SourceSchema1 |
| 201 | + { |
| 202 | + public record Book(int Id, string Title, [property: Shareable] Author Author); |
| 203 | + |
| 204 | + [EntityKey("id")] |
| 205 | + public record Author(int Id); |
| 206 | + |
| 207 | + public class Query |
| 208 | + { |
| 209 | + private readonly OrderedDictionary<int, Book> _books = |
| 210 | + new() |
| 211 | + { |
| 212 | + [1] = new Book(1, "C# in Depth", new Author(1)), |
| 213 | + [2] = new Book(2, "The Lord of the Rings", new Author(2)), |
| 214 | + [3] = new Book(3, "The Hobbit", new Author(2)), |
| 215 | + [4] = new Book(4, "The Silmarillion", new Author(2)) |
| 216 | + }; |
| 217 | + |
| 218 | + public IEnumerable<Book> GetBooks() |
| 219 | + => _books.Values; |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + public static class SourceSchema2 |
| 224 | + { |
| 225 | + public record Author(int Id, string Name); |
| 226 | + |
| 227 | + public class Query |
| 228 | + { |
| 229 | + [Internal] |
| 230 | + public InternalLookups Lookups { get; } = new(); |
| 231 | + } |
| 232 | + |
| 233 | + [Internal] |
| 234 | + public class InternalLookups |
| 235 | + { |
| 236 | + private readonly OrderedDictionary<int, Author> _authors = new() |
| 237 | + { |
| 238 | + [1] = new Author(1, "Jon Skeet"), |
| 239 | + [2] = new Author(2, "JRR Tolkien") |
| 240 | + }; |
| 241 | + |
| 242 | + [Lookup, Internal] |
| 243 | + public Author GetAuthor([Is("{ id } | { name }")] AuthorByInput by) |
| 244 | + { |
| 245 | + if (by.Id is not null) |
| 246 | + { |
| 247 | + return _authors[by.Id.Value]; |
| 248 | + } |
| 249 | + |
| 250 | + return _authors.Values.First(a => a.Name == by.Name); |
| 251 | + } |
| 252 | + } |
| 253 | + |
| 254 | + public record Book(int Id, [property: Shareable] Author Author) |
| 255 | + { |
| 256 | + public string IdAndTitle([Require] string title) |
| 257 | + => $"{Id} - {title}"; |
| 258 | + } |
| 259 | + |
| 260 | + [OneOf] |
| 261 | + public record AuthorByInput(int? Id, string? Name); |
| 262 | + } |
| 263 | + |
| 264 | + public static class SourceSchema3 |
| 265 | + { |
| 266 | + public class Query |
| 267 | + { |
| 268 | + public Author GetTopAuthor() |
| 269 | + => new("Jon Skeet"); |
| 270 | + } |
| 271 | + |
| 272 | + public record Author([property: Shareable] string Name); |
| 273 | + } |
| 274 | + } |
106 | 275 | } |
0 commit comments