Module: HQ::GraphQL::Resource::ClassMethods

Includes:
AutoMutation
Defined in:
lib/hq/graphql/resource.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AutoMutation

#build_copy, #build_create, #build_destroy, #build_mutation, #build_update

Instance Attribute Details

#graphql_nameObject



45
46
47
# File 'lib/hq/graphql/resource.rb', line 45

def graphql_name
  @graphql_name || model_name.demodulize
end

#model_nameObject



49
50
51
# File 'lib/hq/graphql/resource.rb', line 49

def model_name
  @model_name || ::HQ::GraphQL.extract_class(self)
end

Instance Method Details

#const_missing(constant_name) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/hq/graphql/resource.rb', line 88

def const_missing(constant_name)
  constant_name = constant_name.to_sym
  case constant_name
  when :Query
    query_object
  when :NilQuery
    nil_query_object
  when :Input
    input_klass
  when :FilterInput
    filter_input
  when :FilterFields
    filter_fields_enum
  else
    super
  end
end

#filter_fields_enumObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/hq/graphql/resource.rb', line 122

def filter_fields_enum
  @filter_fields_enum ||= begin
    scoped_self = self

    enum_class = Class.new(::GraphQL::Schema::Enum) do
      graphql_name "#{scoped_self.graphql_name}FilterFields"

      lazy_load do
        scoped_self.model_klass.columns.sort_by(&:name).each do |column|
          next unless HQ::GraphQL::Filters.supported?(column)
          value column.name.camelize(:lower), value: column
        end
      end
    end

    const_set(:FilterFields, enum_class)
  end
end

#filter_inputObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/hq/graphql/resource.rb', line 106

def filter_input
  @filter_input ||= begin
    scoped_self = self

    input_class = Class.new(::GraphQL::Schema::InputObject) do
      graphql_name "#{scoped_self.graphql_name}FilterInput"

      argument :field, scoped_self.filter_fields_enum, required: true
      argument :operation, Enum::FilterOperation, required: true
      argument :value, String, required: true
    end

    const_set(:FilterInput, input_class)
  end
end

#find_record(attrs, context) ⇒ Object



35
36
37
38
39
# File 'lib/hq/graphql/resource.rb', line 35

def find_record(attrs, context)
  primary_key = model_klass.primary_key.to_sym
  primary_key_value = attrs[primary_key]
  scope(context).find_by(primary_key => primary_key_value)
end

#input_klassObject



61
62
63
# File 'lib/hq/graphql/resource.rb', line 61

def input_klass
  @input_klass ||= build_input_object
end

#model_klassObject



53
54
55
# File 'lib/hq/graphql/resource.rb', line 53

def model_klass
  @model_klass ||= model_name&.safe_constantize
end

#mutation_klassesObject



57
58
59
# File 'lib/hq/graphql/resource.rb', line 57

def mutation_klasses
  @mutation_klasses ||= {}.with_indifferent_access
end

#new_record(context) ⇒ Object



41
42
43
# File 'lib/hq/graphql/resource.rb', line 41

def new_record(context)
  scope(context).new
end

#nil_query_objectObject



65
66
67
# File 'lib/hq/graphql/resource.rb', line 65

def nil_query_object
  @nil_query_object ||= const_set(:NilQuery, build_graphql_object(name: "#{graphql_name}Copy", auto_nil: false))
end

#query_objectObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/hq/graphql/resource.rb', line 69

def query_object
  @query_object ||= begin
    qo =
      if @query_object_options
        options, block = @query_object_options
        @query_object_options = nil
        build_graphql_object(**options, &block)
      else
        build_graphql_object
      end
    remove_const(:Query) if const_defined?(:Query, false)
    const_set(:Query, qo)
  end
end

#scope(context) ⇒ Object



29
30
31
32
33
# File 'lib/hq/graphql/resource.rb', line 29

def scope(context)
  scope = model_klass
  scope = ::HQ::GraphQL.default_scope(scope, context)
  @default_scope&.call(scope, context) || scope
end

#sort_fields_enumObject



84
85
86
# File 'lib/hq/graphql/resource.rb', line 84

def sort_fields_enum
  @sort_fields_enum || ::HQ::GraphQL::Enum::SortBy
end