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



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

def graphql_name
  @graphql_name || model_name.demodulize
end

#model_nameObject



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

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

Instance Method Details

#const_missing(constant_name) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/hq/graphql/resource.rb', line 85

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
  else
    super
  end
end

#find_record(attrs, context) ⇒ Object



32
33
34
35
36
# File 'lib/hq/graphql/resource.rb', line 32

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



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

def input_klass
  @input_klass ||= build_input_object
end

#model_klassObject



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

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

#mutation_klassesObject



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

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

#new_record(context) ⇒ Object



38
39
40
# File 'lib/hq/graphql/resource.rb', line 38

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

#nil_query_objectObject



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

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

#query_objectObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/hq/graphql/resource.rb', line 66

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



26
27
28
29
30
# File 'lib/hq/graphql/resource.rb', line 26

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

#sort_fields_enumObject



81
82
83
# File 'lib/hq/graphql/resource.rb', line 81

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