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


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/graphql/schema/printer.rb', line 105

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

  field_arguments = field.arguments.values

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

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

122
123
124
125
126
127
128
129
130
# File 'lib/graphql/schema/printer.rb', line 122

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

132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/graphql/schema/printer.rb', line 132

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