Skip to content

Commit c555241

Browse files
committed
Add executor tests, fix a bunch of bugs
1 parent 251f957 commit c555241

File tree

6 files changed

+414
-9
lines changed

6 files changed

+414
-9
lines changed

src/executor.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::collections::HashMap;
22
use std::marker::PhantomData;
33

4+
use ::GraphQLError;
45
use ast::{InputValue, ToInputValue, Document, Selection, Fragment, Definition, Type, FromInputValue, OperationType};
56
use value::Value;
67
use parser::SourcePosition;
@@ -198,6 +199,15 @@ impl<'a> FieldPath<'a> {
198199
}
199200

200201
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+
201211
/// The error message
202212
pub fn message(&self) -> &str {
203213
&self.message
@@ -214,16 +224,16 @@ impl ExecutionError {
214224
}
215225
}
216226

217-
pub fn execute_validated_query<QueryT, MutationT, CtxT>(
227+
pub fn execute_validated_query<'a, QueryT, MutationT, CtxT>(
218228
document: Document,
219229
operation_name: Option<&str>,
220230
root_node: &RootNode<CtxT, QueryT, MutationT>,
221231
variables: &HashMap<String, InputValue>,
222232
context: &CtxT
223233
)
224-
-> (Value, Vec<ExecutionError>)
234+
-> Result<(Value, Vec<ExecutionError>), GraphQLError<'a>>
225235
where QueryT: GraphQLType<CtxT>,
226-
MutationT: GraphQLType<CtxT>,
236+
MutationT: GraphQLType<CtxT>
227237
{
228238
let mut fragments = vec![];
229239
let mut operation = None;
@@ -232,7 +242,7 @@ pub fn execute_validated_query<QueryT, MutationT, CtxT>(
232242
match def {
233243
Definition::Operation(op) => {
234244
if operation_name.is_none() && operation.is_some() {
235-
panic!("Must provide operation name if query contains multiple operations");
245+
return Err(GraphQLError::MultipleOperationsProvided);
236246
}
237247

238248
let move_op = operation_name.is_none()
@@ -246,7 +256,11 @@ pub fn execute_validated_query<QueryT, MutationT, CtxT>(
246256
};
247257
}
248258

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+
250264
let mut errors = Vec::new();
251265
let value;
252266

@@ -269,7 +283,7 @@ pub fn execute_validated_query<QueryT, MutationT, CtxT>(
269283

270284
errors.sort();
271285

272-
(value, errors)
286+
Ok((value, errors))
273287
}
274288

275289
impl<CtxT> Registry<CtxT> {

0 commit comments

Comments
 (0)