Sha256: c35cf5d28857eacf04d6297bfe75bd1fb1b96e02dae29fe583aaadb7da940aae

Contents?: true

Size: 1.75 KB

Versions: 4

Compression:

Stored size: 1.75 KB

Contents

module Ecoportal
  module API
    class GraphQL
      module Base
        class QueryConnection < Ecoportal::API::GraphQL::Base::Query
          accepted_params :searchConf, :after, :before, :first, :last
          include Enumerable

          attr_reader :path

          def initialize(*args, path: default_base_path, **kargs)
            @path = path
            super(*args, **kargs)
          end

          def query(path: default_base_path, **kargs, &block)
            path ||= default_base_path
            kargs = self.class.slice_params(kargs)
            ap = access_point(path)
            raise "Missuse. You have to implement this method in the child class"
          end

          def each(**kargs, &block)
            return to_enum(:each, **kargs) unless block
            cursor = nil; results = 0
            loop do
              kargs.update(after: cursor) if cursor
              connection = query(**kargs)
              #total    = connection.totalCount
              pageInfo = connection.pageInfo
              connection.nodes.each do |item|
                yield item
              end
              break unless cursor = pageInfo.endCursor
            end
          end

          def response_class
            return connection_class if respond_to?(:connection_class)
            @response_class ||= self.class.new_class(
              item_class,
              inherits: Ecoportal::API::GraphQL::Base::Connection,
              namespace: "Ecoportal::API::GraphQL::Connection"
            ) do |klass|
                klass.item_class = item_class
            end
          end

          def default_base_path
            ["currentOrganization"]
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ecoportal-api-graphql-0.1.3 lib/ecoportal/api/graphql/base/query_connection.rb
ecoportal-api-graphql-0.1.2 lib/ecoportal/api/graphql/base/query_connection.rb
ecoportal-api-graphql-0.1.1 lib/ecoportal/api/graphql/base/query_connection.rb
ecoportal-api-graphql-0.1.0 lib/ecoportal/api/graphql/base/query_connection.rb