@@ -65,36 +65,42 @@ final class EntitiesDataFetcher implements DataFetcher<Mono<DataFetcherResult<Li
65
65
66
66
67
67
@ Override
68
- public Mono <DataFetcherResult <List <Object >>> get (DataFetchingEnvironment environment ) {
69
- List <Map <String , Object >> representations = environment .getArgument (_Entity .argumentName );
68
+ public Mono <DataFetcherResult <List <Object >>> get (DataFetchingEnvironment env ) {
69
+ List <Map <String , Object >> representations = env .getArgument (_Entity .argumentName );
70
+ if (representations == null ) {
71
+ return Mono .error (new RepresentationException (
72
+ Collections .emptyMap (), "Missing \" representations\" argument" ));
73
+ }
70
74
71
- Set <String > batched = new HashSet <>();
72
- List <Mono <Object >> monoList = new ArrayList <>();
75
+ Set <String > batchedTypes = new HashSet <>();
76
+ List <Mono <? >> monoList = new ArrayList <>();
73
77
for (int index = 0 ; index < representations .size (); index ++) {
74
78
Map <String , Object > map = representations .get (index );
75
- if (!(map .get ("__typename" ) instanceof String typename )) {
79
+ if (!(map .get ("__typename" ) instanceof String type )) {
76
80
Exception ex = new RepresentationException (map , "Missing \" __typename\" argument" );
77
- monoList .add (resolveException (ex , environment , null , index ));
81
+ monoList .add (resolveException (ex , env , null , index ));
78
82
continue ;
79
83
}
80
- EntityHandlerMethod handlerMethod = this .handlerMethods .get (typename );
84
+ EntityHandlerMethod handlerMethod = this .handlerMethods .get (type );
81
85
if (handlerMethod == null ) {
82
86
Exception ex = new RepresentationException (map , "No entity fetcher" );
83
- monoList .add (resolveException (ex , environment , null , index ));
87
+ monoList .add (resolveException (ex , env , null , index ));
84
88
continue ;
85
89
}
86
90
87
91
if (!handlerMethod .isBatchHandlerMethod ()) {
88
- monoList .add (invokeEntityMethod (environment , handlerMethod , map , index ));
89
- }
90
- else if (batched .contains (typename )) {
91
- // zip needs a value, this will be replaced by batch results
92
- monoList .add (Mono .just (Collections .emptyMap ()));
92
+ monoList .add (invokeEntityMethod (env , handlerMethod , map , index ));
93
93
}
94
94
else {
95
- EntityBatchDelegate batchDelegate = new EntityBatchDelegate (environment , handlerMethod , typename );
96
- monoList .add (batchDelegate .invokeEntityBatchMethod ());
97
- batched .add (typename );
95
+ if (!batchedTypes .contains (type )) {
96
+ EntityBatchDelegate delegate = new EntityBatchDelegate (env , representations , handlerMethod , type );
97
+ monoList .add (delegate .invokeEntityBatchMethod ());
98
+ batchedTypes .add (type );
99
+ }
100
+ else {
101
+ // Covered by batch invocation, but zip needs a value (to be replaced by batch results)
102
+ monoList .add (Mono .just (Collections .emptyMap ()));
103
+ }
98
104
}
99
105
}
100
106
return Mono .zip (monoList , Arrays ::asList ).map (EntitiesDataFetcher ::toDataFetcherResult );
@@ -108,7 +114,7 @@ private Mono<Object> invokeEntityMethod(
108
114
.onErrorResume ((ex ) -> resolveException (ex , env , handlerMethod , index ));
109
115
}
110
116
111
- private Mono <Object > resolveException (
117
+ private Mono <ErrorContainer > resolveException (
112
118
Throwable ex , DataFetchingEnvironment env , @ Nullable EntityHandlerMethod handlerMethod , int index ) {
113
119
114
120
Throwable theEx = (ex instanceof CompletionException ) ? ex .getCause () : ex ;
@@ -117,8 +123,7 @@ private Mono<Object> resolveException(
117
123
118
124
return this .exceptionResolver .resolveException (theEx , theEnv , handler )
119
125
.map (ErrorContainer ::new )
120
- .switchIfEmpty (Mono .fromCallable (() -> createDefaultError (theEx , theEnv )))
121
- .cast (Object .class );
126
+ .switchIfEmpty (Mono .fromCallable (() -> createDefaultError (theEx , theEnv )));
122
127
}
123
128
124
129
private ErrorContainer createDefaultError (Throwable ex , DataFetchingEnvironment env ) {
@@ -154,28 +159,30 @@ private class EntityBatchDelegate {
154
159
155
160
private final EntityHandlerMethod handlerMethod ;
156
161
157
- private final List <Map <String , Object >> representations = new ArrayList <>();
162
+ private final List <Map <String , Object >> filteredRepresentations = new ArrayList <>();
158
163
159
164
private final List <Integer > indexes = new ArrayList <>();
160
165
161
166
@ Nullable
162
167
private List <?> resultList ;
163
168
164
- EntityBatchDelegate (DataFetchingEnvironment env , EntityHandlerMethod handlerMethod , String typeName ) {
169
+ EntityBatchDelegate (
170
+ DataFetchingEnvironment env , List <Map <String , Object >> allRepresentations ,
171
+ EntityHandlerMethod handlerMethod , String type ) {
172
+
165
173
this .environment = env ;
166
174
this .handlerMethod = handlerMethod ;
167
- List <Map <String , Object >> maps = env .getArgument (_Entity .argumentName );
168
- for (int i = 0 ; i < maps .size (); i ++) {
169
- Map <String , Object > map = maps .get (i );
170
- if (typeName .equals (map .get ("__typename" ))) {
171
- this .representations .add (map );
175
+ for (int i = 0 ; i < allRepresentations .size (); i ++) {
176
+ Map <String , Object > map = allRepresentations .get (i );
177
+ if (type .equals (map .get ("__typename" ))) {
178
+ this .filteredRepresentations .add (map );
172
179
this .indexes .add (i );
173
180
}
174
181
}
175
182
}
176
183
177
184
Mono <Object > invokeEntityBatchMethod () {
178
- return this .handlerMethod .getEntities (this .environment , this .representations )
185
+ return this .handlerMethod .getEntities (this .environment , this .filteredRepresentations )
179
186
.mapNotNull ((result ) -> (((List <?>) result ).isEmpty ()) ? null : result )
180
187
.switchIfEmpty (Mono .defer (this ::handleEmptyResult ))
181
188
.onErrorResume (this ::handleErrorResult )
@@ -186,17 +193,17 @@ Mono<Object> invokeEntityBatchMethod() {
186
193
}
187
194
188
195
Mono <Object > handleEmptyResult () {
189
- List <Mono <Object >> exceptions = new ArrayList <>(this .indexes .size ());
196
+ List <Mono <? >> exceptions = new ArrayList <>(this .indexes .size ());
190
197
for (int i = 0 ; i < this .indexes .size (); i ++) {
191
- Map <String , Object > map = this .representations .get (i );
198
+ Map <String , Object > map = this .filteredRepresentations .get (i );
192
199
Exception ex = new RepresentationNotResolvedException (map , this .handlerMethod );
193
200
exceptions .add (resolveException (ex , this .environment , this .handlerMethod , this .indexes .get (i )));
194
201
}
195
202
return Mono .zip (exceptions , Arrays ::asList );
196
203
}
197
204
198
205
Mono <List <Object >> handleErrorResult (Throwable ex ) {
199
- List <Mono <Object >> list = new ArrayList <>();
206
+ List <Mono <? >> list = new ArrayList <>();
200
207
for (Integer index : this .indexes ) {
201
208
list .add (resolveException (ex , this .environment , this .handlerMethod , index ));
202
209
}
0 commit comments