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 graphql_query(**kargs, &block) query_params = self.class.slice_params(kargs) client.query(query_params, &block) rescue Faraday::ParsingError => e puts "Internal Error with these params:" pp kargs raise end def each(connection_block: nil, **kargs, &block) return to_enum(:each, **kargs, connection_block: connection_block) unless block cursor = nil; results = 0 loop do kargs.update(after: cursor) if cursor connection = query(**kargs, &connection_block) #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