Class: GraphqlRails::Model::Configuration

Inherits:
Object
  • Object
show all
Includes:
ChainableOptions, Configurable
Defined in:
lib/graphql_rails/model/configuration.rb

Overview

stores information about model specific config, like attributes and types

Constant Summary

Constants included from ChainableOptions

ChainableOptions::NOT_SET

Instance Method Summary collapse

Methods included from Configurable

#attributes, #name, #type_name

Methods included from ChainableOptions

included, #with

Constructor Details

#initialize(model_class) ⇒ Configuration

Returns a new instance of Configuration.



16
17
18
# File 'lib/graphql_rails/model/configuration.rb', line 16

def initialize(model_class)
  @model_class = model_class
end

Instance Method Details

#attribute(attribute_name, **attribute_options) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/graphql_rails/model/configuration.rb', line 36

def attribute(attribute_name, **attribute_options)
  key = attribute_name.to_s

  attributes[key] ||= build_attribute(attribute_name)

  attributes[key].tap do |new_attribute|
    new_attribute.with(**attribute_options)
    yield(new_attribute) if block_given?
  end
end

#connection_typeObject



71
72
73
# File 'lib/graphql_rails/model/configuration.rb', line 71

def connection_type
  @connection_type ||= BuildConnectionType.call(graphql_type)
end

#graphql_typeObject



61
62
63
64
65
66
67
68
69
# File 'lib/graphql_rails/model/configuration.rb', line 61

def graphql_type
  @graphql_type ||= FindOrBuildGraphqlType.call(
    name: name,
    description: description,
    attributes: attributes,
    type_name: type_name,
    implements: implements
  )
end

#implements(*interfaces) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/graphql_rails/model/configuration.rb', line 27

def implements(*interfaces)
  previous_implements = get_or_set_chainable_option(:implements) || []
  return previous_implements if interfaces.blank?

  full_implements = (previous_implements + interfaces).uniq

  get_or_set_chainable_option(:implements, full_implements) || []
end

#initialize_copy(other) ⇒ Object



20
21
22
23
24
25
# File 'lib/graphql_rails/model/configuration.rb', line 20

def initialize_copy(other)
  super
  @connection_type = nil
  @graphql_type = nil
  @input = other.instance_variable_get(:@input)&.transform_values(&:dup)
end

#input(input_name = nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/graphql_rails/model/configuration.rb', line 47

def input(input_name = nil)
  @input ||= {}
  name = input_name.to_s

  if block_given?
    @input[name] ||= Model::Input.new(model_class, input_name)
    yield(@input[name])
  end

  @input.fetch(name) do
    raise("GraphQL input with name #{input_name.inspect} is not defined for #{model_class.name}")
  end
end

#with_ensured_fields!Object



75
76
77
78
79
80
81
# File 'lib/graphql_rails/model/configuration.rb', line 75

def with_ensured_fields!
  return self if @graphql_type.blank?

  reset_graphql_type if attributes.any? && graphql_type.fields.length != attributes.length

  self
end