8000 fix: generate description of directives. (#1681) · async-graphql/async-graphql@3001a02 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3001a02

Browse files
mythrnrsunli829
andauthored
fix: generate description of directives. (#1681)
* fix: generate description of directives. trigger action fix: expected value of directive test. And use fixed version of poem for MSRV. fix: expected value of directive test. fix: expected value of directive test. fix: modify test fixtures. * fix tests --------- Co-authored-by: Sunli <sunlipad4@icloud.com> Co-authored-by: Sunli <scott_s829@163.com>
1 parent fd48bc2 commit 3001a02

11 files changed

+66
-9
lines changed

src/registry/export_sdl.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ impl Registry {
186186
return;
187187
}
188188

189-
writeln!(sdl, "{}", directive.sdl()).ok();
189+
writeln!(sdl, "{}", directive.sdl(&options)).ok();
190190
});
191191

192192
if options.federation {
@@ -703,7 +703,7 @@ impl Registry {
703703
}
704704
}
705705

706-
fn write_description(
706+
pub(super) fn write_description(
707707
sdl: &mut String,
708708
options: &SDLExportOptions,
709709
level: usize,

src/registry/mod.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -727,8 +727,15 @@ pub struct MetaDirective {
727727
}
728728

729729
impl MetaDirective {
730-
pub(crate) fn sdl(&self) -> String {
731-
let mut sdl = format!("directive @{}", self.name);
730+
pub(crate) fn sdl(&self, options: &SDLExportOptions) -> String {
731+
let mut sdl = String::new();
732+
733+
if let Some(description) = &self.description {
734+
self::export_sdl::write_description(&mut sdl, options, 0, description);
735+
}
736+
737+
write!(sdl, "directive @{}", self.name).ok();
738+
732739
if !self.args.is_empty() {
733740
let args = self
734741
.args
@@ -903,8 +910,8 @@ impl Registry {
903910
self.add_directive(MetaDirective {
904911
name: "oneOf".into(),
905912
description: Some(
906-
"Indicates that an Input Object is a OneOf Input Object (and thus requires
907-
exactly one of its field be provided)"
913+
"Indicates that an Input Object is a OneOf Input Object (and thus requires \
914+
exactly one of its field be provided)"
908915
.to_string(),
909916
),
910917
locations: vec![__DirectiveLocation::INPUT_OBJECT],

tests/directive.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub async fn test_directive_skip() {
2020
value5: value @skip(if: true)
2121
value6: value @skip(if: false)
2222
}
23-
23+
2424
query {
2525
value1: value @skip(if: true)
2626
value2: value @skip(if: false)
@@ -166,7 +166,13 @@ pub async fn test_includes_deprecated_directive() {
166166

167167
let schema = Schema::new(Query, EmptyMutation, EmptySubscription);
168168

169-
assert!(schema.sdl().contains(r#"directive @deprecated(reason: String = "No longer supported") on FIELD_DEFINITION | ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION | ENUM_VALUE"#))
169+
assert!(schema.sdl().contains(
170+
r#"
171+
"""
172+
Marks an element of a GraphQL schema as no longer supported.
173+
"""
174+
directive @deprecated(reason: String = "No longer supported") on FIELD_DEFINITION | ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION | ENUM_VALUE"#,
175+
))
170176
}
171177

172178
#[tokio::test]

tests/export_sdl.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ async fn test_spaces() {
3838
.sorted_fields()
3939
.sorted_enum_items(),
4040
);
41-
std::fs::write("./test_space_schema.graphql", &sdl).unwrap();
4241

4342
let expected = include_str!("schemas/test_space_schema.graphql");
4443
assert_eq!(sdl, expected);

tests/schemas/test_dynamic_schema.graphql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,13 @@ scalar TestScalar @json
3535

3636
union TestUnion @wrap = OutputType | OutputType2
3737

38+
"""
39+
Directs the executor to include this field or fragment only when the `if` argument is true.
40+
"""
3841
directive @include(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
42+
"""
43+
Directs the executor to skip this field or fragment when the `if` argument is true.
44+
"""
3945
directive @skip(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
4046
schema {
4147
query: Query

tests/schemas/test_entity_inaccessible.schema.graphql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,13 @@ extend type Query {
6060
inaccessibleCustomObject: MyCustomObjInaccessible!
6161
}
6262

63+
"""
64+
Directs the executor to include this field or fragment only when the `if` argument is true.
65+
"""
6366
directive @include(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
67+
"""
68+
Directs the executor to skip this field or fragment when the `if` argument is true.
69+
"""
6470
directive @skip(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
6571
extend schema @link(
6672
url: "https://specs.apollo.dev/federation/v2.3",

tests/schemas/test_entity_tag.schema.graphql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,13 @@ extend type Query {
6060
taggedCustomObject: MyCustomObjTagged!
6161
}
6262

63+
"""
64+
Directs the executor to include this field or fragment only when the `if` argument is true.
65+
"""
6366
directive @include(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
67+
"""
68+
Directs the executor to skip this field or fragment when the `if` argument is true.
69+
"""
6470
directive @skip(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
6571
extend schema @link(
6672
url: "https://specs.apollo.dev/federation/v2.3",

tests/schemas/test_fed2_compose.schema.graphql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,14 @@ type Subscription @testDirective(scope: "object type", input: 3) {
1212
anotherValue: SimpleValue!
1313
}
1414

15+
"""
16+
Directs the executor to include this field or fragment only when the `if` argument is true.
17+
"""
1518
directive @include(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
1619
directive @noArgsDirective on FIELD_DEFINITION
20+
"""
21+
Directs the executor to skip this field or fragment when the `if` argument is true.
22+
"""
1723
directive @skip(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
1824
directive @testDirective(scope: String!, input: Int!, opt: Int) on FIELD_DEFINITION | OBJECT
1925
extend schema @link(

tests/schemas/test_fed2_compose_2.schema.graphql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,17 @@ type TestSimpleObject @type_directive_object(description: "This is OBJECT in Sim
3939
field: String! @type_directive_field_definition(description: "This is FIELD_DEFINITION in SimpleObject")
4040
}
4141

42+
"""
43+
Directs the executor to include this field or fragment only when the `if` argument is true.
44+
"""
4245
directive @include(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
46+
"""
47+
Indicates that an Input Object is a OneOf Input Object (and thus requires exactly one of its field be provided)
48+
"""
4349
directive @oneOf on INPUT_OBJECT
50+
"""
51+
Directs the executor to skip this field or fragment when the `if` argument is true.
52+
"""
4453
directive @skip(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
4554
directive @type_directive_argument_definition(description: String!) on ARGUMENT_DEFINITION
4655
directive @type_directive_enum(description: String!) on ENUM

tests/schemas/test_fed2_link.schema.graphqls

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ extend type User @key(fields: "id") {
1414
reviews: [Review!]!
1515
}
1616

17+
"""
18+
Directs the executor to include this field or fragment only when the `if` argument is true.
19+
"""
1720
directive @include(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
21+
"""
22+
Directs the executor to skip this field or fragment when the `if` argument is true.
23+
"""
1824
directive @skip(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
1925
extend schema @link(
2026
url: "https://specs.apollo.dev/federation/v2.3",

0 commit comments

Comments
 (0)
0