Class: DaggerRuby::QueryBuilder
- Inherits:
-
Object
- Object
- DaggerRuby::QueryBuilder
- Defined in:
- lib/dagger_ruby/query_builder.rb
Instance Attribute Summary collapse
-
#operation_chain ⇒ Object
readonly
Returns the value of attribute operation_chain.
-
#root_field ⇒ Object
readonly
Returns the value of attribute root_field.
-
#variables ⇒ Object
readonly
Returns the value of attribute variables.
Class Method Summary collapse
Instance Method Summary collapse
- #build_query_with_selection(field, args = {}) ⇒ Object
- #chain_operation(field, args = {}) ⇒ Object
- #field_selection(field, args = {}) ⇒ Object
-
#initialize(root_field = nil) ⇒ QueryBuilder
constructor
A new instance of QueryBuilder.
- #variable(name, type) ⇒ Object
Constructor Details
#initialize(root_field = nil) ⇒ QueryBuilder
Returns a new instance of QueryBuilder.
14 15 16 17 18 |
# File 'lib/dagger_ruby/query_builder.rb', line 14 def initialize(root_field = nil) @root_field = root_field @operation_chain = [] @variables = {} end |
Instance Attribute Details
#operation_chain ⇒ Object (readonly)
Returns the value of attribute operation_chain.
8 9 10 |
# File 'lib/dagger_ruby/query_builder.rb', line 8 def operation_chain @operation_chain end |
#root_field ⇒ Object (readonly)
Returns the value of attribute root_field.
8 9 10 |
# File 'lib/dagger_ruby/query_builder.rb', line 8 def root_field @root_field end |
#variables ⇒ Object (readonly)
Returns the value of attribute variables.
8 9 10 |
# File 'lib/dagger_ruby/query_builder.rb', line 8 def variables @variables end |
Class Method Details
.enum_value(value) ⇒ Object
10 11 12 |
# File 'lib/dagger_ruby/query_builder.rb', line 10 def self.enum_value(value) value.is_a?(Hash) ? value : value.to_sym end |
Instance Method Details
#build_query_with_selection(field, args = {}) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/dagger_ruby/query_builder.rb', line 34 def build_query_with_selection(field, args = {}) selection = field_selection(field, args) query_parts = [] if @variables.any? vars = @variables.map { |name, type| "$#{name}: #{type}" }.join(", ") query_parts << "query(#{vars})" else query_parts << "query" end if @root_field if @operation_chain.empty? query_parts << "{ #{@root_field} { #{selection} } }" else operations_str = build_operations_chain_with_selection(selection) query_parts << "{ #{@root_field} { #{operations_str} } }" end elsif @operation_chain.any? operations_str = build_operations_chain_with_selection(selection) query_parts << "{ #{operations_str} }" else query_parts << "{ #{selection} }" end query_parts.join(" ") end |
#chain_operation(field, args = {}) ⇒ Object
20 21 22 23 24 25 |
# File 'lib/dagger_ruby/query_builder.rb', line 20 def chain_operation(field, args = {}) new_query = QueryBuilder.new(@root_field) new_query.instance_variable_set(:@operation_chain, @operation_chain + [{ field: field, args: args }]) new_query.instance_variable_set(:@variables, @variables.dup) new_query end |
#field_selection(field, args = {}) ⇒ Object
62 63 64 |
# File 'lib/dagger_ruby/query_builder.rb', line 62 def field_selection(field, args = {}) "#{field}#{format_arguments(args)}" end |
#variable(name, type) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/dagger_ruby/query_builder.rb', line 27 def variable(name, type) new_query = QueryBuilder.new(@root_field) new_query.instance_variable_set(:@operation_chain, @operation_chain.dup) new_query.instance_variable_set(:@variables, @variables.merge(name => type)) new_query end |