Skip to content

Commit

Permalink
Parse directives in more places
Browse files Browse the repository at this point in the history
  • Loading branch information
obmarg committed Jan 7, 2024
1 parent dab97cb commit 63f72a7
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 11 deletions.
2 changes: 2 additions & 0 deletions cynic-parser/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ pub struct FieldDefinition {
pub ty: TypeId,
pub arguments: Vec<NodeId>,
pub description: Option<NodeId>,
pub directives: Vec<DirectiveId>,
}

pub struct InputObjectDefinition {
Expand All @@ -78,6 +79,7 @@ pub struct InputValueDefinition {
pub ty: TypeId,
pub description: Option<NodeId>,
pub default: Option<ValueId>,
pub directives: Vec<DirectiveId>,
}

pub struct RootOperationTypeDefinition {
Expand Down
16 changes: 16 additions & 0 deletions cynic-parser/src/ast/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,14 @@ impl<'a> AstReader<'a, FieldDefinitionId> {
}
})
}

pub fn directives(&self) -> impl Iterator<Item = AstReader<'a, DirectiveId>> + 'a {
self.ast
.lookup(self.id)
.directives
.iter()
.map(|id| self.ast.read(*id))
}
}

impl<'a> AstReader<'a, InputValueDefinitionId> {
Expand All @@ -143,6 +151,14 @@ impl<'a> AstReader<'a, InputValueDefinitionId> {
pub fn default_value(&self) -> Option<AstReader<'a, ValueId>> {
self.ast.lookup(self.id).default.map(|id| self.ast.read(id))
}

pub fn directives(&self) -> impl Iterator<Item = AstReader<'a, DirectiveId>> + 'a {
self.ast
.lookup(self.id)
.directives
.iter()
.map(|id| self.ast.read(*id))
}
}

impl<'a> AstReader<'a, DirectiveId> {
Expand Down
9 changes: 7 additions & 2 deletions cynic-parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,15 @@ mod tests {
#[test]
fn test_basic_object() {
insta::assert_snapshot!(
parse_type_system_document("type MyType @hello { field: Whatever, other: [[Int!]]! }").to_sdl(),
parse_type_system_document(r#"
type MyType @hello {
field: Whatever @hello(name: ["string"]),
other: [[Int!]]!
}"#
).to_sdl(),
@r###"
type MyType @hello {
field: Whatever
field: Whatever@hello(name: ("string"))
other: [[Int!]]!
}
"###
Expand Down
3 changes: 2 additions & 1 deletion cynic-parser/src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ impl<'a> Pretty<'a, BoxAllocator> for NodeDisplay<'a, FieldDefinitionId> {
.append(allocator.text(":"))
.append(allocator.space())
.append(NodeDisplay(self.0.ty()))
.append(allocator.intersperse(self.0.directives().map(NodeDisplay), " "))
}
}

Expand Down Expand Up @@ -130,7 +131,7 @@ impl<'a> Pretty<'a, BoxAllocator> for NodeDisplay<'a, InputValueDefinitionId> {
.append(NodeDisplay(value));
}

builder
builder.append(allocator.intersperse(self.0.directives().map(NodeDisplay), " "))
}
}

Expand Down
18 changes: 10 additions & 8 deletions cynic-parser/src/schema.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ FieldsDefinition: Vec<NodeId> = {
};

FieldDefinition: NodeId = {
<description:StringValue?> <name:Name> <arguments:ArgumentsDefinition?> ":" <ty:Type> => ast.field_definition(FieldDefinition {
name,
ty,
arguments: arguments.unwrap_or_default(),
description,
})
<description:StringValue?> <name:Name> <arguments:ArgumentsDefinition?> ":" <ty:Type> <directives:Directive*> =>
ast.field_definition(FieldDefinition {
name,
ty,
arguments: arguments.unwrap_or_default(),
description,
directives
})
};

ArgumentsDefinition: Vec<NodeId> = {
Expand All @@ -68,9 +70,9 @@ InputFieldsDefinition: Vec<NodeId> = {
"{" <fields:InputValueDefinition+> "}" => fields
};

// TODO: Add directives
InputValueDefinition: NodeId =
<description:StringValue?> <name:Name> ":" <ty:Type> <default:DefaultValue?> => ast.input_value_definition(InputValueDefinition { <> });
<description:StringValue?> <name:Name> ":" <ty:Type> <default:DefaultValue?> <directives:Directive*> =>
ast.input_value_definition(InputValueDefinition { <> });

DefaultValue: ValueId = {
"=" <v:Value> => v
Expand Down

0 comments on commit 63f72a7

Please sign in to comment.