lib/ecoportal/api/graphql/logic/base_query.rb in ecoportal-api-graphql-0.3.8 vs lib/ecoportal/api/graphql/logic/base_query.rb in ecoportal-api-graphql-0.3.9
- old
+ new
@@ -2,20 +2,31 @@
module API
class GraphQL
module Logic
class BaseQuery
include Ecoportal::API::Common::GraphQL::ClassHelpers
- inheritable_attrs :accepted_params
+ inheritable_attrs :accepted_params, :param_defaults
class << self
- def accepted_params(*keys)
+ def param_defaults
+ @param_defaults ||= {}
+ end
+
+ def accepted_params(*keys, default: :unused)
@accepted_params ||= []
return @accepted_params if keys.empty?
+ keys.map(&:to_sym).uniq.each do |key|
+ @accepted_params |= [key]
+ param_defaults[key] = default unless default == :unused
+ end
+
@accepted_params.push(*keys).tap {|ks| ks.uniq!}
+ @accepted_params
end
def clear_accepted_params
+ @param_defaults = {}
@accepted_params = []
end
def slice_params(kargs)
kargs.slice(*accepted_params)
@@ -66,10 +77,11 @@
def basic_block
raise "This method should be implemented in the child class #{self.class}"
end
def graphql_query(path: self.path, **kargs, &block)
- query_params = self.class.slice_params(kargs)
+ default_params = self.class.param_defaults.dup
+ query_params = self.class.slice_params(default_params.merge(kargs))
request(*path) do
client.query(query_params, &block)
end
rescue Faraday::ParsingError, Graphlient::Errors::GraphQLError => e
puts "Internal Error with these params:"