Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions src/schema/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -412,6 +413,7 @@ pub enum DirectiveLocation {
InputObject,
InputFieldDefinition,
VariableDefinition,
DirectiveDefinition,
}

#[derive(Debug, Clone, PartialEq)]
Expand All @@ -420,6 +422,7 @@ pub struct DirectiveDefinition<'a, T: Text<'a>> {
pub description: Option<String>,
pub name: T::Value,
pub arguments: Vec<InputValue<'a, T>>,
pub directives: Vec<Directive<'a, T>>,
pub repeatable: bool,
pub locations: Vec<DirectiveLocation>,
}
Expand All @@ -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<Directive<'a, T>>,
}

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 {
Expand All @@ -464,6 +488,7 @@ impl DirectiveLocation {
InputObject => "INPUT_OBJECT",
InputFieldDefinition => "INPUT_FIELD_DEFINITION",
VariableDefinition => "VARIABLE_DEFINITION",
DirectiveLocation::DirectiveDefinition => "DIRECTIVE_DEFINITION",
}
}

Expand All @@ -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,
}
}

Expand Down Expand Up @@ -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),
};

Expand Down
16 changes: 16 additions & 0 deletions src/schema/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}
}
Expand Down Expand Up @@ -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");
}
Expand All @@ -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,
Expand All @@ -500,4 +515,5 @@ impl_display!(
InputObjectType,
InputObjectTypeExtension,
DirectiveDefinition,
DirectiveExtension,
);
107 changes: 98 additions & 9 deletions src/schema/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DirectiveExtension<'a, T>, 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()
Expand Down Expand Up @@ -645,6 +677,7 @@ where
DirectiveDefinition(ref mut d) => d.description = descr,
SchemaDefinition(_) => unreachable!(),
TypeExtension(_) => unreachable!(),
DirectiveExtension(_) => unreachable!(),
}
def
})
Expand All @@ -655,17 +688,31 @@ where
pub fn type_extension<'a, T>(
input: &mut TokenStream<'a>,
) -> StdParseResult<TypeExtension<'a, T>, 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<Definition<'a, T>, 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()
Expand All @@ -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)
Expand Down Expand Up @@ -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::<String>("extend directive @a").is_err());
}
}
8 changes: 8 additions & 0 deletions tests/schema_roundtrips.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
5 changes: 5 additions & 0 deletions tests/schemas/directive_directives.graphql
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions tests/schemas/extend_directive.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
extend directive @foo @deprecated(reason: "No longer supported")