Module: HQ::GraphQL::Resource::AutoMutation

Included in:
ClassMethods
Defined in:
lib/hq/graphql/resource/auto_mutation.rb

Instance Method Summary collapse

Instance Method Details

#build_copyObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/hq/graphql/resource/auto_mutation.rb', line 71

def build_copy
  scoped_self = self

  build_mutation(action: :copy, require_primary_key: true, nil_klass: true) do
    define_method(:resolve) do |**args|
      resource = scoped_self.find_record(args, context)

      if resource
        copy = resource.copy
        if copy.save
          {
            resource: copy,
            errors: {},
          }
        else
          {
            resource: copy,
            errors: errors_from_resource(copy)
          }
        end
      else
        {
          resource: nil,
          errors: { resource: "Unable to find #{self.class.graphql_name}" }
        }
      end
    end
  end
end

#build_createObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/hq/graphql/resource/auto_mutation.rb', line 11

def build_create
  scoped_self = self

  build_mutation(action: :create) do
    define_method(:resolve) do |**args|
      resource = scoped_self.new_record(context)
      resource.assign_attributes(args[:attributes].format_nested_attributes)
      if resource.save
        {
          resource: resource,
          errors: {},
        }
      else
        {
          resource: nil,
          errors: errors_from_resource(resource)
        }
      end
    end

    lazy_load do
      argument :attributes, ::HQ::GraphQL::Inputs[scoped_self.model_name], required: true
    end
  end
end

#build_destroyObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/hq/graphql/resource/auto_mutation.rb', line 101

def build_destroy
  scoped_self = self

  build_mutation(action: :destroy, require_primary_key: true) do
    define_method(:resolve) do |**attrs|
      resource = scoped_self.find_record(attrs, context)

      if resource
        if resource.destroy
          {
            resource: resource,
            errors: {},
          }
        else
          {
            resource: nil,
            errors: errors_from_resource(resource)
          }
        end
      else
        {
          resource: nil,
          errors: { resource: "Unable to find #{self.class.graphql_name}" }
        }
      end
    end
  end
end

#build_mutation(action:, require_primary_key: false, nil_klass: false, &block) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/hq/graphql/resource/auto_mutation.rb', line 130

def build_mutation(action:, require_primary_key: false, nil_klass: false, &block)
  gql_name = "#{graphql_name}#{action.to_s.titleize}"
  scoped_model_name = model_name
  Class.new(::HQ::GraphQL::Mutation) do
    graphql_name gql_name

    define_method(:ready?) do |*args|
      super(*args) && ::HQ::GraphQL.authorized?(action, scoped_model_name, context)
    end

    lazy_load do
      field :errors, ::HQ::GraphQL::Types::Object, null: false
      field :resource, ::HQ::GraphQL::Types[scoped_model_name, nil_klass], null: true
    end

    instance_eval(&block)

    if require_primary_key
      lazy_load do
        klass = scoped_model_name.constantize
        primary_key = klass.primary_key
        argument primary_key, ::GraphQL::Types::ID, required: true
      end
    end

    def errors_from_resource(resource)
      resource.errors.to_h.deep_transform_keys { |k| k.to_s.camelize(:lower) }
    end
  end
end

#build_updateObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/hq/graphql/resource/auto_mutation.rb', line 37

def build_update
  scoped_self = self

  build_mutation(action: :update, require_primary_key: true) do
    define_method(:resolve) do |**args|
      resource = scoped_self.find_record(args, context)

      if resource
        resource.assign_attributes(args[:attributes].format_nested_attributes)
        if resource.save
          {
            resource: resource,
            errors: {},
          }
        else
          {
            resource: nil,
            errors: errors_from_resource(resource)
          }
        end
      else
        {
          resource: nil,
          errors: { resource: "Unable to find #{self.class.graphql_name}" }
        }
      end
    end

    lazy_load do
      argument :attributes, ::HQ::GraphQL::Inputs[scoped_self.model_name], required: true
    end
  end
end