Skip to content

Commit 19ed895

Browse files
committed
graphql: Avoid double serialization of query
1 parent a85ca3d commit 19ed895

File tree

4 files changed

+23
-25
lines changed

4 files changed

+23
-25
lines changed

graph/src/data/query/query.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ pub struct Query {
112112
pub variables: Option<QueryVariables>,
113113
pub shape_hash: u64,
114114
pub network: Option<String>,
115+
pub query_text: Arc<String>,
116+
pub variables_text: Arc<String>,
115117
_force_use_of_new: (),
116118
}
117119

@@ -124,23 +126,27 @@ impl Query {
124126
network: Option<String>,
125127
) -> Self {
126128
let shape_hash = shape_hash(&document);
129+
130+
let (query_text, variables_text) = if *crate::log::LOG_GQL_TIMING {
131+
(
132+
document
133+
.format(&graphql_parser::Style::default().indent(0))
134+
.replace('\n', " "),
135+
serde_json::to_string(&variables).unwrap_or_default(),
136+
)
137+
} else {
138+
("(gql logging turned off)".to_owned(), "".to_owned())
139+
};
140+
127141
Query {
128142
schema,
129143
document,
130144
variables,
131145
shape_hash,
132146
network,
147+
query_text: Arc::new(query_text),
148+
variables_text: Arc::new(variables_text),
133149
_force_use_of_new: (),
134150
}
135151
}
136-
137-
pub fn query_text(&self) -> String {
138-
self.document
139-
.format(&graphql_parser::Style::default().indent(0))
140-
.replace('\n', " ")
141-
}
142-
143-
pub fn variables_text(&self) -> String {
144-
serde_json::to_string(&self.variables).unwrap_or_default()
145-
}
146152
}

graphql/src/execution/query.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::sync::Arc;
55
use graph::data::graphql::ext::TypeExt;
66
use graph::data::query::{Query as GraphDataQuery, QueryVariables};
77
use graph::data::schema::Schema;
8-
use graph::prelude::QueryExecutionError;
8+
use graph::prelude::{CheapClone, QueryExecutionError};
99

1010
use crate::execution::{get_field, get_named_type};
1111
use crate::introspection::introspection_schema;
@@ -60,13 +60,6 @@ impl Query {
6060
max_complexity: Option<u64>,
6161
max_depth: u8,
6262
) -> Result<Arc<Self>, Vec<QueryExecutionError>> {
63-
let (query_text, variables_text) = match *graph::log::LOG_GQL_TIMING {
64-
true => (query.query_text(), query.variables_text()),
65-
false => ("(gql logging turned off)".to_owned(), "".to_owned()),
66-
};
67-
let query_text = Arc::new(query_text);
68-
let variables_text = Arc::new(variables_text);
69-
7063
let mut operation = None;
7164
let mut fragments = HashMap::new();
7265
for defn in query.document.definitions.into_iter() {
@@ -107,8 +100,8 @@ impl Query {
107100
shape_hash: query.shape_hash,
108101
kind,
109102
network: query.network,
110-
query_text,
111-
variables_text,
103+
query_text: query.query_text.cheap_clone(),
104+
variables_text: query.variables_text.cheap_clone(),
112105
complexity: 0,
113106
};
114107

server/http/src/service.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,8 @@ where
273273

274274
let result = match query {
275275
Ok(query) => {
276-
// Not great that we serialize the query here only for the panic case.
277-
let query_text = query.query_text();
278-
let variables_text = query.variables_text();
276+
let query_text = query.query_text.cheap_clone();
277+
let variables_text = query.variables_text.cheap_clone();
279278

280279
let result =
281280
graph::spawn_blocking_allow_panic(service.graphql_runner.run_query(query))
@@ -284,7 +283,7 @@ where
284283
match result {
285284
Ok(res) => res,
286285

287-
// `JoinError` means a panic.
286+
// `Err(JoinError)` means a panic.
288287
Err(e) => {
289288
let e = e.into_panic();
290289
let e = match e

server/index-node/src/service.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ where
119119
let query_result = match result {
120120
Ok(res) => res,
121121

122-
// `JoinError` means a panic.
122+
// `Err(JoinError)` means a panic.
123123
Err(e) => {
124124
let e = e.into_panic();
125125
let e = match e

0 commit comments

Comments
 (0)