-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnnotationsTest.cs
143 lines (120 loc) Β· 3.93 KB
/
AnnotationsTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System.Collections.Generic;
using System.Linq;
using HotChocolate.Execution.Configuration;
using HotChocolate.Types;
using Microsoft.Extensions.DependencyInjection;
namespace HotChocolate.Extensions.ApolloSubgraph.Integration.Reviews;
public class AnnotationsTest : ReviewsTestBase
{
protected override IRequestExecutorBuilder CreateRequestExecutorBuilder()
{
return new ServiceCollection()
.AddSingleton<UserRepository>()
.AddSingleton<ReviewRepository>()
.AddGraphQL()
.AddApolloSubgraph()
.AddQueryType()
.AddType<Review>()
.AddType<User>()
.AddType<Product>();
}
public sealed record Review
{
public Review(string id, string authorId, Product product, string body)
{
Id = id;
AuthorId = authorId;
Product = product;
Body = body;
}
[GraphQLKey]
[GraphQLType(typeof(IdType))]
public string Id { get; }
public string AuthorId { get; }
[GraphQLProvides("username")]
public Product Product { get;}
public string Body { get; }
public static Review? ResolveEntity(IEntityResolverContext context)
{
var reviews = context.Service<ReviewRepository>();
return reviews.FindById(context.Representation.GetValue<string>("id"));
}
}
[GraphQLExtends]
public sealed record User
{
public User(string id, string username)
{
Id = id;
Username = username;
}
[GraphQLKey]
[GraphQLExternal]
[GraphQLType(typeof(IdType))]
public string Id { get; }
[GraphQLExternal]
public string Username { get; }
public IReadOnlyList<Review> GetReviews([Service] ReviewRepository reviews)
{
return reviews.GetByAuthorId(Id);
}
public static User? ResolveEntity(IEntityResolverContext context)
{
var users = context.Service<UserRepository>();
return users.FindById(context.Representation.GetValue<string>("id"));
}
}
[GraphQLExtends]
public sealed record Product
{
public Product(string upc)
{
Upc = upc;
}
[GraphQLKey]
[GraphQLExternal]
public string Upc { get; }
public IReadOnlyList<Review> GetReviews([Service] ReviewRepository reviews)
{
return reviews.GetByProductUpc(Upc);
}
public static Product ResolveEntity(IEntityResolverContext context)
{
return new Product(context.Representation.GetValue<string>("upc"));
}
}
public sealed class ReviewRepository
{
private static readonly Review[] s_reviews =
{
new(id: "1", authorId: "1", new Product(upc: "1"), body: "Love it!"),
new(id: "2", authorId: "1", new Product(upc: "2"), body: "Too expensive."),
new(id: "3", authorId: "2", new Product(upc: "3"), body: "Could be better."),
new(id: "4", authorId: "2", new Product(upc: "1"), body: "Prefer something better.")
};
public IReadOnlyList<Review> GetByAuthorId(string id)
{
return s_reviews.Where(x => x.AuthorId == id).ToList();
}
public IReadOnlyList<Review> GetByProductUpc(string upc)
{
return s_reviews.Where(x => x.Product.Upc == upc).ToList();
}
public Review? FindById(string id)
{
return s_reviews.SingleOrDefault(x => x.Id == id);
}
}
public sealed class UserRepository
{
private static readonly User[] s_users =
{
new(id: "1", username: "@ada"),
new(id: "2", username: "@complete")
};
public User? FindById(string? id)
{
return s_users.SingleOrDefault(x => x.Id == id);
}
}
}