class Mirah::BaseInputObject
This object provides a DSL by which you can easy create plain ruby objects with `attr_accessor` that will automatically be transformed back and forth from a GraphQL endpoint. This includes changing the format of the key from `ruby_style` to `graphqlStyle` and back again, and extra serialization where necessary for scalar types such as dates.
Public Class Methods
from_graphql_hash(graphql_data)
click to toggle source
# File lib/mirah/base_input_object.rb, line 38 def self.from_graphql_hash(graphql_data) return nil unless graphql_data attrs = inputs.each_with_object({}) do |input, obj| value = graphql_data[input[:graphql_name]] obj[input[:name]] = input[:serializer].deserialize(value) end new(attrs) end
input(name, required:, serializer: Serializers::ScalarSerializer.new)
click to toggle source
@private
# File lib/mirah/base_input_object.rb, line 55 def self.input(name, required:, serializer: Serializers::ScalarSerializer.new) inputs.push( { name: name, serializer: serializer, required: required, graphql_name: name.to_s.camelize(:lower) } ) attr_accessor name end
inputs()
click to toggle source
@private
# File lib/mirah/base_input_object.rb, line 50 def self.inputs @inputs ||= [] end
new(attrs = {})
click to toggle source
# File lib/mirah/base_input_object.rb, line 9 def initialize(attrs = {}) attrs.each do |key, value| raise Errors::InvalidParameter.new(key), "#{key} is not a valid parameter" unless respond_to? "#{key}=" send("#{key}=", value) end end
Public Instance Methods
to_graphql_hash()
click to toggle source
# File lib/mirah/base_input_object.rb, line 31 def to_graphql_hash self.class.inputs.each_with_object({}) do |input, obj| value = input[:serializer].serialize(send(input[:name])) obj[input[:graphql_name]] = value if value end end
valid?()
click to toggle source
# File lib/mirah/base_input_object.rb, line 17 def valid? self.class.inputs.all? do |input| !input[:required] || send(input[:name]) end end
validate!()
click to toggle source
# File lib/mirah/base_input_object.rb, line 23 def validate! self.class.inputs.all? do |input| if input[:required] && !send(input[:name]) raise Errors::MissingParameter.new(input[:name]), "#{input[:name]} is missing" end end end