Module: EPlat::Concerns::GraphQLable

Extended by:
ActiveSupport::Concern
Included in:
Base
Defined in:
lib/e_plat/resource/concerns/graph_q_lable.rb

Constant Summary collapse

FILTER_ARGS =
[:id, :after, :before, :last, :first, :sort_key, :reverse].freeze
QUERY_ARG_ARGS =
[:available_for_sale, :created_at, :product_type, :product_id, :tag, :tag_not, :title, :updated_at, :vendor].freeze

Instance Method Summary collapse

Instance Method Details

#graphql_mutation_string(graphql_proc_constant, action) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/e_plat/resource/concerns/graph_q_lable.rb', line 235

def graphql_mutation_string(graphql_proc_constant, action)
    mutation_proc      = eval graphql_proc_constant
    additional_graphql = self.class.before_graphql_callbacks[action.to_sym]&.call(self)
    input  = 
        if action == :delete 
            self.is_a?(EPlat::Product::Variant) ? graphql_input.new(id: formatted_id) : graphql_input.new({id: formatted_id})
        else
            graphql_input.new mapping.via_native_attributes_where_possible(self.class.remove_root_from as_json)
        end
        
    mutation_proc.call(input.to_graphql_args, additional_graphql:) 
end

#mutate_graphql(action, graphql_proc_constant, *args) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/e_plat/resource/concerns/graph_q_lable.rb', line 213

def mutate_graphql(action, graphql_proc_constant, *args)
    callback_name = (action == :delete) ? :destroy : action

    run_callbacks callback_name.to_sym do
        response = self.class.execute_graphql_request graphql_mutation_string(graphql_proc_constant, action)

        if !response.is_a?(Hash)
            raise EPlat::GraphqlError.new(response.first["message"])
        elsif response["errors"]
            raise EPlat::GraphqlError.new(response["errors"].first["message"])
        else
            response = remove_mutation_root_from(response, graphql_proc_constant)
            load self.class.resources_parsed_from(response), true, true
            
            @persisted = true
            changed_attributes.clear
        end
    end
rescue StandardError => e
    raise EPlat::GraphqlError.new(e.message)
end

#remove_mutation_root_from(data, graphql_proc_constant) ⇒ Object



248
249
250
251
# File 'lib/e_plat/resource/concerns/graph_q_lable.rb', line 248

def remove_mutation_root_from(data, graphql_proc_constant)
    mutation_root = graphql_proc_constant.split(".").last.camelcase(:lower)
    (mutation_root && data[mutation_root]) ? data[mutation_root] : data
end