Class: GraphQL::ActiveRecord
- Inherits:
-
Field
- Object
- Field
- GraphQL::ActiveRecord
- Defined in:
- lib/graphql/active_record.rb
Overview
This class allows GraphQL to translate ActiveRecord objects into GraphQL objects easily and does an auto-include to make it somewhat more efficient (hopefully)
Instance Method Summary collapse
-
#initialize(model:, type:, use_uuid: false) ⇒ ActiveRecord
constructor
A new instance of ActiveRecord.
-
#resolve(object, arguments, ctx) ⇒ Object
override of GraphQL::Field.resolve basically just provides the final object (in this case an AR model) can check RDoc for graphql-ruby gem.
Constructor Details
#initialize(model:, type:, use_uuid: false) ⇒ ActiveRecord
Returns a new instance of ActiveRecord.
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/graphql/active_record.rb', line 11 def initialize(model:, type:, use_uuid: false) @model = model @use_uuid = use_uuid self.type = type self.description = "Find a #{model.name} by ID" self.arguments = { id: GraphQL::Argument.define do type !GraphQL::ID_TYPE description "Id for record" end } end |
Instance Method Details
#resolve(object, arguments, ctx) ⇒ Object
override of GraphQL::Field.resolve basically just provides the final object (in this case an AR model) can check RDoc for graphql-ruby gem
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/graphql/active_record.rb', line 35 def resolve(object, arguments, ctx) includes = map_includes(@model, ctx.ast_node.selections, ctx) model_with_includes = @model.includes(*includes) if @use_uuid model_with_includes.find_by_uuid(arguments['id']) else model_with_includes.find(arguments['id']) end rescue ActiveRecord::ConfigurationError => e @model.find(arguments['id']) end |