diff --git a/src/schema/ast.rs b/src/schema/ast.rs index db2908f..96b7815 100644 --- a/src/schema/ast.rs +++ b/src/schema/ast.rs @@ -35,6 +35,7 @@ pub enum Definition<'a, T: Text<'a>> { TypeDefinition(TypeDefinition<'a, T>), TypeExtension(TypeExtension<'a, T>), DirectiveDefinition(DirectiveDefinition<'a, T>), + DirectiveExtension(DirectiveExtension<'a, T>), } #[derive(Debug, Clone, Default, PartialEq)] @@ -412,6 +413,7 @@ pub enum DirectiveLocation { InputObject, InputFieldDefinition, VariableDefinition, + DirectiveDefinition, } #[derive(Debug, Clone, PartialEq)] @@ -420,6 +422,7 @@ pub struct DirectiveDefinition<'a, T: Text<'a>> { pub description: Option, pub name: T::Value, pub arguments: Vec>, + pub directives: Vec>, pub repeatable: bool, pub locations: Vec, } @@ -434,12 +437,33 @@ where description: None, name, arguments: vec![], + directives: vec![], repeatable: false, locations: vec![], } } } +#[derive(Debug, Clone, PartialEq)] +pub struct DirectiveExtension<'a, T: Text<'a>> { + pub position: Pos, + pub name: T::Value, + pub directives: Vec>, +} + +impl<'a, T> DirectiveExtension<'a, T> +where + T: Text<'a>, +{ + pub fn new(name: T::Value) -> Self { + Self { + position: Pos::default(), + name, + directives: vec![], + } + } +} + impl DirectiveLocation { /// Returns GraphQL syntax compatible name of the directive pub fn as_str(&self) -> &'static str { @@ -464,6 +488,7 @@ impl DirectiveLocation { InputObject => "INPUT_OBJECT", InputFieldDefinition => "INPUT_FIELD_DEFINITION", VariableDefinition => "VARIABLE_DEFINITION", + DirectiveLocation::DirectiveDefinition => "DIRECTIVE_DEFINITION", } } @@ -474,8 +499,19 @@ impl DirectiveLocation { Query | Mutation | Subscription | Field | FragmentDefinition | FragmentSpread | InlineFragment => true, - Schema | Scalar | Object | FieldDefinition | ArgumentDefinition | Interface | Union - | Enum | EnumValue | InputObject | InputFieldDefinition | VariableDefinition => false, + Schema + | Scalar + | Object + | FieldDefinition + | ArgumentDefinition + | Interface + | Union + | Enum + | EnumValue + | InputObject + | InputFieldDefinition + | VariableDefinition + | DirectiveLocation::DirectiveDefinition => false, } } @@ -513,6 +549,7 @@ impl FromStr for DirectiveLocation { "INPUT_OBJECT" => InputObject, "INPUT_FIELD_DEFINITION" => InputFieldDefinition, "VARIABLE_DEFINITION" => VariableDefinition, + "DIRECTIVE_DEFINITION" => DirectiveLocation::DirectiveDefinition, _ => return Err(InvalidDirectiveLocation), }; diff --git a/src/schema/format.rs b/src/schema/format.rs index 6ebc945..2ded4c8 100644 --- a/src/schema/format.rs +++ b/src/schema/format.rs @@ -54,6 +54,7 @@ where Definition::TypeDefinition(ref t) => t.display(f), Definition::TypeExtension(ref e) => e.display(f), Definition::DirectiveDefinition(ref d) => d.display(f), + Definition::DirectiveExtension(ref d) => d.display(f), } } } @@ -459,6 +460,7 @@ where f.write("directive @"); f.write(self.name.as_ref()); format_arguments(&self.arguments, f); + format_directives(&self.directives, f); if self.repeatable { f.write(" repeatable"); } @@ -478,6 +480,19 @@ where } } +impl<'a, T> Displayable for DirectiveExtension<'a, T> +where + T: Text<'a>, +{ + fn display(&self, f: &mut Formatter) { + f.indent(); + f.write("extend directive @"); + f.write(self.name.as_ref()); + format_directives(&self.directives, f); + f.endline(); + } +} + impl_display!( 'a Document, @@ -500,4 +515,5 @@ impl_display!( InputObjectType, InputObjectTypeExtension, DirectiveDefinition, + DirectiveExtension, ); diff --git a/src/schema/grammar.rs b/src/schema/grammar.rs index 263bfa4..7a45111 100644 --- a/src/schema/grammar.rs +++ b/src/schema/grammar.rs @@ -589,18 +589,50 @@ where position(), ident("directive").and(punct("@")).with(name::<'a, T>()), parser(arguments_definition), + parser(directives), optional(ident("repeatable")), ident("on").with(parser(directive_locations)), ) - .map(|(position, name, arguments, repeatable, locations)| { - DirectiveDefinition { + .map( + |(position, name, arguments, directives, repeatable, locations)| DirectiveDefinition { position, name, arguments, + directives, locations, repeatable: repeatable.is_some(), description: None, // is filled in described_definition + }, + ) + .parse_stream(input) + .into_result() +} + +pub fn directive_extension<'a, T>( + input: &mut TokenStream<'a>, +) -> StdParseResult, TokenStream<'a>> +where + T: Text<'a>, +{ + ( + position(), + ident("directive").and(punct("@")).with(name::<'a, T>()), + parser(directives), + ) + .flat_map(|(position, name, directives)| { + if directives.is_empty() { + let mut e = Errors::empty(position); + e.add_error(Error::expected_static_message( + "Directive extension should contain at least \ + one directive.", + )); + return Err(e); } + Ok(DirectiveExtension { + position, + name, + directives, + }) }) .parse_stream(input) .into_result() @@ -645,6 +677,7 @@ where DirectiveDefinition(ref mut d) => d.description = descr, SchemaDefinition(_) => unreachable!(), TypeExtension(_) => unreachable!(), + DirectiveExtension(_) => unreachable!(), } def }) @@ -655,17 +688,31 @@ where pub fn type_extension<'a, T>( input: &mut TokenStream<'a>, ) -> StdParseResult, TokenStream<'a>> +where + T: Text<'a>, +{ + choice(( + parser(scalar_type_extension).map(TypeExtension::Scalar), + parser(object_type_extension).map(TypeExtension::Object), + parser(interface_type_extension).map(TypeExtension::Interface), + parser(union_type_extension).map(TypeExtension::Union), + parser(enum_type_extension).map(TypeExtension::Enum), + parser(input_object_type_extension).map(TypeExtension::InputObject), + )) + .parse_stream(input) + .into_result() +} + +pub fn extension<'a, T>( + input: &mut TokenStream<'a>, +) -> StdParseResult, TokenStream<'a>> where T: Text<'a>, { ident("extend") .with(choice(( - parser(scalar_type_extension).map(TypeExtension::Scalar), - parser(object_type_extension).map(TypeExtension::Object), - parser(interface_type_extension).map(TypeExtension::Interface), - parser(union_type_extension).map(TypeExtension::Union), - parser(enum_type_extension).map(TypeExtension::Enum), - parser(input_object_type_extension).map(TypeExtension::InputObject), + parser(type_extension).map(Definition::TypeExtension), + parser(directive_extension).map(Definition::DirectiveExtension), ))) .parse_stream(input) .into_result() @@ -679,7 +726,7 @@ where { choice(( parser(schema).map(Definition::SchemaDefinition), - parser(type_extension).map(Definition::TypeExtension), + parser(extension), parser(described_definition), )) .parse_stream(input) @@ -727,4 +774,46 @@ mod test { } ); } + + #[test] + fn directive_definition_directives() { + let doc = ast( + "directive @a @deprecated(reason: \"x\") repeatable on FIELD | DIRECTIVE_DEFINITION", + ); + match &doc.definitions[0] { + Definition::DirectiveDefinition(d) => { + assert_eq!(d.name, "a"); + assert_eq!(d.directives.len(), 1); + assert_eq!(d.directives[0].name, "deprecated"); + assert!(d.repeatable); + assert_eq!( + d.locations, + vec![ + DirectiveLocation::Field, + DirectiveLocation::DirectiveDefinition + ] + ); + } + other => panic!("unexpected definition: {:?}", other), + } + } + + #[test] + fn directive_extension() { + let doc = ast("extend directive @a @deprecated"); + match &doc.definitions[0] { + Definition::DirectiveExtension(d) => { + assert_eq!(d.position, Pos { line: 1, column: 8 }); + assert_eq!(d.name, "a"); + assert_eq!(d.directives.len(), 1); + assert_eq!(d.directives[0].name, "deprecated"); + } + other => panic!("unexpected definition: {:?}", other), + } + } + + #[test] + fn directive_extension_without_directives() { + assert!(parse_schema::("extend directive @a").is_err()); + } } diff --git a/tests/schema_roundtrips.rs b/tests/schema_roundtrips.rs index 2b157a3..372bc63 100644 --- a/tests/schema_roundtrips.rs +++ b/tests/schema_roundtrips.rs @@ -123,3 +123,11 @@ fn directive_variable_definition() { fn repeatable() { roundtrip("repeatable") } +#[test] +fn directive_directives() { + roundtrip("directive_directives") +} +#[test] +fn extend_directive() { + roundtrip("extend_directive") +} diff --git a/tests/schemas/directive_directives.graphql b/tests/schemas/directive_directives.graphql new file mode 100644 index 0000000..0e73dee --- /dev/null +++ b/tests/schemas/directive_directives.graphql @@ -0,0 +1,5 @@ +directive @onDirective on DIRECTIVE_DEFINITION + +directive @foo @onDirective on OBJECT + +directive @bar(arg: String) @deprecated(reason: "Use `@baz`.") repeatable on FIELD | OBJECT diff --git a/tests/schemas/extend_directive.graphql b/tests/schemas/extend_directive.graphql new file mode 100644 index 0000000..f9e523f --- /dev/null +++ b/tests/schemas/extend_directive.graphql @@ -0,0 +1 @@ +extend directive @foo @deprecated(reason: "No longer supported")