Module: GraphQL::Schema::Printer::TypeKindPrinters::FieldPrinter

Included in:
InputObjectPrinter, InterfacePrinter, ObjectPrinter
Defined in:
lib/graphql/schema/printer.rb

Instance Method Summary collapse

Instance Method Details



65
66
67
68
# File 'lib/graphql/schema/printer.rb', line 65

def print_args(field)
  return if field.arguments.empty?
  "(#{field.arguments.values.map{ |arg| print_input_value(arg) }.join(", ")})"
end


61
62
63
# File 'lib/graphql/schema/printer.rb', line 61

def print_fields(type)
  type.all_fields.map{ |field| "  #{field.name}#{print_args(field)}: #{field.type}" }.join("\n")
end


70
71
72
73
74
75
76
77
78
# File 'lib/graphql/schema/printer.rb', line 70

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

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


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/graphql/schema/printer.rb', line 80

def print_value(value, type)
  case type
  when FLOAT_TYPE
    value.to_f.inspect
  when INT_TYPE
    value.to_i.inspect
  when BOOLEAN_TYPE
    (!!value).inspect
  when ScalarType, ID_TYPE, STRING_TYPE
    value.to_s.inspect
  when EnumType
    value.to_s
  when InputObjectType
    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
    "[#{value.to_a.map{ |v| print_value(v, type.of_type) }.join(", ")}]"
  else
    raise NotImplementedError, "Unexpected value type #{type.inspect}"
  end
end