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.



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

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

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



36
37
38
# File 'lib/graphql/definition_helpers/defined_by_config.rb', line 36

def arguments
  @arguments
end

#fieldsObject (readonly)

Returns the value of attribute fields.



36
37
38
# File 'lib/graphql/definition_helpers/defined_by_config.rb', line 36

def fields
  @fields
end

#input_fieldsObject (readonly)

Returns the value of attribute input_fields.



36
37
38
# File 'lib/graphql/definition_helpers/defined_by_config.rb', line 36

def input_fields
  @input_fields
end

#valuesObject (readonly)

Returns the value of attribute values.



36
37
38
# File 'lib/graphql/definition_helpers/defined_by_config.rb', line 36

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



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

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



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

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



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

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



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

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



48
49
50
# File 'lib/graphql/definition_helpers/defined_by_config.rb', line 48

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

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

For EnumType



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

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