1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use std::collections::{HashMap, HashSet};

use crate::{
    parser::types::{Field, Selection, SelectionSet},
    validation::visitor::{Visitor, VisitorContext},
    Positioned,
};

#[derive(Default)]
pub struct OverlappingFieldsCanBeMerged;

impl<'a> Visitor<'a> for OverlappingFieldsCanBeMerged {
    fn enter_selection_set(
        &mut self,
        ctx: &mut VisitorContext<'a>,
        selection_set: &'a Positioned<SelectionSet>,
    ) {
        let mut find_conflicts = FindConflicts {
            outputs: Default::default(),
            visited: Default::default(),
            ctx,
        };
        find_conflicts.find(None, selection_set);
    }
}

struct FindConflicts<'a, 'ctx> {
    outputs: HashMap<(Option<&'a str>, &'a str), &'a Positioned<Field>>,
    visited: HashSet<&'a str>,
    ctx: &'a mut VisitorContext<'ctx>,
}

impl<'a, 'ctx> FindConflicts<'a, 'ctx> {
    pub fn find(&mut self, on_type: Option<&'a str>, selection_set: &'a Positioned<SelectionSet>) {
        for selection in &selection_set.node.items {
            match &selection.node {
                Selection::Field(field) => {
                    let output_name = field
                        .node
                        .alias
                        .as_ref()
                        .map(|name| &name.node)
                        .unwrap_or_else(|| &field.node.name.node);
                    self.add_output(on_type, &output_name, field);
                }
                Selection::InlineFragment(inline_fragment) => {
                    let on_type = inline_fragment
                        .node
                        .type_condition
                        .as_ref()
                        .map(|cond| cond.node.on.node.as_str());
                    self.find(on_type, &inline_fragment.node.selection_set);
                }
                Selection::FragmentSpread(fragment_spread) => {
                    if let Some(fragment) =
                        self.ctx.fragment(&fragment_spread.node.fragment_name.node)
                    {
                        let on_type = Some(fragment.node.type_condition.node.on.node.as_str());

                        if !self
                            .visited
                            .insert(fragment_spread.node.fragment_name.node.as_str())
                        {
                            // To avoid recursing itself, this error is detected by the
                            // `NoFragmentCycles` validator.
                            continue;
                        }

                        self.find(on_type, &fragment.node.selection_set);
                    }
                }
            }
        }
    }

    fn add_output(
        &mut self,
        on_type: Option<&'a str>,
        name: &'a str,
        field: &'a Positioned<Field>,
    ) {
        if let Some(prev_field) = self.outputs.get(&(on_type, name)) {
            if prev_field.node.name.node != field.node.name.node {
                self.ctx.report_error(
                    vec![prev_field.pos, field.pos],
                    format!("Fields \"{}\" conflict because \"{}\" and \"{}\" are different fields. Use different aliases on the fields to fetch both if this was intentional.",
                            name, prev_field.node.name.node, field.node.name.node));
            }

            // check arguments
            if prev_field.node.arguments.len() != field.node.arguments.len() {
                self.ctx.report_error(
                    vec![prev_field.pos, field.pos],
                    format!("Fields \"{}\" conflict because they have differing arguments. Use different aliases on the fields to fetch both if this was intentional.", name));
            }

            for (name, value) in &prev_field.node.arguments {
                match field.node.get_argument(&name.node) {
                    Some(other_value) if value == other_value => {}
                    _=> self.ctx.report_error(
                        vec![prev_field.pos, field.pos],
                        format!("Fields \"{}\" conflict because they have differing arguments. Use different aliases on the fields to fetch both if this was intentional.", name)),
                }
            }
        } else {
            self.outputs.insert((on_type, name), field);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    pub fn factory() -> OverlappingFieldsCanBeMerged {
        OverlappingFieldsCanBeMerged
    }

    #[test]
    fn same_field_on_different_type() {
        expect_passes_rule!(
            factory,
            r#"
          {
           pet {
            ... on Dog {
                doesKnowCommand(dogCommand: SIT)
            }
            ... on Cat {
                doesKnowCommand(catCommand: JUMP)
            }
           }
          }
        "#,
        );
    }

    #[test]
    fn same_field_on_same_type() {
        expect_fails_rule!(
            factory,
            r#"
          {
           pet {
            ... on Dog {
                doesKnowCommand(dogCommand: SIT)
            }
            ... on Dog {
                doesKnowCommand(dogCommand: Heel)
            }
           }
          }
        "#,
        );
    }

    #[test]
    fn same_alias_on_different_type() {
        expect_passes_rule!(
            factory,
            r#"
          {
           pet {
            ... on Dog {
                volume: barkVolume
            }
            ... on Cat {
                volume: meowVolume
            }
           }
          }
        "#,
        );
    }
}