Class: GraphQL::DefinitionHelpers::DefinedByConfig::DefinitionConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/definition_helpers/defined_by_config.rb

Overview

This object is ‘instance_eval`’d when defining any object in the schema. Then, the applicable properties of this object are transfered to the given instance.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDefinitionConfig

Returns a new instance of DefinitionConfig.



43
44
45
46
47
48
49
50
51
# File 'lib/graphql/definition_helpers/defined_by_config.rb', line 43

def initialize
  @interfaces = []
  @possible_types = []
  @on = []
  @fields = {}
  @arguments = {}
  @values = []
  @input_fields = {}
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



41
42
43
# File 'lib/graphql/definition_helpers/defined_by_config.rb', line 41

def arguments
  @arguments
end

#fieldsObject (readonly)

Returns the value of attribute fields.



41
42
43
# File 'lib/graphql/definition_helpers/defined_by_config.rb', line 41

def fields
  @fields
end

#input_fieldsObject (readonly)

Returns the value of attribute input_fields.



41
42
43
# File 'lib/graphql/definition_helpers/defined_by_config.rb', line 41

def input_fields
  @input_fields
end

#valuesObject (readonly)

Returns the value of attribute values.



41
42
43
# File 'lib/graphql/definition_helpers/defined_by_config.rb', line 41

def values
  @values
end

Class Method Details

.attr_definable(*names) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/graphql/definition_helpers/defined_by_config.rb', line 16

def self.attr_definable(*names)
  attr_accessor(*names)
  names.each do |name|
    ivar_name = "@#{name}".to_sym
    define_method(name) do |new_value=nil|
      new_value && self.instance_variable_set(ivar_name, new_value)
      instance_variable_get(ivar_name)
    end
  end
end

Instance Method Details

#argument(name, type, description = nil, default_value: nil) ⇒ Object



89
90
91
92
93
94
95
96
# File 'lib/graphql/definition_helpers/defined_by_config.rb', line 89

def argument(name, type, description = nil, default_value: nil)
  argument = GraphQL::Argument.new
  argument.name = name.to_s
  argument.type = type
  argument.description = description
  argument.default_value = default_value
  @arguments[name.to_s] = argument
end

#field(name, type = nil, desc = nil, field: nil, property: nil, &block) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/graphql/definition_helpers/defined_by_config.rb', line 57

def field(name, type = nil, desc = nil, field: nil, property: nil, &block)
  if block_given?
    field = GraphQL::Field.define(&block)
  else
    field ||= GraphQL::Field.new
  end
  type && field.type = type
  desc && field.description = desc
  property && field.property = property
  field.name ||= name.to_s
  fields[name.to_s] = field
end

#input_field(name, type = nil, desc = nil, default_value: nil, &block) ⇒ Object

For InputObjectType



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/graphql/definition_helpers/defined_by_config.rb', line 76

def input_field(name, type = nil, desc = nil, default_value: nil, &block)
  argument = if block_given?
               GraphQL::Argument.define(&block)
             else
               GraphQL::Argument.new
             end
  argument.name = name
  type && argument.type = type
  desc && argument.description = desc
  default_value && argument.default_value = default_value
  input_fields[name.to_s] = argument
end

#to_instance(object, attributes) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/graphql/definition_helpers/defined_by_config.rb', line 98

def to_instance(object, attributes)
  attributes.each do |attr_name|
    configured_value = self.public_send(attr_name)
    object.public_send("#{attr_name}=", configured_value)
  end
  object
end

#typesObject



53
54
55
# File 'lib/graphql/definition_helpers/defined_by_config.rb', line 53

def types
  GraphQL::DefinitionHelpers::TypeDefiner.instance
end

#value(name, desc = nil, deprecation_reason: nil, value: name) ⇒ Object

For EnumType



71
72
73
# File 'lib/graphql/definition_helpers/defined_by_config.rb', line 71

def value(name, desc = nil, deprecation_reason: nil, value: name)
  values << GraphQL::EnumType::EnumValue.new(name: name, description: desc, deprecation_reason: deprecation_reason, value: value)
end