1
1
use std:: collections:: HashMap ;
2
2
use std:: marker:: PhantomData ;
3
3
4
+ use :: GraphQLError ;
4
5
use ast:: { InputValue , ToInputValue , Document , Selection , Fragment , Definition , Type , FromInputValue , OperationType } ;
5
6
use value:: Value ;
6
7
use parser:: SourcePosition ;
@@ -198,6 +199,15 @@ impl<'a> FieldPath<'a> {
198
199
}
199
200
200
201
impl ExecutionError {
202
+ #[ doc( hidden) ]
203
+ pub fn new ( location : SourcePosition , path : & [ & str ] , message : & str ) -> ExecutionError {
204
+ ExecutionError {
205
+ location : location,
206
+ path : path. iter ( ) . map ( |s| ( * s) . to_owned ( ) ) . collect ( ) ,
207
+ message : message. to_owned ( ) ,
208
+ }
209
+ }
210
+
201
211
/// The error message
202
212
pub fn message ( & self ) -> & str {
203
213
& self . message
@@ -214,16 +224,16 @@ impl ExecutionError {
214
224
}
215
225
}
216
226
217
- pub fn execute_validated_query < QueryT , MutationT , CtxT > (
227
+ pub fn execute_validated_query < ' a , QueryT , MutationT , CtxT > (
218
228
document : Document ,
219
229
operation_name : Option < & str > ,
220
230
root_node : & RootNode < CtxT , QueryT , MutationT > ,
221
231
variables : & HashMap < String , InputValue > ,
222
232
context : & CtxT
223
233
)
224
- -> ( Value , Vec < ExecutionError > )
234
+ -> Result < ( Value , Vec < ExecutionError > ) , GraphQLError < ' a > >
225
235
where QueryT : GraphQLType < CtxT > ,
226
- MutationT : GraphQLType < CtxT > ,
236
+ MutationT : GraphQLType < CtxT >
227
237
{
228
238
let mut fragments = vec ! [ ] ;
229
239
let mut operation = None ;
@@ -232,7 +242,7 @@ pub fn execute_validated_query<QueryT, MutationT, CtxT>(
232
242
match def {
233
243
Definition :: Operation ( op) => {
234
244
if operation_name. is_none ( ) && operation. is_some ( ) {
235
- panic ! ( "Must provide operation name if query contains multiple operations" ) ;
245
+ return Err ( GraphQLError :: MultipleOperationsProvided ) ;
236
246
}
237
247
238
248
let move_op = operation_name. is_none ( )
@@ -246,7 +256,11 @@ pub fn execute_validated_query<QueryT, MutationT, CtxT>(
246
256
} ;
247
257
}
248
258
249
- let op = operation. expect ( "Could not find operation to execute" ) ;
259
+ let op = match operation {
260
+ Some ( op) => op,
261
+ None => return Err ( GraphQLError :: UnknownOperationName ) ,
262
+ } ;
263
+
250
264
let mut errors = Vec :: new ( ) ;
251
265
let value;
252
266
@@ -269,7 +283,7 @@ pub fn execute_validated_query<QueryT, MutationT, CtxT>(
269
283
270
284
errors. sort ( ) ;
271
285
272
- ( value, errors)
286
+ Ok ( ( value, errors) )
273
287
}
274
288
275
289
impl < CtxT > Registry < CtxT > {
0 commit comments