Resolve models and types
This commit is contained in:
parent
e06051bc96
commit
a7fc4df09b
2 changed files with 39 additions and 0 deletions
|
@ -1,3 +1,19 @@
|
|||
class ApplicationRecord < ActiveRecord::Base
|
||||
self.abstract_class = true
|
||||
|
||||
def self.record_from_typed_id(id)
|
||||
class_name, record_id = GraphQL::Schema::UniqueWithinType.decode(id)
|
||||
|
||||
if defined?(class_name)
|
||||
Object.const_get(class_name).find(record_id)
|
||||
else
|
||||
raise ActiveRecord::RecordNotFound, "Unexpected object: #{class_name}"
|
||||
end
|
||||
rescue => e
|
||||
raise ActiveRecord::RecordNotFound, e.message
|
||||
end
|
||||
|
||||
def to_typed_id
|
||||
GraphQL::Schema::UniqueWithinType.encode(self.class.name, id)
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue