# File lib/active_graphql/model.rb, line 127 def primary_key self.class.active_graphql.primary_key end
module ActiveGraphql::Model
Allows to have ActiveRecord-like models which comunicates with graphql endpoint instead of DB: class RemoteUser
include ActiveGraphql::Model graphql_url('http://localhost:3001/graphql') graphql_attributes :id, :full_name
end
Now you can do: RemoteUser.where(created_at { from: '2000-01-01', to: '2010-01-01' }) RemoteUser.all.for_each { |user| … } RemoteUser.where(…).count
Model
expects that graphql has GraphqlRails CRUD actions with default naming (createRemoteUser, remoteUsers, etc.)
Public Class Methods
new(attributes)
click to toggle source
# File lib/active_graphql/model.rb, line 36 def initialize(attributes) @attributes = attributes.deep_transform_keys { |it| it.to_s.underscore.to_sym } end
Public Instance Methods
active_graphql() { |active_graphql| ... }
click to toggle source
# File lib/active_graphql/model.rb, line 143 def active_graphql @active_graphql ||= ActiveGraphql::Model::Configuration.new if block_given? yield(@active_graphql) @active_graphql.attributes.each do |attribute| define_method(attribute.name) do read_graphql_attribute(attribute) end end end @active_graphql end
all()
click to toggle source
# File lib/active_graphql/model.rb, line 176 def all @all ||= ::ActiveGraphql::Model::RelationProxy.new(self) end
attributes=(new_attributes)
click to toggle source
# File lib/active_graphql/model.rb, line 61 def attributes=(new_attributes) formatted_new_attributes = new_attributes.deep_transform_keys { |it| it.to_s.underscore.to_sym } @attributes = attributes.merge(formatted_new_attributes) end
create(params)
click to toggle source
# File lib/active_graphql/model.rb, line 156 def create(params) action_name = "create_#{active_graphql.resource_name}" response = exec_graphql do |api| api.mutation(action_name).input(params) end new(response.result.to_h).tap do |record| record.graphql_errors = response.detailed_errors if !response.success? || !record.valid? end end
create!(params)
click to toggle source
# File lib/active_graphql/model.rb, line 167 def create!(params) record = create(params) return record if record.valid? error_message = (record.errors['graphql'] || record.errors.full_messages).first raise Errors::RecordNotValidError, error_message end
destroy()
click to toggle source
# File lib/active_graphql/model.rb, line 66 def destroy action_name = "destroy_#{self.class.active_graphql.resource_name}" response = exec_graphql { |api| api.mutation(action_name).input(primary_key => primary_key_value) } response.success? end
exec_graphql(*args, &block)
click to toggle source
# File lib/active_graphql/model.rb, line 95 def exec_graphql(*args, &block) self.class.exec_graphql(*args, &block) end
graphql_errors()
click to toggle source
# File lib/active_graphql/model.rb, line 110 def graphql_errors @graphql_errors ||= [] end
inherited(sublass)
click to toggle source
# File lib/active_graphql/model.rb, line 139 def inherited(sublass) sublass.instance_variable_set(:@active_graphql, active_graphql.dup) end
mutate(action_name, params = {})
click to toggle source
# File lib/active_graphql/model.rb, line 40 def mutate(action_name, params = {}) all_params = { primary_key => primary_key_value }.merge(params) response = exec_graphql { |api| api.mutation(action_name.to_s).input(all_params) } self.attributes = response.result.to_h self.graphql_errors = response.detailed_errors valid? end
primary_key()
click to toggle source
primary_key_value()
click to toggle source
# File lib/active_graphql/model.rb, line 131 def primary_key_value send(primary_key) end
read_attribute_for_validation(key)
click to toggle source
Calls superclass method
# File lib/active_graphql/model.rb, line 123 def read_attribute_for_validation(key) key == 'graphql' ? key : super end
read_graphql_attribute(attribute)
click to toggle source
# File lib/active_graphql/model.rb, line 99 def read_graphql_attribute(attribute) value = attributes[attribute.name] if attribute.decorate_with send(attribute.decorate_with, value) else value end end
reload()
click to toggle source
# File lib/active_graphql/model.rb, line 72 def reload self.attributes = self.class.find(primary_key_value).attributes self end
save()
click to toggle source
# File lib/active_graphql/model.rb, line 77 def save if primary_key_value.present? update(attributes.except(primary_key)) else self.class.create(attributes) end end
save!()
click to toggle source
# File lib/active_graphql/model.rb, line 85 def save! if primary_key_value.present? update!(attributes.reject { |attr, _| attr == primary_key }) else self.class.create!(attributes) end end
update(params)
click to toggle source
# File lib/active_graphql/model.rb, line 48 def update(params) action_name = "update_#{self.class.active_graphql.resource_name}" mutate(action_name, params) end
update!(params)
click to toggle source
# File lib/active_graphql/model.rb, line 53 def update!(params) success = update(params) return true if success error_message = (errors['graphql'] || errors.full_messages).first raise Errors::RecordNotValidError, error_message end
validate_graphql_errors()
click to toggle source
# File lib/active_graphql/model.rb, line 114 def validate_graphql_errors graphql_errors.each do |error| error_key = error[:field] || 'graphql' error_message = error[:short_message] || error[:message] errors.add(error_key, error_message) end end