-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make everything know their own name (#727)
* Make everything know their own name Fixes #708 In a few places (but not consistently) a `name` field was omitted from some structs used as map values on the basis that it would have been redundant with the map key. This reverts that decision, making it the user’s responsibility when mutating documents to keep names consistent. * Add a `pub name: Name` field to `executable::Fragment` as well as `ScalarType`, `ObjectType`, `InterfaceType`, `EnumType`, `UnionType`, and `InputObjectType` in `schema`. * Add a `fn name(&self) -> &Name` method to the `schema::ExtendedType` enum * Add a `pub name: Option<Name>` field to `executable::Operation` * Remove `executable::OperationRef<'_>` (which was equivalent to `(Option<&Name>, &Node<Operation>)`), replacing its uses with `&Node<Operation>` * clippy
- Loading branch information
1 parent
90dde73
commit 7efc372
Showing
48 changed files
with
542 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
//! This example shows how to rename a type definition | ||
use apollo_compiler::schema::ExtendedType; | ||
use apollo_compiler::schema::Name; | ||
use apollo_compiler::Schema; | ||
|
||
#[cfg(not(test))] | ||
fn main() { | ||
print!("{}", renamed()) | ||
} | ||
|
||
fn renamed() -> Schema { | ||
let input = "type Query { field: Int }"; | ||
let mut schema = Schema::parse(input, "schema.graphql"); | ||
schema.validate().unwrap(); | ||
|
||
// 1. Remove the definition from the `types` map, using its old name as a key | ||
let mut type_def = schema.types.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::from("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() | ||
.node = new_name; | ||
|
||
schema.validate().unwrap(); | ||
schema | ||
} | ||
|
||
#[test] | ||
fn test_renamed() { | ||
let expected = expect_test::expect![[r#" | ||
schema { | ||
query: MyQuery | ||
} | ||
type MyQuery { | ||
field: Int | ||
} | ||
"#]]; | ||
expected.assert_eq(&renamed().to_string()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.