Merge branch 'async-await' into async-await-rebase
This commit is contained in:
commit
2884ef313b
1 changed files with 115 additions and 0 deletions
|
@ -900,6 +900,121 @@ where
|
||||||
Ok((value, errors))
|
Ok((value, errors))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "async")]
|
||||||
|
pub async fn execute_validated_query_async<'a, QueryT, MutationT, CtxT, S>(
|
||||||
|
document: Document<'a, S>,
|
||||||
|
operation_name: Option<&str>,
|
||||||
|
root_node: &RootNode<'a, QueryT, MutationT, S>,
|
||||||
|
variables: &Variables<S>,
|
||||||
|
context: &CtxT,
|
||||||
|
) -> Result<(Value<S>, Vec<ExecutionError<S>>), GraphQLError<'a>>
|
||||||
|
where
|
||||||
|
S: ScalarValue + Send + Sync,
|
||||||
|
QueryT: crate::GraphQLTypeAsync<S, Context = CtxT> + Send + Sync,
|
||||||
|
QueryT::TypeInfo: Send + Sync,
|
||||||
|
MutationT: crate::GraphQLTypeAsync<S, Context = CtxT> + Send + Sync,
|
||||||
|
MutationT::TypeInfo: Send + Sync,
|
||||||
|
CtxT: Send + Sync,
|
||||||
|
for<'b> &'b S: ScalarRefValue<'b>,
|
||||||
|
{
|
||||||
|
let mut fragments = vec![];
|
||||||
|
let mut operation = None;
|
||||||
|
|
||||||
|
for def in document {
|
||||||
|
match def {
|
||||||
|
Definition::Operation(op) => {
|
||||||
|
if operation_name.is_none() && operation.is_some() {
|
||||||
|
return Err(GraphQLError::MultipleOperationsProvided);
|
||||||
|
}
|
||||||
|
|
||||||
|
let move_op = operation_name.is_none()
|
||||||
|
|| op.item.name.as_ref().map(|s| s.item) == operation_name;
|
||||||
|
|
||||||
|
if move_op {
|
||||||
|
operation = Some(op);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Definition::Fragment(f) => fragments.push(f),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
let op = match operation {
|
||||||
|
Some(op) => op,
|
||||||
|
None => return Err(GraphQLError::UnknownOperationName),
|
||||||
|
};
|
||||||
|
|
||||||
|
let default_variable_values = op.item.variable_definitions.map(|defs| {
|
||||||
|
defs.item
|
||||||
|
.items
|
||||||
|
.iter()
|
||||||
|
.filter_map(|&(ref name, ref def)| {
|
||||||
|
def.default_value
|
||||||
|
.as_ref()
|
||||||
|
.map(|i| (name.item.to_owned(), i.item.clone()))
|
||||||
|
})
|
||||||
|
.collect::<HashMap<String, InputValue<S>>>()
|
||||||
|
});
|
||||||
|
|
||||||
|
let errors = RwLock::new(Vec::new());
|
||||||
|
let value;
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut all_vars;
|
||||||
|
let mut final_vars = variables;
|
||||||
|
|
||||||
|
if let Some(defaults) = default_variable_values {
|
||||||
|
all_vars = variables.clone();
|
||||||
|
|
||||||
|
for (name, value) in defaults {
|
||||||
|
all_vars.entry(name).or_insert(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
final_vars = &all_vars;
|
||||||
|
}
|
||||||
|
|
||||||
|
let root_type = match op.item.operation_type {
|
||||||
|
OperationType::Query => root_node.schema.query_type(),
|
||||||
|
OperationType::Mutation => root_node
|
||||||
|
.schema
|
||||||
|
.mutation_type()
|
||||||
|
.expect("No mutation type found"),
|
||||||
|
};
|
||||||
|
|
||||||
|
let executor = Executor {
|
||||||
|
fragments: &fragments
|
||||||
|
.iter()
|
||||||
|
.map(|f| (f.item.name.item, &f.item))
|
||||||
|
.collect(),
|
||||||
|
variables: final_vars,
|
||||||
|
current_selection_set: Some(&op.item.selection_set[..]),
|
||||||
|
parent_selection_set: None,
|
||||||
|
current_type: root_type,
|
||||||
|
schema: &root_node.schema,
|
||||||
|
context,
|
||||||
|
errors: &errors,
|
||||||
|
field_path: FieldPath::Root(op.start),
|
||||||
|
};
|
||||||
|
|
||||||
|
value = match op.item.operation_type {
|
||||||
|
OperationType::Query => {
|
||||||
|
executor
|
||||||
|
.resolve_into_value_async(&root_node.query_info, &root_node)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
OperationType::Mutation => {
|
||||||
|
executor
|
||||||
|
.resolve_into_value_async(&root_node.mutation_info, &root_node.mutation_type)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut errors = errors.into_inner().unwrap();
|
||||||
|
errors.sort();
|
||||||
|
|
||||||
|
Ok((value, errors))
|
||||||
|
}
|
||||||
|
|
||||||
impl<'r, S> Registry<'r, S>
|
impl<'r, S> Registry<'r, S>
|
||||||
where
|
where
|
||||||
S: ScalarValue + 'r,
|
S: ScalarValue + 'r,
|
||||||
|
|
Loading…
Add table
Reference in a new issue