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



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

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


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

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


205
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
# File 'lib/graphql/schema/printer.rb', line 205

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