Sha256: ebaac8b39aa5588e4b15dda12b25275211529e415c27eaff596fbcd956acb884

Contents?: true

Size: 1.89 KB

Versions: 18

Compression:

Stored size: 1.89 KB

Contents

module GraphQL
  module Models
    class BackedByModel
      attr_accessor :graph_type, :model_type, :object_to_model

      DEFAULT_OBJECT_TO_MODEL = -> (obj) { obj }

      def initialize(graph_type, model_type)
        @graph_type = graph_type
        @model_type = model_type
        @object_to_model = DEFAULT_OBJECT_TO_MODEL
      end

      def types
        GraphQL::Define::TypeDefiner.instance
      end

      def object_to_model(value = nil)
        @object_to_model = value if value
        @object_to_model
      end

      def attr(name, **options)
        DefinitionHelpers.define_attribute(graph_type, model_type, model_type, [], name, object_to_model, options)
      end

      def proxy_to(association, &block)
        DefinitionHelpers.define_proxy(graph_type, model_type, model_type, [], association, object_to_model, &block)
      end

      def has_one(association, **options)
        DefinitionHelpers.define_has_one(graph_type, model_type, model_type, [], association, object_to_model, options)
      end

      def has_many_connection(association, **options)
        DefinitionHelpers.define_has_many_connection(graph_type, model_type, model_type, [], association, object_to_model, options)
      end

      def has_many_array(association, **options)
        DefinitionHelpers.define_has_many_array(graph_type, model_type, model_type, [], association, object_to_model, options)
      end

      def field(*args, &block)
        defined_field = GraphQL::Define::AssignObjectField.call(graph_type, *args, &block)

        DefinitionHelpers.register_field_metadata(graph_type, defined_field.name, {
          macro: :field,
          macro_type: :custom,
          path: [],
          base_model_type: @model_type.to_s.classify.constantize,
          model_type: @model_type.to_s.classify.constantize,
          object_to_base_model: object_to_model
        })

        defined_field
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
graphql-activerecord-0.6.5 lib/graphql/models/backed_by_model.rb
graphql-activerecord-0.6.4 lib/graphql/models/backed_by_model.rb
graphql-activerecord-0.6.3 lib/graphql/models/backed_by_model.rb
graphql-activerecord-0.6.2 lib/graphql/models/backed_by_model.rb
graphql-activerecord-0.6.1 lib/graphql/models/backed_by_model.rb
graphql-activerecord-0.6.0 lib/graphql/models/backed_by_model.rb
graphql-activerecord-0.5.6 lib/graphql/models/backed_by_model.rb
graphql-activerecord-0.5.5 lib/graphql/models/backed_by_model.rb
graphql-activerecord-0.5.4 lib/graphql/models/backed_by_model.rb
graphql-activerecord-0.5.3 lib/graphql/models/backed_by_model.rb
graphql-activerecord-0.5.2 lib/graphql/models/backed_by_model.rb
graphql-activerecord-0.5.1 lib/graphql/models/backed_by_model.rb
graphql-activerecord-0.5.0 lib/graphql/models/backed_by_model.rb
graphql-activerecord-0.4.3 lib/graphql/models/backed_by_model.rb
graphql-activerecord-0.4.2 lib/graphql/models/backed_by_model.rb
graphql-activerecord-0.4.1 lib/graphql/models/backed_by_model.rb
graphql-activerecord-0.4.0 lib/graphql/models/backed_by_model.rb
graphql-activerecord-0.3.1 lib/graphql/models/backed_by_model.rb