|
| 1 | +use std::{collections::HashMap, hash::Hash}; |
| 2 | + |
| 3 | +use graphql_parser::query::{Field, Text, Value}; |
| 4 | +use indexmap::IndexMap; |
| 5 | + |
| 6 | +use crate::parser_util::alias_or_name; |
| 7 | + |
| 8 | +/// Merges duplicates in a vector of fields. The fields in the vector are added to a |
| 9 | +/// map from field name to field. If a field with the same name already exists in the |
| 10 | +/// map, the existing and new field's children are combined into the existing field's |
| 11 | +/// children. These children will be merged later when they are normalized. |
| 12 | +/// |
| 13 | +/// The map is an `IndexMap` to ensure iteration order of the fields is preserved. |
| 14 | +/// This prevents tests from being flaky due to field order changing between test runs. |
| 15 | +pub fn merge<'a, 'b, T>(fields: Vec<Field<'a, T>>) -> Result<Vec<Field<'a, T>>, String> |
| 16 | +where |
| 17 | + T: Text<'a> + Eq + AsRef<str>, |
| 18 | + T::Value: Hash, |
| 19 | +{ |
| 20 | + let mut merged: IndexMap<String, Field<'a, T>> = IndexMap::new(); |
| 21 | + |
| 22 | + for current_field in fields { |
| 23 | + let response_key = alias_or_name(¤t_field); |
| 24 | + match merged.get_mut(&response_key) { |
| 25 | + Some(existing_field) => { |
| 26 | + if can_merge(¤t_field, existing_field)? { |
| 27 | + existing_field |
| 28 | + .selection_set |
| 29 | + .items |
| 30 | + .extend(current_field.selection_set.items); |
| 31 | + } |
| 32 | + } |
| 33 | + None => { |
| 34 | + merged.insert(response_key, current_field); |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + let fields = merged.into_iter().map(|(_, field)| field).collect(); |
| 40 | + |
| 41 | + Ok(fields) |
| 42 | +} |
| 43 | + |
| 44 | +fn can_merge<'a, T>(field_a: &Field<'a, T>, field_b: &Field<'a, T>) -> Result<bool, String> |
| 45 | +where |
| 46 | + T: Text<'a> + Eq + AsRef<str>, |
| 47 | + T::Value: Hash, |
| 48 | +{ |
| 49 | + if field_a.name != field_b.name { |
| 50 | + return Err(format!( |
| 51 | + "Fields `{}` and `{}` are different", |
| 52 | + field_a.name.as_ref(), |
| 53 | + field_b.name.as_ref(), |
| 54 | + )); |
| 55 | + } |
| 56 | + if !same_arguments(&field_a.arguments, &field_b.arguments) { |
| 57 | + return Err(format!( |
| 58 | + "Two fields named `{}` have different arguments", |
| 59 | + field_a.name.as_ref(), |
| 60 | + )); |
| 61 | + } |
| 62 | + |
| 63 | + Ok(true) |
| 64 | +} |
| 65 | + |
| 66 | +/// Compares two sets of arguments and returns true only if |
| 67 | +/// both are the same. `arguments_a` should not have two |
| 68 | +/// arguments with the same name. Similarly `arguments_b` |
| 69 | +/// should not have duplicates. It is assumed that [Argument |
| 70 | +/// Uniqueness] validation has already been run by the time |
| 71 | +/// this function is called. |
| 72 | +/// |
| 73 | +/// [Argument Uniqueness]: https://spec.graphql.org/October2021/#sec-Argument-Uniqueness |
| 74 | +fn same_arguments<'a, 'b, T>( |
| 75 | + arguments_a: &[(T::Value, Value<'a, T>)], |
| 76 | + arguments_b: &[(T::Value, Value<'a, T>)], |
| 77 | +) -> bool |
| 78 | +where |
| 79 | + T: Text<'a> + Eq + AsRef<str>, |
| 80 | + T::Value: Hash, |
| 81 | +{ |
| 82 | + if arguments_a.len() != arguments_b.len() { |
| 83 | + return false; |
| 84 | + } |
| 85 | + |
| 86 | + let mut arguments_a_map = HashMap::new(); |
| 87 | + for (arg_a_name, arg_a_val) in arguments_a { |
| 88 | + arguments_a_map.insert(arg_a_name, arg_a_val); |
| 89 | + } |
| 90 | + |
| 91 | + for (arg_b_name, arg_b_val) in arguments_b { |
| 92 | + match arguments_a_map.get(arg_b_name) { |
| 93 | + Some(arg_a_val) => { |
| 94 | + if *arg_a_val != arg_b_val { |
| 95 | + return false; |
| 96 | + } |
| 97 | + } |
| 98 | + None => return false, |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + true |
| 103 | +} |
| 104 | + |
| 105 | +#[cfg(test)] |
| 106 | +mod tests { |
| 107 | + |
| 108 | + use super::same_arguments; |
| 109 | + use graphql_parser::query::Value; |
| 110 | + |
| 111 | + #[test] |
| 112 | + fn same_args_test() { |
| 113 | + let arguments_a = vec![("a", Value::Int(1.into()))]; |
| 114 | + let arguments_b = vec![("a", Value::Int(1.into()))]; |
| 115 | + let result = same_arguments::<&str>(&arguments_a, &arguments_b); |
| 116 | + assert!(result); |
| 117 | + |
| 118 | + let arguments_a = vec![("a", Value::Int(1.into())), ("b", Value::Int(2.into()))]; |
| 119 | + let arguments_b = vec![("a", Value::Int(1.into())), ("b", Value::Int(2.into()))]; |
| 120 | + let result = same_arguments::<&str>(&arguments_a, &arguments_b); |
| 121 | + assert!(result); |
| 122 | + |
| 123 | + let arguments_a = vec![("a", Value::Int(1.into())), ("b", Value::Int(2.into()))]; |
| 124 | + let arguments_b = vec![("b", Value::Int(2.into())), ("a", Value::Int(1.into()))]; |
| 125 | + let result = same_arguments::<&str>(&arguments_a, &arguments_b); |
| 126 | + assert!(result); |
| 127 | + |
| 128 | + let arguments_a = vec![("a", Value::Int(1.into()))]; |
| 129 | + let arguments_b = vec![("a", Value::Int(2.into()))]; |
| 130 | + let result = same_arguments::<&str>(&arguments_a, &arguments_b); |
| 131 | + assert!(!result); |
| 132 | + |
| 133 | + let arguments_a = vec![("a", Value::Int(1.into())), ("b", Value::Int(1.into()))]; |
| 134 | + let arguments_b = vec![("a", Value::Int(1.into()))]; |
| 135 | + let result = same_arguments::<&str>(&arguments_a, &arguments_b); |
| 136 | + assert!(!result); |
| 137 | + |
| 138 | + let arguments_a = vec![("a", Value::Int(1.into()))]; |
| 139 | + let arguments_b = vec![("b", Value::Int(1.into()))]; |
| 140 | + let result = same_arguments::<&str>(&arguments_a, &arguments_b); |
| 141 | + assert!(!result); |
| 142 | + } |
| 143 | +} |
0 commit comments