Class: GraphQL::ActiveRecordExtensions::Field
- Inherits:
-
Field
- Object
- Field
- GraphQL::ActiveRecordExtensions::Field
- Defined in:
- lib/graphql/active_record_extensions/field.rb
Instance Method Summary collapse
-
#initialize(model:, type:, use_uuid: false) ⇒ Field
constructor
A new instance of Field.
-
#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) ⇒ Field
Returns a new instance of Field.
8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/graphql/active_record_extensions/field.rb', line 8 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
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/graphql/active_record_extensions/field.rb', line 32 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 |