Sha256: d65e438948180109f08adf64765cb8cb7d4779c94f671ca814d68fb603f07e48

Contents?: true

Size: 1.29 KB

Versions: 3

Compression:

Stored size: 1.29 KB

Contents

module Ecoportal
  module API
    module Common
      module GraphQL
        module QueryIntegration
          include Ecoportal::API::Common::GraphQL::ClassHelpers::ClassMethods

          attr_accessor :client
          attr_reader :path

          # @!macro [attach] query
          #   @method $1
          #   The $1 query integration
          #   @return [Collection::$1, $1]
          #   @return [Connection::$1, $1]
          def query(method, query_klass:)
            method = method.to_sym
            class_method = "#{method}_query_class".to_sym

            define_method(method) do |**kargs, &block|
              if kargs.empty? && !block_given?
                method(class_method).call
              else
                method(class_method).call.query(**kargs, &block)
              end
            end

            define_method(class_method) do
              resolve_class(query_klass).tap do |klass|
                unless klass <= Ecoportal::API::GraphQL::Base::Query
                  raise "Expected query_klass to be of type Ecoportal::API::GraphQL::Base::Query. Given: #{klass}"
                end
              end.new(client, base_path: path)
            end
            private class_method
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ecoportal-api-graphql-0.1.11 lib/ecoportal/api/common/graphql/query_integration.rb
ecoportal-api-graphql-0.1.10 lib/ecoportal/api/common/graphql/query_integration.rb
ecoportal-api-graphql-0.1.9 lib/ecoportal/api/common/graphql/query_integration.rb