-
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.
feat(parser, compiler): add parsing
ast::Type
from a string (#718)
* feat(parser): add parse_type method to allow type parsing only * setup field type parsing in apollo-compiler * add field type to schema in compiler * parse_field_type to return an option * parse_field_type returns Result<(FieldType, Diagnostics), Diagnostics> * clippy * FieldType struct does not need sources, as we create diagnostics right away * Parse directly into `ast::Type` * naming * Return Err() on parse errors * tweak unreachable! messages * Do not pass the same SourceMap twice Co-authored-by: Simon Sapin <[email protected]> * Improve doc comments for `Parser::parse_{type,selection_set}` Co-authored-by: Simon Sapin <[email protected]> * doc tweak * changelogs --------- Co-authored-by: Renée Kooi <[email protected]> Co-authored-by: Renée <[email protected]> Co-authored-by: Simon Sapin <[email protected]>
- Loading branch information
1 parent
ed0d481
commit d0fca39
Showing
10 changed files
with
189 additions
and
11 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
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,37 @@ | ||
use apollo_compiler::schema::Type; | ||
|
||
#[test] | ||
fn test_valid_field_type() { | ||
let input = "String!"; | ||
let field_type = Type::parse(input, "field_type.graphql").expect("expected a field type"); | ||
assert_eq!(field_type.to_string(), input); | ||
|
||
let input = "[[[[[Int!]!]!]!]!]!"; | ||
let field_type = Type::parse(input, "field_type.graphql").expect("expected a field type"); | ||
assert_eq!(field_type.to_string(), input); | ||
} | ||
|
||
#[test] | ||
fn test_invalid_field_type() { | ||
let input = "[[String]"; | ||
match Type::parse(input, "field_type.graphql") { | ||
Ok(parsed) => panic!("Field type should fail to parse, instead got `{parsed}`"), | ||
Err(errors) => { | ||
let errors = errors.to_string_no_color(); | ||
assert!( | ||
errors.contains("Error: syntax error: expected R_BRACK, got EOF"), | ||
"{errors}" | ||
); | ||
} | ||
} | ||
|
||
let input = "[]"; | ||
match Type::parse(input, "field_type.graphql") { | ||
Ok(parsed) => panic!("Field type should fail to parse, instead got `{parsed}`"), | ||
Err(diag) => { | ||
let errors = diag.to_string_no_color(); | ||
assert!(errors.contains("expected item type"), "{errors}"); | ||
assert!(errors.contains("expected R_BRACK, got EOF"), "{errors}"); | ||
} | ||
} | ||
} |
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
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