Skip to content

Commit 96217da

Browse files
committed
Allow descriptions for directives (fixes #11)
1 parent 3547f60 commit 96217da

File tree

4 files changed

+63
-23
lines changed

4 files changed

+63
-23
lines changed

src/schema/grammar.rs

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ pub fn object_type<'a>(input: &mut TokenStream<'a>)
191191
ObjectType {
192192
position, name, directives, fields,
193193
implements_interfaces: interfaces,
194-
description: None, // is filled in type_definition
194+
description: None, // is filled in described_definition
195195
}
196196
})
197197
.parse_stream(input)
@@ -237,7 +237,7 @@ pub fn interface_type<'a>(input: &mut TokenStream<'a>)
237237
.map(|(position, name, directives, fields)| {
238238
InterfaceType {
239239
position, name, directives, fields,
240-
description: None, // is filled in type_definition
240+
description: None, // is filled in described_definition
241241
}
242242
})
243243
.parse_stream(input)
@@ -288,7 +288,7 @@ pub fn union_type<'a>(input: &mut TokenStream<'a>)
288288
UnionType {
289289
position, name, directives,
290290
types: types.unwrap_or_else(Vec::new),
291-
description: None, // is filled in type_definition
291+
description: None, // is filled in described_definition
292292
}
293293
})
294294
.parse_stream(input)
@@ -351,7 +351,7 @@ pub fn enum_type<'a>(input: &mut TokenStream<'a>)
351351
EnumType {
352352
position, name, directives,
353353
values: values.unwrap_or_else(Vec::new),
354-
description: None, // is filled in type_definition
354+
description: None, // is filled in described_definition
355355
}
356356
})
357357
.parse_stream(input)
@@ -402,7 +402,7 @@ pub fn input_object_type<'a>(input: &mut TokenStream<'a>)
402402
.map(|(position, name, directives, fields)| {
403403
InputObjectType {
404404
position, name, directives, fields,
405-
description: None, // is filled in type_definition
405+
description: None, // is filled in described_definition
406406
}
407407
})
408408
.parse_stream(input)
@@ -459,39 +459,47 @@ pub fn directive_definition<'a>(input: &mut TokenStream<'a>)
459459
.map(|(position, name, arguments, locations)| {
460460
DirectiveDefinition {
461461
position, name, arguments, locations,
462-
description: None, // is filled in type_definition
462+
description: None, // is filled in described_definition
463463
}
464464
})
465465
.parse_stream(input)
466466
}
467467

468-
pub fn type_definition<'a>(input: &mut TokenStream<'a>)
469-
-> ParseResult<TypeDefinition, TokenStream<'a>>
468+
pub fn described_definition<'a>(input: &mut TokenStream<'a>)
469+
-> ParseResult<Definition, TokenStream<'a>>
470470
{
471471
use self::TypeDefinition::*;
472472
(
473473
optional(parser(string)),
474474
choice((
475-
parser(scalar_type).map(Scalar),
476-
parser(object_type).map(Object),
477-
parser(interface_type).map(Interface),
478-
parser(union_type).map(Union),
479-
parser(enum_type).map(Enum),
480-
parser(input_object_type).map(InputObject),
481-
)),
475+
choice((
476+
parser(scalar_type).map(Scalar),
477+
parser(object_type).map(Object),
478+
parser(interface_type).map(Interface),
479+
parser(union_type).map(Union),
480+
parser(enum_type).map(Enum),
481+
parser(input_object_type).map(InputObject),
482+
)).map(Definition::TypeDefinition),
483+
parser(directive_definition).map(Definition::DirectiveDefinition),
484+
))
482485
)
483486
// We can't set description inside type definition parser, because
484487
// that means parser will need to backtrace, and that in turn
485488
// means that error reporting is bad (along with performance)
486489
.map(|(descr, mut def)| {
487490
use schema::ast::TypeDefinition::*;
491+
use schema::ast::Definition::*;
492+
use schema::ast::Definition::{TypeDefinition as T};
488493
match def {
489-
Scalar(ref mut s) => s.description = descr,
490-
Object(ref mut o) => o.description = descr,
491-
Interface(ref mut i) => i.description = descr,
492-
Union(ref mut u) => u.description = descr,
493-
Enum(ref mut e) => e.description = descr,
494-
InputObject(ref mut o) => o.description = descr,
494+
T(Scalar(ref mut s)) => s.description = descr,
495+
T(Object(ref mut o)) => o.description = descr,
496+
T(Interface(ref mut i)) => i.description = descr,
497+
T(Union(ref mut u)) => u.description = descr,
498+
T(Enum(ref mut e)) => e.description = descr,
499+
T(InputObject(ref mut o)) => o.description = descr,
500+
DirectiveDefinition(ref mut d) => d.description = descr,
501+
SchemaDefinition(_) => unreachable!(),
502+
TypeExtension(_) => unreachable!(),
495503
}
496504
def
497505
})
@@ -519,9 +527,8 @@ pub fn definition<'a>(input: &mut TokenStream<'a>)
519527
{
520528
choice((
521529
parser(schema).map(Definition::SchemaDefinition),
522-
parser(type_definition).map(Definition::TypeDefinition),
523530
parser(type_extension).map(Definition::TypeExtension),
524-
parser(directive_definition).map(Definition::DirectiveDefinition),
531+
parser(described_definition),
525532
)).parse_stream(input)
526533
}
527534

tests/schema_roundtrips.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,4 @@ fn roundtrip2(filename: &str) {
4848
#[test] fn extend_input() { roundtrip2("extend_input"); }
4949
#[test] fn directive() { roundtrip("directive"); }
5050
#[test] fn kitchen_sink() { roundtrip2("kitchen-sink"); }
51+
#[test] fn directive_descriptions() { roundtrip2("directive_descriptions"); }
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
Directs the executor to include this field or fragment only when the `if` argument is true.
3+
"""
4+
directive @include(
5+
"""
6+
Included when true.
7+
"""
8+
if: Boolean!
9+
) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
10+
11+
"""
12+
Directs the executor to skip this field or fragment when the `if` argument is true.
13+
"""
14+
directive @skip(
15+
"""
16+
Skipped when true.
17+
"""
18+
if: Boolean!
19+
) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""
2+
Directs the executor to include this field or fragment only when the `if` argument is true.
3+
"""
4+
directive @include("""
5+
Included when true.
6+
""" if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
7+
8+
"""
9+
Directs the executor to skip this field or fragment when the `if` argument is true.
10+
"""
11+
directive @skip("""
12+
Skipped when true.
13+
""" if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT

0 commit comments

Comments
 (0)