Skip to content

Commit

Permalink
Merge pull request #4138 from rmosolgo/fix-printing-empty-input-objects
Browse files Browse the repository at this point in the history
Don't print empty braces when input object has no arguments
  • Loading branch information
rmosolgo committed Jul 14, 2022
2 parents cefc000 + 23b3edc commit 0df1879
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
13 changes: 8 additions & 5 deletions lib/graphql/language/printer.rb
Expand Up @@ -236,12 +236,15 @@ def print_input_object_type_definition(input_object_type)
out = print_description(input_object_type)
out << "input #{input_object_type.name}"
out << print_directives(input_object_type.directives)
out << " {\n"
input_object_type.fields.each.with_index do |field, i|
out << print_description(field, indent: ' ', first_in_block: i == 0)
out << " #{print_input_value_definition(field)}\n"
if !input_object_type.fields.empty?
out << " {\n"
input_object_type.fields.each.with_index do |field, i|
out << print_description(field, indent: ' ', first_in_block: i == 0)
out << " #{print_input_value_definition(field)}\n"
end
out << "}"
end
out << "}"
out
end

def print_directive_definition(directive)
Expand Down
15 changes: 12 additions & 3 deletions spec/graphql/schema/printer_spec.rb
Expand Up @@ -70,6 +70,9 @@ class Media < GraphQL::Schema::Union
class NoFields < GraphQL::Schema::Object
end

class NoArguments < GraphQL::Schema::InputObject
end

class Query < GraphQL::Schema::Object
description "The query root of this schema"

Expand All @@ -80,7 +83,9 @@ class Query < GraphQL::Schema::Object
argument :deprecated_arg, String, required: false, deprecation_reason: "Use something else"
end

field :no_fields_type, NoFields
field :no_fields_type, NoFields do
argument :no_arguments_input, NoArguments
end
end

class CreatePost < GraphQL::Schema::RelayClassicMutation
Expand Down Expand Up @@ -531,6 +536,8 @@ class Subscription < GraphQL::Schema::Object
): CreatePostPayload
}
input NoArguments
type NoFields
interface Node {
Expand All @@ -552,7 +559,7 @@ class Subscription < GraphQL::Schema::Object
The query root of this schema
"""
type Query {
noFieldsType: NoFields
noFieldsType(noArgumentsInput: NoArguments!): NoFields
post(
deprecatedArg: String @deprecated(reason: "Use something else")
Expand Down Expand Up @@ -721,6 +728,8 @@ def foobar
): CreatePostPayload
}
input NoArguments
type NoFields
interface Node {
Expand All @@ -741,7 +750,7 @@ def foobar
The query root of this schema
"""
type Query {
noFieldsType: NoFields
noFieldsType(noArgumentsInput: NoArguments!): NoFields
post(
"""
Post ID
Expand Down

0 comments on commit 0df1879

Please sign in to comment.