class GraphqlRails::Model::AddFieldsToGraphqlType

Adds graphql attributes as graphql fields to given graphql schema object.

Attributes

attributes[R]
klass[R]

Public Class Methods

new(klass:, attributes:) click to toggle source
# File lib/graphql_rails/model/add_fields_to_graphql_type.rb, line 12
def initialize(klass:, attributes:)
  @klass = klass
  @attributes = attributes
end

Public Instance Methods

call() click to toggle source
# File lib/graphql_rails/model/add_fields_to_graphql_type.rb, line 17
def call
  attributes.each { |attribute| define_graphql_field(attribute) }
end

Private Instance Methods

define_graphql_field(attribute) click to toggle source
# File lib/graphql_rails/model/add_fields_to_graphql_type.rb, line 25
def define_graphql_field(attribute) # rubocop:disable Metrics/MethodLength)
  klass.class_eval do
    field(*attribute.field_args, **attribute.field_options) do
      attribute.attributes.values.each do |arg_attribute|
        argument(*arg_attribute.input_argument_args, **arg_attribute.input_argument_options)
      end
    end

    define_method(attribute.field_name) do |**kwargs|
      CallGraphqlModelMethod.call(
        model: object,
        attribute_config: attribute,
        method_keyword_arguments: kwargs,
        graphql_context: context
      )
    end
  end
end