48
48
import org .springframework .graphql .execution .DefaultBatchLoaderRegistry ;
49
49
import org .springframework .graphql .execution .ExecutionGraphQlService ;
50
50
import org .springframework .graphql .execution .GraphQlSource ;
51
+ import org .springframework .lang .Nullable ;
51
52
import org .springframework .stereotype .Controller ;
52
53
53
54
import static org .assertj .core .api .Assertions .assertThat ;
54
55
55
56
/**
56
- * Tests with invocation of DataFetcher's from annotated methods.
57
+ * Test GraphQL requests handled through {@code @SchemaMapping} methods.
58
+ *
57
59
* @author Rossen Stoyanchev
58
60
*/
59
- public class AnnotatedDataFetcherInvocationTests {
61
+ public class SchemaMappingInvocationTests {
60
62
61
63
@ Test
62
64
void queryWithScalarArgument () {
@@ -71,13 +73,11 @@ void queryWithScalarArgument() {
71
73
" }" +
72
74
"}" ;
73
75
74
- ExecutionResult result = initGraphQlService (BookController . class )
76
+ ExecutionResult result = initGraphQlService ()
75
77
.execute (new RequestInput (query , null , null ))
76
78
.block ();
77
79
78
- assertThat (result .getErrors ()).isEmpty ();
79
- Map <String , Object > data = result .getData ();
80
- assertThat (data ).isNotNull ();
80
+ Map <String , Object > data = getData (result );
81
81
82
82
Map <String , Object > book = getValue (data , "bookById" );
83
83
assertThat (book .get ("id" )).isEqualTo ("1" );
@@ -97,14 +97,11 @@ void queryWithObjectArgument() {
97
97
" }" +
98
98
"}" ;
99
99
100
- ExecutionResult result = initGraphQlService (BookController . class )
100
+ ExecutionResult result = initGraphQlService ()
101
101
.execute (new RequestInput (query , null , null ))
102
102
.block ();
103
103
104
- assertThat (result .getErrors ()).isEmpty ();
105
- Map <String , Object > data = result .getData ();
106
- assertThat (data ).isNotNull ();
107
-
104
+ Map <String , Object > data = getData (result );
108
105
List <Map <String , Object >> bookList = getValue (data , "booksByCriteria" );
109
106
assertThat (bookList ).hasSize (2 );
110
107
assertThat (bookList .get (0 ).get ("name" )).isEqualTo ("Nineteen Eighty-Four" );
@@ -128,13 +125,11 @@ void queryWithArgumentViaDataFetchingEnvironment() {
128
125
return executionInput ;
129
126
});
130
127
131
- ExecutionResult result = initGraphQlService (BookController . class )
128
+ ExecutionResult result = initGraphQlService ()
132
129
.execute (requestInput )
133
130
.block ();
134
131
135
- assertThat (result .getErrors ()).isEmpty ();
136
- Map <String , Object > data = result .getData ();
137
- assertThat (data ).isNotNull ();
132
+ Map <String , Object > data = getData (result );
138
133
139
134
Map <String , Object > author = getValue (data , "authorById" );
140
135
assertThat (author .get ("id" )).isEqualTo ("101" );
@@ -154,13 +149,11 @@ void mutation() {
154
149
" }" +
155
150
"}" ;
156
151
157
- ExecutionResult result = initGraphQlService (BookController . class )
152
+ ExecutionResult result = initGraphQlService ()
158
153
.execute (new RequestInput (operation , null , null ))
159
154
.block ();
160
155
161
- assertThat (result .getErrors ()).isEmpty ();
162
- Map <String , Object > data = result .getData ();
163
- assertThat (data ).isNotNull ();
156
+ Map <String , Object > data = getData (result );
164
157
165
158
Map <String , Object > author = getValue (data , "addAuthor" );
166
159
assertThat (author .get ("id" )).isEqualTo ("99" );
@@ -177,13 +170,11 @@ void subscription() {
177
170
" }" +
178
171
"}" ;
179
172
180
- ExecutionResult result = initGraphQlService (BookController . class )
173
+ ExecutionResult result = initGraphQlService ()
181
174
.execute (new RequestInput (operation , null , null ))
182
175
.block ();
183
176
184
- assertThat (result .getErrors ()).isEmpty ();
185
- Publisher <ExecutionResult > publisher = result .getData ();
186
- assertThat (publisher ).isNotNull ();
177
+ Publisher <ExecutionResult > publisher = getData (result );
187
178
188
179
Flux <Map <String , Object >> bookFlux = Flux .from (publisher ).map (rs -> {
189
180
Map <String , Object > map = rs .getData ();
@@ -203,14 +194,22 @@ void subscription() {
203
194
}
204
195
205
196
206
- private ExecutionGraphQlService initGraphQlService (Class <?> beanClass ) {
197
+ private ExecutionGraphQlService initGraphQlService () {
207
198
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext ();
208
199
applicationContext .register (TestConfig .class );
209
200
applicationContext .refresh ();
210
201
211
202
return applicationContext .getBean (ExecutionGraphQlService .class );
212
203
}
213
204
205
+ private <T > T getData (@ Nullable ExecutionResult result ) {
206
+ assertThat (result ).isNotNull ();
207
+ assertThat (result .getErrors ()).isEmpty ();
208
+ T data = result .getData ();
209
+ assertThat (data ).isNotNull ();
210
+ return data ;
211
+ }
212
+
214
213
@ SuppressWarnings ("unchecked" )
215
214
private <T > T getValue (Map <String , Object > data , String key ) {
216
215
return (T ) data .get (key );
@@ -253,6 +252,7 @@ public DefaultBatchLoaderRegistry batchLoaderRegistry() {
253
252
}
254
253
255
254
255
+ @ SuppressWarnings ("unused" )
256
256
@ Controller
257
257
private static class BookController {
258
258
0 commit comments