Skip to content

Commit

Permalink
feat(biome_graphql_parser): parse union extension (#3010)
Browse files Browse the repository at this point in the history
  • Loading branch information
vohoanglong0107 committed May 29, 2024
1 parent 7160fb4 commit 8a26e25
Show file tree
Hide file tree
Showing 16 changed files with 331 additions and 383 deletions.
52 changes: 29 additions & 23 deletions crates/biome_graphql_factory/src/generated/node_factory.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 2 additions & 42 deletions crates/biome_graphql_factory/src/generated/syntax_factory.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion crates/biome_graphql_parser/src/parser/definitions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use self::{
r#enum::parse_enum_type_definition,
scalar::{parse_scalar_type_definition, parse_scalar_type_extension},
schema::{parse_schema_definition, parse_schema_extension},
union::parse_union_type_definition,
union::{parse_union_type_definition, parse_union_type_extension},
};

use super::value::is_at_string;
Expand Down Expand Up @@ -100,6 +100,7 @@ fn parse_extension(p: &mut GraphqlParser) -> ParsedSyntax {
T![scalar] => parse_scalar_type_extension(p),
T![type] => parse_object_type_extension(p),
T![interface] => parse_interface_type_extension(p),
T![union] => parse_union_type_extension(p),
_ => Absent,
}
}
Expand Down
24 changes: 23 additions & 1 deletion crates/biome_graphql_parser/src/parser/definitions/union.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::parser::{
directive::DirectiveList,
is_nth_at_name, is_nth_at_non_kw_name, parse_description,
parse_error::{expected_name, expected_named_type},
parse_error::{expected_name, expected_named_type, expected_union_extension},
parse_name,
r#type::parse_named_type,
GraphqlParser,
Expand Down Expand Up @@ -39,6 +39,28 @@ pub(crate) fn parse_union_type_definition(p: &mut GraphqlParser) -> ParsedSyntax
Present(m.complete(p, GRAPHQL_UNION_TYPE_DEFINITION))
}

/// Must only be called if the next 2 token is `extend` and `union`, otherwise it will panic.
#[inline]
pub(super) fn parse_union_type_extension(p: &mut GraphqlParser) -> ParsedSyntax {
let m = p.start();

p.bump(T![extend]);
p.bump(T![union]);

parse_name(p).or_add_diagnostic(p, expected_name);

let directive_list = DirectiveList.parse_list(p);
let directive_empty = directive_list.range(p).is_empty();

let union_members_empty = parse_union_member_types(p).is_absent();

if directive_empty && union_members_empty {
p.error(expected_union_extension(p, p.cur_range()));
}

Present(m.complete(p, GRAPHQL_UNION_TYPE_EXTENSION))
}

#[inline]
fn parse_union_member_types(p: &mut GraphqlParser) -> ParsedSyntax {
if !is_at_union_member_types(p) {
Expand Down
7 changes: 7 additions & 0 deletions crates/biome_graphql_parser/src/parser/parse_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,10 @@ pub(crate) fn expected_object_extension(p: &GraphqlParser, range: TextRange) ->
range,
)
}

pub(crate) fn expected_union_extension(p: &GraphqlParser, range: TextRange) -> ParseDiagnostic {
p.err_builder(
"Expected at least one directive or union member types",
range,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
extend union SearchResult

extend union SearchResult Photo | Person
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
---
source: crates/biome_graphql_parser/tests/spec_test.rs
expression: snapshot
---
## Input
```graphql
extend union SearchResult
extend union SearchResult Photo | Person
```

## AST

```
GraphqlRoot {
bom_token: missing (optional),
definitions: GraphqlDefinitionList [
GraphqlUnionTypeExtension {
extend_token: EXTEND_KW@0..7 "extend" [] [Whitespace(" ")],
union_token: UNION_KW@7..13 "union" [] [Whitespace(" ")],
name: GraphqlName {
value_token: GRAPHQL_NAME@13..25 "SearchResult" [] [],
},
directives: GraphqlDirectiveList [],
union_members: missing (optional),
},
GraphqlUnionTypeExtension {
extend_token: EXTEND_KW@25..34 "extend" [Newline("\n"), Newline("\n")] [Whitespace(" ")],
union_token: UNION_KW@34..40 "union" [] [Whitespace(" ")],
name: GraphqlName {
value_token: GRAPHQL_NAME@40..53 "SearchResult" [] [Whitespace(" ")],
},
directives: GraphqlDirectiveList [],
union_members: GraphqlUnionMemberTypes {
eq_token: missing (required),
bitwise_or_token: missing (optional),
members: GraphqlUnionMemberTypeList [
GraphqlNamedType {
name: GraphqlName {
value_token: GRAPHQL_NAME@53..59 "Photo" [] [Whitespace(" ")],
},
},
PIPE@59..61 "|" [] [Whitespace(" ")],
GraphqlNamedType {
name: GraphqlName {
value_token: GRAPHQL_NAME@61..67 "Person" [] [],
},
},
],
},
},
],
eof_token: EOF@67..68 "" [Newline("\n")] [],
}
```

## CST

```
0: [email protected]
0: (empty)
1: [email protected]
0: [email protected]
0: [email protected] "extend" [] [Whitespace(" ")]
1: [email protected] "union" [] [Whitespace(" ")]
2: [email protected]
0: [email protected] "SearchResult" [] []
3: [email protected]
4: (empty)
1: [email protected]
0: [email protected] "extend" [Newline("\n"), Newline("\n")] [Whitespace(" ")]
1: [email protected] "union" [] [Whitespace(" ")]
2: [email protected]
0: [email protected] "SearchResult" [] [Whitespace(" ")]
3: [email protected]
4: [email protected]
0: (empty)
1: (empty)
2: [email protected]
0: [email protected]
0: [email protected]
0: [email protected] "Photo" [] [Whitespace(" ")]
1: [email protected] "|" [] [Whitespace(" ")]
2: [email protected]
0: [email protected]
0: [email protected] "Person" [] []
2: [email protected] "" [Newline("\n")] []
```

## Diagnostics

```
union_extension.graphql:3:1 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
× Expected at least one directive or union member types
1 │ extend union SearchResult
2 │
> 3 │ extend union SearchResult Photo | Person
│ ^^^^^^
4 │
union_extension.graphql:3:27 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
× expected `=` but instead found `Photo`
1 │ extend union SearchResult
2 │
> 3 │ extend union SearchResult Photo | Person
│ ^^^^^
4 │
i Remove Photo
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
extend union SearchResult = Photo | Person

extend union SearchResult @deprecated
Loading

0 comments on commit 8a26e25

Please sign in to comment.