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



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/graphql/schema/printer.rb', line 180

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.sort_by(&:name).map.with_index{ |arg, i|
    "#{print_description(arg, "  #{indentation}", i == 0)}  #{indentation}"\
    "#{print_input_value(arg)}"
  }.join("\n")
  out << "\n#{indentation})"
end


196
197
198
199
200
201
202
203
204
# File 'lib/graphql/schema/printer.rb', line 196

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


206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/graphql/schema/printer.rb', line 206

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_isolated_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