Skip to content

Commit 1e79063

Browse files
authored
fix(compiler): fix handling of orphan root type extensions (#993)
1 parent bfd2772 commit 1e79063

File tree

3 files changed

+42
-15
lines changed

3 files changed

+42
-15
lines changed

crates/apollo-compiler/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
2626

2727
## Fixes
2828

29+
- **Fix handling of orphan root type extensions - [dariuszkuc], [pull/993]**
2930
- **Fix possible stack overflow in validation of directive definition arguments with nested types - [dariuszkuc], [pull/987]**
3031
- **Fix `iter_origin()` to be a pub method- [duckki], [pull/989]**
3132

crates/apollo-compiler/src/schema/from_ast.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,24 @@ impl SchemaBuilder {
285285
mut errors,
286286
} = self;
287287
schema.sources = errors.sources.clone();
288+
289+
// process orphan type extensions (https://github.com/apollographql/apollo-rs/pull/678) first,
290+
// so they can be reflected on the implicit schema definition below
291+
if adopt_orphan_extensions {
292+
for (type_name, extensions) in orphan_type_extensions {
293+
let type_def = adopt_type_extensions(&mut errors, &type_name, &extensions);
294+
let previous = schema.types.insert(type_name, type_def);
295+
assert!(previous.is_none());
296+
}
297+
} else {
298+
for extensions in orphan_type_extensions.values() {
299+
for ext in extensions {
300+
let name = ext.name().unwrap().clone();
301+
errors.push(name.location(), BuildError::OrphanTypeExtension { name })
302+
}
303+
}
304+
}
305+
288306
match schema_definition {
289307
SchemaDefinitionStatus::Found => {}
290308
SchemaDefinitionStatus::NoneSoFar { orphan_extensions } => {
@@ -323,21 +341,6 @@ impl SchemaBuilder {
323341
}
324342
}
325343
}
326-
// https://github.com/apollographql/apollo-rs/pull/678
327-
if adopt_orphan_extensions {
328-
for (type_name, extensions) in orphan_type_extensions {
329-
let type_def = adopt_type_extensions(&mut errors, &type_name, &extensions);
330-
let previous = schema.types.insert(type_name, type_def);
331-
assert!(previous.is_none());
332-
}
333-
} else {
334-
for extensions in orphan_type_extensions.values() {
335-
for ext in extensions {
336-
let name = ext.name().unwrap().clone();
337-
errors.push(name.location(), BuildError::OrphanTypeExtension { name })
338-
}
339-
}
340-
}
341344
(schema, errors)
342345
}
343346
}

crates/apollo-compiler/tests/schema.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use apollo_compiler::schema::SchemaBuilder;
12
use apollo_compiler::Schema;
23

34
#[test]
@@ -226,3 +227,25 @@ fn test_default_root_op_name_ignored_with_explicit_schema_def() {
226227
let schema = Schema::parse_and_validate(input, "schema.graphql").unwrap();
227228
assert!(schema.schema_definition.mutation.is_none())
228229
}
230+
231+
#[test]
232+
fn handles_implicit_root_types() {
233+
let sdl = r#"
234+
extend type Query {
235+
foo: String
236+
}
237+
238+
extend type Mutation {
239+
bar: String
240+
}
241+
"#;
242+
243+
let builder = SchemaBuilder::new().adopt_orphan_extensions();
244+
let schema = builder
245+
.parse(sdl, "schema.graphql")
246+
.build()
247+
.expect("schema parsed successfully");
248+
249+
assert!(schema.schema_definition.query.is_some());
250+
assert!(schema.schema_definition.mutation.is_some());
251+
}

0 commit comments

Comments
 (0)