Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: input types loose out on provided graphQL documentation #2015

Merged
merged 9 commits into from
May 31, 2024
15 changes: 11 additions & 4 deletions src/core/config/from_document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,12 @@ fn to_types(
)
.some(),
TypeKind::Enum(_) => Valid::none(),
TypeKind::InputObject(input_object_type) => {
to_input_object(input_object_type, &type_definition.node.directives).some()
}
TypeKind::InputObject(input_object_type) => to_input_object(
input_object_type,
&type_definition.node.description,
&type_definition.node.directives,
)
.some(),
TypeKind::Union(_) => Valid::none(),
TypeKind::Scalar => Valid::succeed(Some(to_scalar_type())),
}
Expand Down Expand Up @@ -252,11 +255,15 @@ where
}
fn to_input_object(
input_object_type: InputObjectType,
description: &Option<Positioned<String>>,
directives: &[Positioned<ConstDirective>],
) -> Valid<config::Type, String> {
to_input_object_fields(&input_object_type.fields)
.fuse(Protected::from_directives(directives.iter()))
.map(|(fields, protected)| config::Type { fields, protected, ..Default::default() })
.map(|(fields, protected)| {
let doc = description.to_owned().map(|pos| pos.node);
config::Type { fields, protected, doc, ..Default::default() }
})
}

fn to_fields_inner<T, F>(
Expand Down
2 changes: 1 addition & 1 deletion src/core/config/into_document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ fn config_document(config: &ConfigModule) -> ServiceDocument {
};
definitions.push(TypeSystemDefinition::Type(pos(TypeDefinition {
extend: false,
description: None,
description: type_def.doc.clone().map(pos),
name: pos(Name::new(type_name.clone())),
directives: type_def
.added_fields
Expand Down
20 changes: 17 additions & 3 deletions src/core/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,12 @@ fn print_type_def(type_def: &TypeDefinition) -> String {
}
TypeKind::InputObject(input) => {
let directives = print_directives(&type_def.directives);
let doc = type_def.description.as_ref().map_or(String::new(), |d| {
format!(r#" """{} {}{} """{}"#, "\n", d.node, "\n", "\n")
});
format!(
"input {} {}{{\n{}\n}}\n",
"{}input {} {}{{\n{}\n}}\n",
doc,
type_def.name.node,
directives,
input
Expand Down Expand Up @@ -145,8 +149,12 @@ fn print_type_def(type_def: &TypeDefinition) -> String {
String::new()
};
let directives = print_directives(&type_def.directives);
let doc = type_def.description.as_ref().map_or(String::new(), |d| {
format!(r#" """{} {}{} """{}"#, "\n", d.node, "\n", "\n")
});
format!(
"type {} {}{}{{\n{}\n}}\n",
"{}type {} {}{}{{\n{}\n}}\n",
doc,
type_def.name.node,
implements,
directives,
Expand Down Expand Up @@ -209,7 +217,13 @@ fn print_field(field: &async_graphql::parser::types::FieldDefinition) -> String

fn print_input_value(field: &async_graphql::parser::types::InputValueDefinition) -> String {
let directives_str = print_directives(&field.directives);
format!(" {}: {}{}", field.name.node, field.ty.node, directives_str)
let doc = field.description.as_ref().map_or(String::new(), |d| {
format!(r#" """{} {}{} """{}"#, "\n", d.node, "\n", "\n")
});
format!(
"{} {}: {}{}",
doc, field.name.node, field.ty.node, directives_str
)
}
fn print_directive(directive: &DirectiveDefinition) -> String {
let args = directive
Expand Down
39 changes: 39 additions & 0 deletions tests/core/snapshots/test-input-documentation.md_client.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
source: tests/core/spec.rs
assertion_line: 278
expression: formatted
---
scalar Date

scalar Email

scalar Empty

input Foo {
id: Int
}

scalar JSON

type Mutation {
testDocumentation(input: Foo!): Post
}

scalar PhoneNumber

type Post {
body: String
id: Int!
}

type Query {
foo: String
postFromFoo(id: Int!): Post
}

scalar Url

schema {
emekaokoli19 marked this conversation as resolved.
Show resolved Hide resolved
query: Query
mutation: Mutation
}
36 changes: 36 additions & 0 deletions tests/core/snapshots/test-input-documentation.md_merged.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
source: tests/core/spec.rs
assertion_line: 235
expression: formatter
---
schema @server @upstream(baseURL: "http://jsonplaceholder.typicode.com") {
query: Query
mutation: Mutation
}

"""
Test input documentation
"""
input Foo {
"""
Test input field documentation
"""
id: Int
}

type Mutation {
testDocumentation(input: Foo!): Post @http(body: "{{.args.input}}", method: "POST", path: "/posts")
}

type Post {
body: String
id: Int!
}

"""
Some Documentation
"""
type Query {
foo: String @http(path: "/foo")
postFromFoo(id: Int!): Post @http(path: "/posts?id={{.args.id}}")
}
39 changes: 39 additions & 0 deletions tests/execution/test-input-documentation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
identity: true
---

# test-input-type-documentation

```graphql @config
schema @server @upstream(baseURL: "http://jsonplaceholder.typicode.com") {
query: Query
mutation: Mutation
}

"""
Test input documentation
"""
input Foo {
"""
Test input field documentation
"""
id: Int
}

type Mutation {
testDocumentation(input: Foo!): Post @http(body: "{{.args.input}}", method: "POST", path: "/posts")
}

type Post {
body: String
id: Int!
}

"""
Some Documentation
"""
type Query {
foo: String @http(path: "/foo")
postFromFoo(id: Int!): Post @http(path: "/posts?id={{.args.id}}")
}
```
Loading