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.



40
41
42
43
44
45
46
47
48
# File 'lib/graphql/definition_helpers/defined_by_config.rb', line 40

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

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



38
39
40
# File 'lib/graphql/definition_helpers/defined_by_config.rb', line 38

def arguments
  @arguments
end

#fieldsObject (readonly)

Returns the value of attribute fields.



38
39
40
# File 'lib/graphql/definition_helpers/defined_by_config.rb', line 38

def fields
  @fields
end

#input_fieldsObject (readonly)

Returns the value of attribute input_fields.



38
39
40
# File 'lib/graphql/definition_helpers/defined_by_config.rb', line 38

def input_fields
  @input_fields
end

#valuesObject (readonly)

Returns the value of attribute values.



38
39
40
# File 'lib/graphql/definition_helpers/defined_by_config.rb', line 38

def values
  @values
end

Class Method Details

.attr_definable(*names) ⇒ Object



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

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



86
87
88
89
90
91
92
93
# File 'lib/graphql/definition_helpers/defined_by_config.rb', line 86

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



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/graphql/definition_helpers/defined_by_config.rb', line 54

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.resolve = -> (t,a,c) { t.public_send(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



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/graphql/definition_helpers/defined_by_config.rb', line 73

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.desc = desc
  default_value && argument.default_value = default_value
  input_fields[name.to_s] = argument
end

#to_instance(object, attributes) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/graphql/definition_helpers/defined_by_config.rb', line 95

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



50
51
52
# File 'lib/graphql/definition_helpers/defined_by_config.rb', line 50

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

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

For EnumType



68
69
70
# File 'lib/graphql/definition_helpers/defined_by_config.rb', line 68

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