-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathGraphQLQueryTests.cls
239 lines (219 loc) · 7.25 KB
/
GraphQLQueryTests.cls
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Tests using modified examples from https://graphql.org/learn/queries/
@isTest
public class GraphQLQueryTests {
// {
// ping
// }
@isTest
private static void node(){
GraphQLNode node = new GraphQLNode('ping');
GraphQLQuery qry = new GraphQLQuery(node, null);
System.assertEquals('{ping}', qry.query.deleteWhitespace());
}
// {
// hero {
// name
// }
// }
@isTest
private static void nodeWithChild(){
GraphQLNode node = new GraphQLNode('hero')
.add('name');
GraphQLQuery qry = new GraphQLQuery(node, null);
System.assertEquals('{hero{name}}', qry.query.deleteWhitespace());
}
// {
// hero(name: "luke skywalker") {
// id
// }
// }
@isTest
private static void testStringArg(){
GraphQLNode node = new GraphQLNode('hero')
.addArguments(new GraphQLArgument('name', 'luke skywalker'))
.add('id');
GraphQLQuery qry = new GraphQLQuery(node, null);
System.assertEquals('{hero(name:\"luke skywalker\"){id}}', qry.query.remove('\n'));
}
// {
// hero(id: 1000) {
// name
// }
// }
@isTest
private static void testNumberArg(){
GraphQLNode node = new GraphQLNode('hero')
.addArguments(new GraphQLArgument('id', 1000))
.add('name');
GraphQLQuery qry = new GraphQLQuery(node, null);
System.assertEquals('{hero(id:1000){name}}', qry.query.remove('\n'));
}
// {
// hero(isJedi: true) {
// name
// }
// }
@isTest
private static void testBooleanArg(){
GraphQLNode node = new GraphQLNode('hero')
.addArguments(new GraphQLArgument('isJedi', true))
.add('name');
GraphQLQuery qry = new GraphQLQuery(node, null);
System.assertEquals('{hero(isJedi:true){name}}', qry.query.remove('\n'));
}
// {
// hero(episode: EMPIRE) {
// name
// }
// }
@isTest
private static void testStringVariableArg(){
GraphQLNode node = new GraphQLNode('hero')
.addArguments(new GraphQLArgument('episode', 'EMPIRE', true))
.add('name');
GraphQLQuery qry = new GraphQLQuery(node, null);
System.assertEquals('{hero(episode:EMPIRE){name}}', qry.query.remove('\n'));
}
// {
// hero(attr: {droid: false, weapon: "lightsaber"}) {
// name
// }
// }
@isTest
private static void testNestedArgs(){
GraphQLNode node = new GraphQLNode('hero')
.addArguments(new GraphQLArgument('attr', new GraphQLArgument[]{
new GraphQLArgument('droid', false),
new GraphQLArgument('weapon', 'lightsaber')
}))
.add('name');
GraphQLQuery qry = new GraphQLQuery(node, null);
System.assertEquals('{hero(attr:{droid:false, weapon:\"lightsaber\"}){name}}', qry.query.remove('\n'));
}
// {
// hero(episode: EMPIRE, dies: true}) {
// name
// }
// }
@isTest
private static void testMultipleArgs(){
GraphQLNode node = new GraphQLNode('hero')
.addArguments(new GraphQLArgument[]{
new GraphQLArgument('episode', 'EMPIRE', true),
new GraphQLArgument('dies', true)
})
.add('name');
GraphQLQuery qry = new GraphQLQuery(node, null);
System.assertEquals('{hero(episode:EMPIRE, dies:true){name}}', qry.query.remove('\n'));
}
// {
// hero {
// attr {
// weapon
// }
// }
// }
@isTest
private static void testNestedNode(){
GraphQLNode node = new GraphQLNode('hero')
.add(
new GraphQLNode('attr').add('weapon')
);
GraphQLQuery qry = new GraphQLQuery(node, null);
System.assertEquals('{hero{attr{weapon}}}', qry.query.remove('\n'));
}
// {
// hero {
// name
// ... on Droid {
// primaryFunction
// }
// }
// }
@isTest
private static void testChildOnType(){
GraphQLNode node = new GraphQLNode('hero')
.add(new Object[]{
'name',
new GraphQLNode('Droid')
.setTypeFragment(true)
.add('primaryFunction')
});
GraphQLQuery qry = new GraphQLQuery(node, null);
System.assertEquals('{hero{name... on Droid{primaryFunction}}}', qry.query.remove('\n'));
}
// {
// empireHero: hero(episode: EMPIRE) {
// name
// }
// jediHero: hero(episode: JEDI) {
// name
// }
// }
@isTest
private static void testAlias(){
GraphQLNode[] nodes = new GraphQLNode[]{
new GraphQLNode('hero')
.setAlias('empireHero')
.addArguments(new GraphQLArgument('episode', 'EMPIRE', true))
.add('name'),
new GraphQLNode('hero')
.setAlias('jediHero')
.addArguments(new GraphQLArgument('episode', 'JEDI', true))
.add('name')
};
GraphQLQuery qry = new GraphQLQuery(nodes, null);
System.assertEquals('{empireHero: hero(episode:EMPIRE){name}jediHero: hero(episode:JEDI){name}}', qry.query.remove('\n'));
}
// mutation CreateReviewForEpisode($ep: Episode!, $review: ReviewInput!) {
// createReview(episode: $ep, review: $review) {
// stars
// commentary
// }
// }
// == Variables ==
// {
// "ep": "JEDI",
// "review": {
// "stars": 5,
// "commentary": "This is a great movie!"
// }
// }
@isTest
private static void testOperation(){
GraphQLNode node = new GraphQLNode('CreateReviewForEpisode')
.setOperation('mutation')
.addArguments(new GraphQLArgument('$ep', 'Episode!', true))
.addArguments(new GraphQLArgument('$review', 'ReviewInput!', true))
.add(
new GraphQLNode('createReview')
.addArguments(new GraphQLArgument[]{
new GraphQLArgument('episode', '$ep', true),
new GraphQLArgument('review', '$review', true)
})
.add(new Object[]{'stars', 'commentary'})
);
CreateReviewForEpisode createReviewVariables = new CreateReviewForEpisode();
createReviewVariables.ep = 'JEDI';
createReviewVariables.review = new Review();
createReviewVariables.review.stars = 5;
createReviewVariables.review.commentary = 'This is a great movie!';
GraphQLQuery qry = new GraphQLQuery(node, createReviewVariables);
String payload = JSON.serialize(qry);
System.assertEquals(
'{"variables":{"review":{"stars":5,"commentary":"This is a great movie!"},"ep":"JEDI"},"query":"mutation CreateReviewForEpisode($ep:Episode!, $review:ReviewInput!){\\ncreateReview(episode:$ep, review:$review){\\nstars\\ncommentary\\n}\\n}\\n"}',
payload
);
}
/* DTO for testOperation mutation test */
public class CreateReviewForEpisode
{
public String ep { get; set; }
public Review review { get; set; }
}
public class Review
{
public Integer stars { get; set; }
public String commentary { get; set; }
}
}