Skip to content

Commit

Permalink
Attach previous location to duplicate type diagnostic
Browse files Browse the repository at this point in the history
Reviewed By: tyao1

Differential Revision: D58497898

fbshipit-source-id: 04f037dce49bd3b3c3745d596b96aacb14859d16
  • Loading branch information
captbaritone authored and facebook-github-bot committed Jun 13, 2024
1 parent 9e0d2ef commit 8fd21e7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
29 changes: 19 additions & 10 deletions compiler/crates/schema/src/in_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -759,16 +759,14 @@ impl InMemorySchema {
let mut field_count = 0;
let mut directive_count = 0;

let mut diagnostics: Vec<Diagnostic> = Vec::new();
let mut duplicate_definitions: Vec<(Type, Location)> = Vec::new();

for (definition, location) in schema_definitions.iter().chain(&client_definitions) {
let mut insert_into_type_map = |name: StringKey, type_: Type| {
match type_map.entry(name) {
Entry::Occupied(_) => {
diagnostics.push(Diagnostic::error(
SchemaError::DuplicateType(name),
location.with_span(definition.span()),
));
Entry::Occupied(existing_entry) => {
duplicate_definitions
.push((*existing_entry.get(), location.with_span(definition.span())));
}
Entry::Vacant(vacant) => {
vacant.insert(type_);
Expand Down Expand Up @@ -845,10 +843,6 @@ impl InMemorySchema {
}
}

if !diagnostics.is_empty() {
return Err(diagnostics);
}

// Step 2: define operation types, directives, and types
let string_type = type_map
.get(&"String".intern())
Expand Down Expand Up @@ -907,6 +901,20 @@ impl InMemorySchema {
}
}

if !duplicate_definitions.is_empty() {
return Err(duplicate_definitions
.into_iter()
.map(|(type_, location)| {
let name = schema.get_type_name(type_);
let previous_location = schema.get_type_location(type_);
Diagnostic::error(SchemaError::DuplicateType(name), location).annotate(
format!("`{}` was previously defined here:", name),
previous_location,
)
})
.collect());
}

for document in schema_documents
.iter()
.chain(client_schema_documents.iter())
Expand Down Expand Up @@ -1853,6 +1861,7 @@ impl InMemorySchema {
})
.collect()
}

fn get_type_location(&self, type_: Type) -> Location {
match type_ {
Type::InputObject(id) => self.input_objects[id.as_usize()].name.location,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,11 @@ interface Foo {
7 │ interface Foo {
│ ^^^
8 │ name: String

ℹ︎ `Foo` was previously defined here:

invalid-duplicate-type-name.graphql:3:6
2 │
3 │ type Foo {
│ ^^^
4 │ name: String

0 comments on commit 8fd21e7

Please sign in to comment.