Module: GraphQL::Define::InstanceDefinable

Included in:
Argument, BaseType, GraphQL::Directive, Field
Defined in:
lib/graphql/define/instance_definable.rb

Overview

This module provides the ‘.define { … }` API for BaseType, Field, Argument and GraphQL::Directive.

The goals are:

  • Minimal overhead in consuming classes

  • Independence between consuming classes

  • Extendable by third-party libraries without monkey-patching or other nastiness

Examples:

Make a class definable

class Car
  attr_accessor :make, :model, :all_wheel_drive

  accepts_definitions(
    # These attrs will be defined with plain setters, `{attr}=`
    :make, :model,
    # This attr has a custom definition which applies the config to the target
    doors: -> (car, doors_count) { doors_count.times { car.doors << Door.new } }
  )
end

# Create an instance with `.define`:
subaru_baja = Car.define do
  make "Subaru"
  model "Baja"
  doors 4
end

# The custom proc was applied:
subaru_baja.doors #=> [<Door>, <Door>, <Door>, <Door>]

Extending the definition of a class

# Add some definitions:
Car.accepts_definitions(:all_wheel_drive)

# Use it in a definition
subaru_baja = Car.define do
  # ...
  all_wheel_drive true
end

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



43
44
45
# File 'lib/graphql/define/instance_definable.rb', line 43

def self.included(base)
  base.extend(ClassMethods)
end