Skip to content

Commit d74c030

Browse files
committed
graphql: Add complexity value to query logs
1 parent d0331ca commit d74c030

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

graphql/src/execution/query.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ pub struct Query {
4242
/// have dummy values
4343
pub(crate) query_text: Arc<String>,
4444
pub(crate) variables_text: Arc<String>,
45+
pub(crate) complexity: u64,
4546
}
4647

4748
impl Query {
@@ -88,7 +89,7 @@ impl Query {
8889
q::OperationDefinition::Query(q::Query { selection_set, .. }) => {
8990
(Kind::Query, selection_set)
9091
}
91-
// Queries can be run by just sending a selction set
92+
// Queries can be run by just sending a selection set
9293
q::OperationDefinition::SelectionSet(selection_set) => (Kind::Query, selection_set),
9394
q::OperationDefinition::Subscription(q::Subscription { selection_set, .. }) => {
9495
(Kind::Subscription, selection_set)
@@ -100,20 +101,21 @@ impl Query {
100101
}
101102
};
102103

103-
let query = Arc::new(Self {
104+
let mut query = Self {
104105
schema: query.schema,
105106
variables,
106107
fragments,
107108
selection_set,
108109
kind,
109110
query_text,
110111
variables_text,
111-
});
112+
complexity: 0,
113+
};
112114

113115
query.validate_fields()?;
114116
query.check_complexity(max_complexity, max_depth)?;
115117

116-
Ok(query)
118+
Ok(Arc::new(query))
117119
}
118120

119121
/// Return the block constraint for the toplevel query field(s) Since,
@@ -161,6 +163,7 @@ impl Query {
161163
kind: self.kind,
162164
query_text: self.query_text.clone(),
163165
variables_text: self.variables_text.clone(),
166+
complexity: self.complexity,
164167
})
165168
}
166169

@@ -186,7 +189,7 @@ impl Query {
186189
}
187190

188191
fn check_complexity(
189-
&self,
192+
&mut self,
190193
max_complexity: Option<u64>,
191194
max_depth: u8,
192195
) -> Result<(), Vec<QueryExecutionError>> {
@@ -198,6 +201,7 @@ impl Query {
198201
max_complexity,
199202
)]);
200203
}
204+
self.complexity = complexity;
201205
}
202206
Ok(())
203207
}

graphql/src/query/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ where
8686
"query_time_ms" => start.elapsed().as_millis(),
8787
"cached" => ctx.cached.load(std::sync::atomic::Ordering::SeqCst),
8888
"block" => block_ptr.map(|b| b.number).unwrap_or(0),
89+
"complexity" => &query.complexity
8990
);
9091
}
9192
result

0 commit comments

Comments
 (0)