-
-
Notifications
You must be signed in to change notification settings - Fork 722
/
ResolverServiceTests.cs
160 lines (133 loc) · 4.52 KB
/
ResolverServiceTests.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using System.Threading.Tasks;
using HotChocolate.Execution;
using HotChocolate.Tests;
using HotChocolate.Types;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.ObjectPool;
using Snapshooter.Xunit;
using Xunit;
namespace HotChocolate.Resolvers;
public class ResolverServiceTests
{
[Fact]
public async Task AddDefaultService()
{
Snapshot.FullName();
await new ServiceCollection()
.AddSingleton<SayHelloService>()
.AddGraphQL()
.AddQueryType<QueryService>()
.ExecuteRequestAsync("{ sayHello }")
.MatchSnapshotAsync();
}
[Fact]
public async Task AddPooledService()
{
Snapshot.FullName();
var pooledService = new SayHelloServicePool();
await new ServiceCollection()
.AddSingleton<ObjectPool<SayHelloService>>(pooledService)
.AddGraphQL()
.AddQueryType<QueryPooledService>()
.ModifyRequestOptions(o => o.IncludeExceptionDetails = true)
.ExecuteRequestAsync("{ sayHello }")
.MatchSnapshotAsync();
Assert.True(pooledService.GetService);
Assert.True(pooledService.ReturnService);
}
[Fact]
public async Task AddSynchronizedService()
{
Snapshot.FullName();
var executor =
await new ServiceCollection()
.AddSingleton<SayHelloService>()
.AddGraphQL()
.AddQueryType<QuerySynchronizedService>()
.BuildRequestExecutorAsync();
Assert.False(executor.Schema.QueryType.Fields["sayHello"].IsParallelExecutable);
await executor.ExecuteAsync("{ sayHello }").MatchSnapshotAsync();
}
[Fact]
public async Task AddResolverService()
{
Snapshot.FullName();
var executor =
await new ServiceCollection()
.AddSingleton<SayHelloService>()
.AddGraphQL()
.AddQueryType<QueryResolverService>(x => x.Name("Query"))
.ModifyRequestOptions(o => o.IncludeExceptionDetails = true)
.MapField(
new FieldReference("Query", "sayHello"),
next => async context =>
{
await next(context);
Assert.True(
context.LocalContextData.ContainsKey(
WellKnownMiddleware.ResolverServiceScope));
})
.BuildRequestExecutorAsync();
await executor.ExecuteAsync("{ sayHello }").MatchSnapshotAsync();
}
[Fact]
public async Task AddResolverService_2()
{
Snapshot.FullName();
var executor =
await new ServiceCollection()
.AddSingleton<SayHelloService>()
.AddGraphQL()
.AddQueryType<QueryType>()
.ModifyRequestOptions(o => o.IncludeExceptionDetails = true)
.BuildRequestExecutorAsync();
await executor.ExecuteAsync("{ sayHello }").MatchSnapshotAsync();
}
public class QueryType : ObjectType
{
protected override void Configure(IObjectTypeDescriptor descriptor)
{
descriptor
.Field("sayHello")
.ResolveWith<QueryResolverService>(r => r.SayHello(default!));
}
}
public class SayHelloService
{
public string SayHello() => "Hello";
}
public class QueryService
{
public string SayHello([Service] SayHelloService service)
=> service.SayHello();
}
public class QueryPooledService
{
public string SayHello([Service(ServiceKind.Pooled)] SayHelloService service)
=> service.SayHello();
}
public class QuerySynchronizedService
{
public string SayHello([Service(ServiceKind.Synchronized)] SayHelloService service)
=> service.SayHello();
}
public class QueryResolverService
{
public string SayHello([Service(ServiceKind.Resolver)] SayHelloService service)
=> service.SayHello();
}
public class SayHelloServicePool : ObjectPool<SayHelloService>
{
public bool GetService { get; private set; }
public bool ReturnService { get; private set; }
public override SayHelloService Get()
{
GetService = true;
return new SayHelloService();
}
public override void Return(SayHelloService obj)
{
ReturnService = true;
}
}
}