Skip to content

Commit

Permalink
fix: input types loose out on provided graphQL documentation (#2015)
Browse files Browse the repository at this point in the history
Co-authored-by: Tushar Mathur <[email protected]>
  • Loading branch information
emekaokoli19 and tusharmath authored May 31, 2024
1 parent 0fa7e10 commit 27ea39a
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 12 deletions.
16 changes: 12 additions & 4 deletions src/core/blueprint/into_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ fn to_type(def: &Definition) -> dynamic::Type {
}
object = object.field(dyn_schema_field);
}
if let Some(description) = &def.description {
object = object.description(description);
}
for interface in def.implements.iter() {
object = object.implement(interface.clone());
}
Expand All @@ -115,10 +118,15 @@ fn to_type(def: &Definition) -> dynamic::Type {
Definition::InputObject(def) => {
let mut input_object = dynamic::InputObject::new(def.name.clone());
for field in def.fields.iter() {
input_object = input_object.field(dynamic::InputValue::new(
field.name.clone(),
to_type_ref(&field.of_type),
));
let mut input_field =
dynamic::InputValue::new(field.name.clone(), to_type_ref(&field.of_type));
if let Some(description) = &field.description {
input_field = input_field.description(description);
}
input_object = input_object.field(input_field);
}
if let Some(description) = &def.description {
input_object = input_object.description(description);
}

dynamic::Type::InputObject(input_object)
Expand Down
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
48 changes: 48 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,48 @@
---
source: tests/core/spec.rs
assertion_line: 278
expression: formatted
---
scalar Date

scalar Email

scalar Empty

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

scalar JSON

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

scalar PhoneNumber

type Post {
body: String
id: Int!
}

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

scalar Url

schema {
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}}")
}
```

1 comment on commit 27ea39a

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running 30s test @ http://localhost:8000/graphql

4 threads and 100 connections

Thread Stats Avg Stdev Max +/- Stdev
Latency 6.58ms 2.95ms 85.12ms 71.92%
Req/Sec 3.84k 143.05 4.94k 81.58%

458783 requests in 30.02s, 2.30GB read

Requests/sec: 15283.14

Transfer/sec: 78.44MB

Please sign in to comment.