Skip to content

Commit 34dc38e

Browse files
committed
graphql: Do not pass variables around during execution
Query preprocessing eliminated all variable references
1 parent 6dc097a commit 34dc38e

File tree

6 files changed

+61
-195
lines changed

6 files changed

+61
-195
lines changed

graph/src/data/graphql/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,3 @@ pub use object_or_interface::ObjectOrInterface;
3131
pub mod object_macro;
3232
pub use crate::object;
3333
pub use object_macro::{object_value, IntoValue};
34-
pub mod visitor;

graphql/src/execution/execution.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ impl Default for WeightedResult {
136136

137137
struct HashableQuery<'a> {
138138
query_schema_id: &'a DeploymentHash,
139-
query_variables: &'a HashMap<String, r::Value>,
140139
query_fragments: &'a HashMap<String, q::FragmentDefinition>,
141140
selection_set: &'a q::SelectionSet,
142141
block_ptr: &'a BlockPtr,
@@ -163,13 +162,6 @@ impl StableHash for HashableQuery<'_> {
163162
self.query_schema_id
164163
.stable_hash(sequence_number.next_child(), state);
165164

166-
// Not stable! Uses to_string()
167-
self.query_variables
168-
.iter()
169-
.map(|(k, v)| (k, v.to_string()))
170-
.collect::<HashMap<_, _>>()
171-
.stable_hash(sequence_number.next_child(), state);
172-
173165
// Not stable! Uses to_string()
174166
self.query_fragments
175167
.iter()
@@ -197,7 +189,6 @@ fn cache_key(
197189
// Otherwise, incorrect results may be returned.
198190
let query = HashableQuery {
199191
query_schema_id: ctx.query.schema.id(),
200-
query_variables: &ctx.query.variables,
201192
query_fragments: &ctx.query.fragments,
202193
selection_set,
203194
block_ptr,
@@ -608,8 +599,8 @@ pub fn collect_fields_inner<'a>(
608599
let selections = selection_set
609600
.items
610601
.iter()
611-
.filter(|selection| !qast::skip_selection(selection, &ctx.query.variables))
612-
.filter(|selection| qast::include_selection(selection, &ctx.query.variables));
602+
.filter(|selection| !qast::skip_selection(selection))
603+
.filter(|selection| qast::include_selection(selection));
613604

614605
for selection in selections {
615606
match selection {
@@ -1058,7 +1049,7 @@ pub fn coerce_argument_values<'a>(
10581049
.flatten()
10591050
{
10601051
let value = qast::get_argument_value(&field.arguments, &argument_def.name).cloned();
1061-
match coercion::coerce_input_value(value, &argument_def, &resolver, &query.variables) {
1052+
match coercion::coerce_input_value(value, &argument_def, &resolver) {
10621053
Ok(Some(value)) => {
10631054
if argument_def.name == "text".to_string() {
10641055
coerced_values.insert(

graphql/src/execution/query.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use graph::prelude::{
1616
};
1717

1818
use crate::introspection::introspection_schema;
19-
use crate::query::ext::ValueExt;
2019
use crate::query::{ast as qast, ext::BlockConstraint};
2120
use crate::schema::ast as sast;
2221
use crate::{
@@ -79,9 +78,7 @@ impl<'a> std::fmt::Display for SelectedFields<'a> {
7978
pub struct Query {
8079
/// The schema against which to execute the query
8180
pub schema: Arc<ApiSchema>,
82-
/// The variables for the query, coerced into proper values
83-
pub variables: HashMap<String, r::Value>,
84-
/// The root selection set of the query
81+
/// The root selection set of the query. All variable references have already been resolved
8582
pub selection_set: Arc<q::SelectionSet>,
8683
/// The ShapeHash of the original query
8784
pub shape_hash: u64,
@@ -179,7 +176,6 @@ impl Query {
179176

180177
let query = Self {
181178
schema: raw_query.schema,
182-
variables: raw_query.variables,
183179
fragments: raw_query.fragments,
184180
selection_set: Arc::new(raw_query.selection_set),
185181
shape_hash: query.shape_hash,
@@ -272,7 +268,6 @@ impl Query {
272268

273269
Arc::new(Self {
274270
schema: Arc::new(introspection_schema),
275-
variables: self.variables.clone(),
276271
fragments: self.fragments.clone(),
277272
selection_set: self.selection_set.clone(),
278273
shape_hash: self.shape_hash,
@@ -417,7 +412,7 @@ fn coerce_variable(
417412

418413
let resolver = |name: &str| schema.document().get_named_type(name);
419414

420-
coerce_value(value, &variable_def.var_type, &resolver, &HashMap::new()).map_err(|value| {
415+
coerce_value(value, &variable_def.var_type, &resolver).map_err(|value| {
421416
vec![QueryExecutionError::InvalidArgumentError(
422417
variable_def.position,
423418
variable_def.name.to_owned(),

graphql/src/query/ast.rs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use graph::prelude::{q::*, r};
2-
use std::collections::HashMap;
1+
use graph::prelude::q::*;
32
use std::ops::Deref;
43

54
use graph::prelude::QueryExecutionError;
@@ -64,18 +63,14 @@ pub fn get_argument_value<'a>(arguments: &'a [(String, Value)], name: &str) -> O
6463
}
6564

6665
/// Returns true if a selection should be skipped (as per the `@skip` directive).
67-
pub fn skip_selection(selection: &Selection, variables: &HashMap<String, r::Value>) -> bool {
66+
pub fn skip_selection(selection: &Selection) -> bool {
6867
match get_directive(selection, "skip".to_string()) {
6968
Some(directive) => match get_argument_value(&directive.arguments, "if") {
7069
Some(val) => match val {
7170
// Skip if @skip(if: true)
7271
Value::Boolean(skip_if) => *skip_if,
7372

74-
// Also skip if @skip(if: $variable) where $variable is true
75-
Value::Variable(name) => variables.get(name).map_or(false, |var| match var {
76-
r::Value::Boolean(v) => v.to_owned(),
77-
_ => false,
78-
}),
73+
Value::Variable(_) => unreachable!("variables have already been resolved"),
7974

8075
_ => false,
8176
},
@@ -86,18 +81,14 @@ pub fn skip_selection(selection: &Selection, variables: &HashMap<String, r::Valu
8681
}
8782

8883
/// Returns true if a selection should be included (as per the `@include` directive).
89-
pub fn include_selection(selection: &Selection, variables: &HashMap<String, r::Value>) -> bool {
84+
pub fn include_selection(selection: &Selection) -> bool {
9085
match get_directive(selection, "include".to_string()) {
9186
Some(directive) => match get_argument_value(&directive.arguments, "if") {
9287
Some(val) => match val {
9388
// Include if @include(if: true)
9489
Value::Boolean(include) => *include,
9590

96-
// Also include if @include(if: $variable) where $variable is true
97-
Value::Variable(name) => variables.get(name).map_or(false, |var| match var {
98-
r::Value::Boolean(v) => v.to_owned(),
99-
_ => false,
100-
}),
91+
Value::Variable(_) => unreachable!("variables have already been resolved"),
10192

10293
_ => false,
10394
},

graphql/src/store/prefetch.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -895,8 +895,8 @@ fn collect_fields_inner<'a>(
895895
let selections = selection_set
896896
.items
897897
.iter()
898-
.filter(|selection| !qast::skip_selection(selection, &ctx.query.variables))
899-
.filter(|selection| qast::include_selection(selection, &ctx.query.variables));
898+
.filter(|selection| !qast::skip_selection(selection))
899+
.filter(|selection| qast::include_selection(selection));
900900

901901
for selection in selections {
902902
match selection {

0 commit comments

Comments
 (0)