-
-
Notifications
You must be signed in to change notification settings - Fork 722
/
TypeConverterTests.cs
211 lines (187 loc) · 6.46 KB
/
TypeConverterTests.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using HotChocolate.Tests;
using HotChocolate.Types;
using HotChocolate.Utilities;
using Microsoft.Extensions.DependencyInjection;
using Snapshooter.Xunit;
using Xunit;
using static HotChocolate.Tests.TestHelper;
namespace HotChocolate.Execution.Integration.TypeConverter
{
public class TypeConverterTests
{
[Fact]
public async Task VariablesAreCoercedToTypesOtherThanTheDefinedClrTypes()
{
Snapshot.FullName();
await ExpectValid(
@"
query foo($a: FooInput) {
foo(foo: $a) {
id
time
number
}
}",
request: r => r.AddVariableValue("a",
new Dictionary<string, object>
{
{"id", "934b987bc0d842bbabfd8a3b3f8b476e"},
{"time", "2018-05-29T01:00Z"},
{"number", (byte)123}
}),
configure: c => c.AddQueryType<Query>())
.MatchSnapshotAsync();
}
[Fact]
public async Task VariableIsCoercedToTypesOtherThanTheDefinedClrTypes()
{
Snapshot.FullName();
await ExpectValid(
@"
query foo($time: DateTime) {
time(time: $time)
}",
request: r => r.AddVariableValue("time", "2018-05-29T01:00Z"),
configure: c => c.AddQueryType<QueryType>())
.MatchSnapshotAsync();
}
[Fact]
public async Task VariableIsNotSerializedAndMustBeConvertedToClrType()
{
Snapshot.FullName();
var time = new DateTime(2018, 01, 01, 12, 10, 10, DateTimeKind.Utc);
await ExpectValid(
@"
query foo($time: DateTime) {
time(time: $time)
}",
request: r => r.AddVariableValue("time", time),
configure: c => c.AddQueryType<QueryType>())
.MatchSnapshotAsync();
}
[Fact]
public async Task VariableIsPartlyNotSerializedAndMustBeConvertedToClrType()
{
Snapshot.FullName();
await ExpectValid(
@"
query foo($a: FooInput) {
foo(foo: $a) {
id
time
number
}
}",
request: r => r.AddVariableValue("a",
new Dictionary<string, object>
{
{"id", "934b987bc0d842bbabfd8a3b3f8b476e"},
{"time", "2018-05-29T01:00Z"},
{"number", (byte)123}
}),
configure: c => c.AddQueryType<QueryType>())
.MatchSnapshotAsync();
}
[Fact]
public void Register_TypeConverter_As_Service()
{
// arrange
var services = new ServiceCollection();
// act
services.AddGraphQL().AddTypeConverter<IntToStringConverter>();
// assert
ITypeConverter conversion =
services.BuildServiceProvider().GetService<ITypeConverter>();
Assert.Equal("123", conversion.Convert<int, string>(123));
}
[Fact]
public void Register_DelegateTypeConverter_As_Service()
{
// arrange
var services = new ServiceCollection();
// act
services.AddGraphQL().AddTypeConverter<int, string>(
from => from.ToString() + "_123");
// assert
ITypeConverter conversion =
services.BuildServiceProvider().GetService<ITypeConverter>();
Assert.Equal("123_123", conversion.Convert<int, string>(123));
}
[Fact]
public void Register_Multiple_TypeConverters_As_Service()
{
// arrange
var services = new ServiceCollection();
services.AddGraphQLCore();
// act
services.AddTypeConverter<int, string>(
from => from.ToString() + "_123");
services.AddTypeConverter<char, string>(
from => from + "_123");
// assert
ITypeConverter conversion =
services.BuildServiceProvider().GetService<ITypeConverter>();
Assert.Equal("123_123", conversion.Convert<int, string>(123));
Assert.Equal("a_123", conversion.Convert<char, string>('a'));
}
[Fact]
public void Convert_Null_To_Value_Type_Default()
{
Guid empty = TypeConverterExtensions.Convert<object, Guid>(
DefaultTypeConverter.Default,
null);
Assert.Equal(Guid.Empty, empty);
}
public class QueryType
: ObjectType<Query>
{
protected override void Configure(
IObjectTypeDescriptor<Query> descriptor)
{
descriptor.Field(t => t.GetTime(default))
.Argument("time", a => a.Type<DateTimeType>())
.Type<DateTimeType>();
}
}
public class Query
{
public Foo GetFoo(Foo foo)
{
return foo;
}
public DateTime? GetTime(DateTime? time)
{
return time;
}
}
public class Foo
{
[GraphQLType(typeof(NonNullType<IdType>))]
public Guid Id { get; set; }
[GraphQLType(typeof(DateTimeType))]
public DateTime? Time { get; set; }
[GraphQLType(typeof(LongType))]
public int? Number { get; set; }
}
public class IntToStringConverter : IChangeTypeProvider
{
public bool TryCreateConverter(
Type source,
Type target,
ChangeTypeProvider root,
out ChangeType converter)
{
if (source == typeof(int) && target == typeof(string))
{
converter = input => input?.ToString();
return true;
}
converter = null;
return false;
}
}
}
}