-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathrename.rs
56 lines (46 loc) · 1.52 KB
/
rename.rs
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
//! This example shows how to rename a type definition
use apollo_compiler::name;
use apollo_compiler::schema::ExtendedType;
use apollo_compiler::validation::Valid;
use apollo_compiler::Schema;
#[cfg(not(test))]
fn main() {
print!("{}", renamed())
}
fn renamed() -> Valid<Schema> {
let input = "type Query { field: Int }";
let mut schema = Schema::parse(input, "schema.graphql").unwrap();
// 1. Remove the definition from the `types` map, using its old name as a key
let mut type_def = schema.types.shift_remove("Query").unwrap();
// 2. Set the new name in the struct
let ExtendedType::Object(obj) = &mut type_def else {
panic!("expected an object type")
};
let new_name = name!("MyQuery");
obj.make_mut().name = new_name.clone();
// 3. Insert back into the map using the new name as the key
// WARNING: it’s your responsibility to make sure to use the same name as in the struct!
// Failing to do so make cause code elsewhere to behave incorrectly, or potentially panic.
schema.types.insert(new_name.clone(), type_def);
// 4. Update any existing reference to the old name
schema
.schema_definition
.make_mut()
.query
.as_mut()
.unwrap()
.name = new_name;
schema.validate().unwrap()
}
#[test]
fn test_renamed() {
let expected = expect_test::expect![[r#"
schema {
query: MyQuery
}
type MyQuery {
field: Int
}
"#]];
expected.assert_eq(&renamed().to_string());
}