-
-
Notifications
You must be signed in to change notification settings - Fork 481
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(biome_graphql_parser): parse schema definition #2557
Merged
arendjr
merged 4 commits into
biomejs:main
from
vohoanglong0107:feat-graphql-parse-schema-definition
May 1, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1960bf7
feat: parse graphql schema definition
vohoanglong0107 fe72ff3
refactor: extract condition to new fn
vohoanglong0107 6ff1c1e
docs: document why it's necessary to check for end of root operation
vohoanglong0107 42c0b7f
fix: check root operation type definition end at new operation defini…
vohoanglong0107 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
140 changes: 140 additions & 0 deletions
140
crates/biome_graphql_parser/src/parser/definitions/schema.rs
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,140 @@ | ||
use crate::parser::{ | ||
directive::DirectiveList, | ||
parse_description, | ||
parse_error::{ | ||
expected_named_type, expected_operation_type, expected_root_operation_type_definition, | ||
}, | ||
r#type::parse_named_type, | ||
value::is_at_string, | ||
GraphqlParser, | ||
}; | ||
use biome_graphql_syntax::{ | ||
GraphqlSyntaxKind::{self, *}, | ||
T, | ||
}; | ||
use biome_parser::{ | ||
parse_lists::ParseNodeList, parse_recovery::ParseRecovery, parsed_syntax::ParsedSyntax, | ||
prelude::ParsedSyntax::*, Parser, | ||
}; | ||
|
||
use super::{is_at_definition, operation::OPERATION_TYPE}; | ||
|
||
#[inline] | ||
pub(crate) fn parse_schema_definition(p: &mut GraphqlParser) -> ParsedSyntax { | ||
if !is_at_schema_definition(p) { | ||
return Absent; | ||
} | ||
let m = p.start(); | ||
// description is optional | ||
parse_description(p).ok(); | ||
|
||
p.bump(T![schema]); | ||
|
||
DirectiveList.parse_list(p); | ||
p.expect(T!['{']); | ||
RootOperationTypeDefinitionList.parse_list(p); | ||
p.expect(T!['}']); | ||
|
||
Present(m.complete(p, GRAPHQL_SCHEMA_DEFINITION)) | ||
} | ||
|
||
#[derive(Default)] | ||
struct RootOperationTypeDefinitionList; | ||
|
||
impl ParseNodeList for RootOperationTypeDefinitionList { | ||
type Kind = GraphqlSyntaxKind; | ||
type Parser<'source> = GraphqlParser<'source>; | ||
|
||
const LIST_KIND: Self::Kind = GRAPHQL_ROOT_OPERATION_TYPE_DEFINITION_LIST; | ||
|
||
fn parse_element(&mut self, p: &mut Self::Parser<'_>) -> ParsedSyntax { | ||
parse_root_operation_type_definition(p) | ||
} | ||
|
||
fn is_at_list_end(&self, p: &mut Self::Parser<'_>) -> bool { | ||
is_at_root_operation_type_definition_end(p) | ||
} | ||
|
||
fn recover( | ||
&mut self, | ||
p: &mut Self::Parser<'_>, | ||
parsed_element: ParsedSyntax, | ||
) -> biome_parser::parse_recovery::RecoveryResult { | ||
parsed_element.or_recover( | ||
p, | ||
&RootOperationTypeDefinitionListRecovery, | ||
expected_root_operation_type_definition, | ||
) | ||
} | ||
} | ||
|
||
struct RootOperationTypeDefinitionListRecovery; | ||
|
||
impl ParseRecovery for RootOperationTypeDefinitionListRecovery { | ||
type Kind = GraphqlSyntaxKind; | ||
type Parser<'source> = GraphqlParser<'source>; | ||
const RECOVERED_KIND: Self::Kind = GRAPHQL_BOGUS; | ||
|
||
fn is_at_recovered(&self, p: &mut Self::Parser<'_>) -> bool { | ||
is_at_root_operation_type_definition(p) || is_at_root_operation_type_definition_end(p) | ||
} | ||
} | ||
|
||
#[inline] | ||
fn parse_root_operation_type_definition(p: &mut GraphqlParser) -> ParsedSyntax { | ||
if !(is_at_root_operation_type_definition(p)) { | ||
return Absent; | ||
} | ||
let m = p.start(); | ||
if p.at_ts(OPERATION_TYPE) { | ||
let m = p.start(); | ||
p.bump_ts(OPERATION_TYPE); | ||
m.complete(p, GRAPHQL_OPERATION_TYPE); | ||
} | ||
// missing operation type | ||
else if p.at(T![:]) { | ||
p.error(expected_operation_type(p, p.cur_range())); | ||
} | ||
// handle typo in operation type | ||
else { | ||
p.error(expected_operation_type(p, p.cur_range())); | ||
p.bump_any(); | ||
} | ||
p.expect(T![:]); | ||
parse_named_type(p).or_add_diagnostic(p, expected_named_type); | ||
|
||
Present(m.complete(p, GRAPHQL_ROOT_OPERATION_TYPE_DEFINITION)) | ||
} | ||
|
||
#[inline] | ||
pub(crate) fn is_at_schema_definition(p: &mut GraphqlParser<'_>) -> bool { | ||
p.at(T![schema]) || (is_at_string(p) && p.lookahead_at(T![schema])) | ||
} | ||
|
||
#[inline] | ||
fn is_at_root_operation_type_definition(p: &mut GraphqlParser<'_>) -> bool { | ||
p.at_ts(OPERATION_TYPE) | ||
// missing operation type | ||
|| p.at(T![:]) | ||
// there is likely a typo in the operation type | ||
|| p.lookahead_at(T![:]) | ||
} | ||
|
||
/// To prevent a missing closing brace from causing the parser to include the next definition | ||
/// as part of the root operation type definition | ||
/// ```graphql | ||
/// schema { | ||
/// query: Query | ||
/// mutation: Mutation | ||
/// | ||
/// schema { | ||
/// query: Query | ||
/// } | ||
#[inline] | ||
fn is_at_root_operation_type_definition_end(p: &mut GraphqlParser<'_>) -> bool { | ||
// stop at closing brace or at the start of a new definition | ||
p.at(T!['}']) | ||
|| (!p.at_ts(OPERATION_TYPE) && is_at_definition(p)) | ||
// start of a new operation definition | ||
|| (p.at_ts(OPERATION_TYPE) && !p.lookahead_at(T![:])) | ||
} |
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
25 changes: 25 additions & 0 deletions
25
crates/biome_graphql_parser/tests/graphql_test_suite/err/definitions/schema.graphql
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,25 @@ | ||
schema { | ||
quer: MyQueryRootType | ||
mutatio: MyMutationRootType | ||
subscriptio: MySubscriptionRootType | ||
: MySubscriptionRootType | ||
8: MySubscriptionRootType | ||
} | ||
|
||
schema { | ||
quer: | ||
} | ||
|
||
"sth schema { | ||
quer: | ||
} | ||
|
||
schema { | ||
query: MyQueryRootType | ||
|
||
schema { | ||
query: MyQueryRootType | ||
|
||
query MyQueryRootType { | ||
field: String | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please help me to understand why we need here
is_at_root_operation_type_definition_end
?What cases do we cover? Is it possible to check only
p.at(T!['}'])
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider this case:
If we only check for
p.at(T!['}'])
the second schema definition is also included in RootOperationType, so I included another check to see if we are at the start of other definition or not.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think this is a good idea?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe it's a great idea.
I'm wondering if it's possible to have a 'complex' check when we get an
Absent
result from theparse_element
function.Specifically, it would be beneficial to maintain
p.at(T!['}'])
the happy path, avoiding additional checks which are required for the failure path.I don't have any work solution for now, we might think about it in the next MR.