Class: ZeroRuby::InputObject

Inherits:
Dry::Struct
  • Object
show all
Includes:
TypeNames
Defined in:
lib/zero_ruby/input_object.rb

Overview

Base class for input objects (nested argument types). Uses Dry::Struct for type coercion and validation.

Includes ZeroRuby::TypeNames for convenient type access via the Types module (e.g., Types::String, Types::ID, Types::Boolean).

Examples:

class Types::PostInput < ZeroRuby::InputObject
  argument :id, Types::ID
  argument :title, Types::String.constrained(min_size: 1, max_size: 200)
  argument :body, Types::String.optional
  argument :published, Types::Boolean.default(false)
end

class PostCreate < ZeroRuby::Mutation
  argument :post_input, Types::PostInput
  argument :notify, Types::Boolean.default(false)

  def execute
    # args[:post_input].title, args[:post_input].body, etc.
    # Or use **args[:post_input] to splat into method calls
  end
end

Constant Summary

Constants included from TypeNames

TypeNames::Types

Class Method Summary collapse

Class Method Details

.argument(name, type, description: nil, **_options) ⇒ Object

Alias attribute to argument for DSL compatibility

Parameters:

  • name (Symbol)

    The argument name

  • type (Dry::Types::Type)

    The type (from ZeroRuby::Types)

  • description (String, nil) (defaults to: nil)

    Optional description (stored for documentation, not passed to Dry::Struct)



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/zero_ruby/input_object.rb', line 46

def argument(name, type, description: nil, **_options)
  # Store description for documentation/TypeScript generation
  argument_descriptions[name.to_sym] = description if description

  # Use attribute? for optional types (allows key to be omitted)
  # Use attribute for required types (key must be present)
  if optional_type?(type)
    attribute?(name, type)
  else
    attribute(name, type)
  end
end

.argument_descriptionsObject

Get stored argument descriptions



60
61
62
# File 'lib/zero_ruby/input_object.rb', line 60

def argument_descriptions
  @argument_descriptions ||= {}
end

.arguments_metadataHash<Symbol, Hash>

Returns argument metadata for TypeScript generation

Returns:

  • (Hash<Symbol, Hash>)

    Map of argument name to metadata



72
73
74
75
76
77
78
79
80
# File 'lib/zero_ruby/input_object.rb', line 72

def 
  schema.keys.each_with_object({}) do |key, hash|
    hash[key.name] = {
      type: key.type,
      required: key.required?,
      name: key.name
    }
  end
end

.optional_type?(type) ⇒ Boolean

Check if a type is optional (can accept nil or has a default)

Returns:

  • (Boolean)


65
66
67
68
# File 'lib/zero_ruby/input_object.rb', line 65

def optional_type?(type)
  return false unless type.respond_to?(:optional?)
  type.optional? || (type.respond_to?(:default?) && type.default?)
end