Module: GraphQL::Schema::Printer::TypeKindPrinters::ArgsPrinter

Includes:
DescriptionPrinter
Included in:
DirectivePrinter, FieldPrinter
Defined in:
lib/graphql/schema/printer.rb

Instance Method Summary collapse

Methods included from DescriptionPrinter

#print_description

Instance Method Details



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/graphql/schema/printer.rb', line 127

def print_args(warden, field, indentation = '')
  arguments = warden.arguments(field)
  return if arguments.empty?

  if arguments.all?{ |arg| !arg.description }
    return "(#{arguments.map{ |arg| print_input_value(arg) }.join(", ")})"
  end

  out = "(\n".dup
  out << arguments.map.with_index{ |arg, i|
    "#{print_description(arg, "  #{indentation}", i == 0)}  #{indentation}"\
    "#{print_input_value(arg)}"
  }.join("\n")
  out << "\n#{indentation})"
end


143
144
145
146
147
148
149
150
151
# File 'lib/graphql/schema/printer.rb', line 143

def print_input_value(arg)
  if arg.default_value?
    default_string = " = #{print_value(arg.default_value, arg.type)}"
  else
    default_string = nil
  end

  "#{arg.name}: #{arg.type.to_s}#{default_string}"
end


153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/graphql/schema/printer.rb', line 153

def print_value(value, type)
  case type
  when FLOAT_TYPE
    return 'null' if value.nil?
    value.to_f.inspect
  when INT_TYPE
    return 'null' if value.nil?
    value.to_i.inspect
  when BOOLEAN_TYPE
    return 'null' if value.nil?
    (!!value).inspect
  when ScalarType, ID_TYPE, STRING_TYPE
    return 'null' if value.nil?
    value.to_s.inspect
  when EnumType
    return 'null' if value.nil?
    type.coerce_result(value)
  when InputObjectType
    return 'null' if value.nil?
    fields = value.to_h.map{ |field_name, field_value|
      field_type = type.input_fields.fetch(field_name.to_s).type
      "#{field_name}: #{print_value(field_value, field_type)}"
    }.join(", ")
    "{#{fields}}"
  when NonNullType
    print_value(value, type.of_type)
  when ListType
    return 'null' if value.nil?
    "[#{value.to_a.map{ |v| print_value(v, type.of_type) }.join(", ")}]"
  else
    raise NotImplementedError, "Unexpected value type #{type.inspect}"
  end
end