Skip to content

Commit 1777fb1

Browse files
committed
graphql: Keep original field order when grouping by block constraint
1 parent b0026b7 commit 1777fb1

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

graphql/src/execution/query.rs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -246,18 +246,21 @@ impl Query {
246246
Ok(Arc::new(query))
247247
}
248248

249-
/// Return the block constraint for the toplevel query field(s), merging the selection sets of
250-
/// fields that have the same block constraint.
249+
/// Return the block constraint for the toplevel query field(s), merging
250+
/// consecutive fields that have the same block constraint, while making
251+
/// sure that the fields appear in the same order as they did in the
252+
/// query
251253
///
252-
/// Also returns the combined error policy for those fields, which is `Deny` if any field is
253-
/// `Deny` and `Allow` otherwise.
254+
/// Also returns the combined error policy for those fields, which is
255+
/// `Deny` if any field is `Deny` and `Allow` otherwise.
254256
pub fn block_constraint(
255257
&self,
256-
) -> Result<HashMap<BlockConstraint, (a::SelectionSet, ErrorPolicy)>, Vec<QueryExecutionError>>
258+
) -> Result<Vec<(BlockConstraint, (a::SelectionSet, ErrorPolicy))>, Vec<QueryExecutionError>>
257259
{
258-
let mut bcs = HashMap::new();
260+
let mut bcs: Vec<(BlockConstraint, (a::SelectionSet, ErrorPolicy))> = Vec::new();
259261

260262
let root_type = self.schema.query_type.as_ref();
263+
let mut prev_bc: Option<BlockConstraint> = None;
261264
for field in self.selection_set.fields_for(root_type) {
262265
let bc = match field.argument_value("block") {
263266
Some(bc) => BlockConstraint::try_from_value(bc).map_err(|_| {
@@ -281,16 +284,19 @@ impl Query {
281284
None => ErrorPolicy::Deny,
282285
};
283286

284-
let (selection_set, error_policy) = bcs.entry(bc).or_insert_with(|| {
285-
(
286-
a::SelectionSet::empty_from(&self.selection_set),
287-
field_error_policy,
288-
)
289-
});
290-
selection_set.push(field);
291-
if field_error_policy == ErrorPolicy::Deny {
292-
*error_policy = ErrorPolicy::Deny;
287+
let next_bc = Some(bc.clone());
288+
if prev_bc == next_bc {
289+
let (selection_set, error_policy) = &mut bcs.last_mut().unwrap().1;
290+
selection_set.push(field);
291+
if field_error_policy == ErrorPolicy::Deny {
292+
*error_policy = ErrorPolicy::Deny;
293+
}
294+
} else {
295+
let mut selection_set = a::SelectionSet::empty_from(&self.selection_set);
296+
selection_set.push(field);
297+
bcs.push((bc, (selection_set, field_error_policy)))
293298
}
299+
prev_bc = next_bc;
294300
}
295301
Ok(bcs)
296302
}

graphql/src/query/ext.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl ValueExt for q::Value {
5454
}
5555
}
5656

57-
#[derive(PartialEq, Eq, Hash, Debug)]
57+
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
5858
pub enum BlockConstraint {
5959
Hash(H256),
6060
Number(BlockNumber),

0 commit comments

Comments
 (0)