Skip to content

Commit a3a6829

Browse files
authored
1 parent c0aafc0 commit a3a6829

1 file changed

Lines changed: 28 additions & 42 deletions

File tree

  • query-engine/core/src/query_document

query-engine/core/src/query_document/mod.rs

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,32 @@ impl CompactedDocument {
206206
.map(|op| op.into_read().expect("Trying to compact a write operation."))
207207
.collect();
208208

209+
// Convert the selections into a map of arguments. This defines the
210+
// response order and how we fetch the right data from the response set.
211+
let arguments = selections
212+
.iter()
213+
.map(|selection| {
214+
// findUnique always has a `where` argument which is an object (validated during query parsing).
215+
let where_obj = selection
216+
.argument(args::WHERE)
217+
.and_then(|arg| arg.to_owned().into_object())
218+
.expect("findUnique must contain `where` object argument");
219+
extract_filter(where_obj, &model)
220+
})
221+
.collect_vec();
222+
223+
// Gets the argument keys for later mapping.
224+
let keys = arguments
225+
.iter()
226+
.flat_map(|map| {
227+
map.iter().flat_map(|(key, value)| match value {
228+
ArgumentValue::Object(obj) => obj.keys().map(ToOwned::to_owned).collect_vec(),
229+
_ => vec![key.to_owned()],
230+
})
231+
})
232+
.unique()
233+
.collect_vec();
234+
209235
// This block creates the findMany query from the separate findUnique queries.
210236
let selection = {
211237
// The name of the query should be findManyX if the first query
@@ -223,22 +249,7 @@ impl CompactedDocument {
223249
// query. Otherwise we fail hard here.
224250
builder.set_nested_selections(selections[0].nested_selections().to_vec());
225251

226-
// The query arguments are extracted here. Combine all query
227-
// arguments from the different queries into a one large argument.
228-
let query_filters = selections
229-
.iter()
230-
.map(|selection| {
231-
// findUnique always has a `where` argument which is an object (validated during query parsing).
232-
let where_obj = selection
233-
.argument(args::WHERE)
234-
.and_then(|arg| arg.to_owned().into_object())
235-
.expect("findUnique must contain `where` object argument");
236-
let filters = extract_filter(where_obj, &model);
237-
238-
QueryFilters::new(filters)
239-
})
240-
.collect();
241-
let selection_set = SelectionSet::new(query_filters);
252+
let selection_set = SelectionSet::new(arguments.iter().cloned().map(QueryFilters::new).collect());
242253

243254
// We must select all unique fields in the query so we can
244255
// match the right response back to the right request later on.
@@ -283,35 +294,10 @@ impl CompactedDocument {
283294
QueryOptions::none()
284295
};
285296

286-
// Convert the selections into a map of arguments. This defines the
287-
// response order and how we fetch the right data from the response set.
288-
let arguments: Vec<HashMap<String, ArgumentValue>> = selections
289-
.into_iter()
290-
.map(|mut sel| {
291-
let where_obj = sel.pop_argument().unwrap().1.into_object().unwrap();
292-
let filter_map: HashMap<String, ArgumentValue> =
293-
extract_filter(where_obj, &model).into_iter().collect();
294-
295-
filter_map
296-
})
297-
.collect();
298-
299-
// Gets the argument keys for later mapping.
300-
let keys: Vec<_> = arguments
301-
.iter()
302-
.flat_map(|map| {
303-
map.iter().flat_map(|(key, value)| match value {
304-
ArgumentValue::Object(obj) => obj.keys().map(ToOwned::to_owned).collect::<Vec<_>>(),
305-
_ => vec![key.to_owned()],
306-
})
307-
})
308-
.unique()
309-
.collect();
310-
311297
Self {
312298
operation: Operation::Read(selection),
313299
name,
314-
arguments,
300+
arguments: arguments.into_iter().map(HashMap::from_iter).collect(),
315301
nested_selection,
316302
keys,
317303
original_query_options,

0 commit comments

Comments
 (0)