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.
Instance Method Summary collapse
- #build_query_with_selection(field) ⇒ Object
- #chain_operation(field, args = {}) ⇒ Object
-
#initialize(root_field = nil) ⇒ QueryBuilder
constructor
A new instance of QueryBuilder.
- #load_from_id(id) ⇒ Object
- #variable(name, type) ⇒ Object
Constructor Details
#initialize(root_field = nil) ⇒ QueryBuilder
Returns a new instance of QueryBuilder.
10 11 12 13 14 |
# File 'lib/dagger_ruby/query_builder.rb', line 10 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 |
Instance Method Details
#build_query_with_selection(field) ⇒ 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 |
# File 'lib/dagger_ruby/query_builder.rb', line 34 def build_query_with_selection(field) 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} { #{field} } }" else operations_str = build_operations_chain_with_selection(field) query_parts << "{ #{@root_field} { #{operations_str} } }" end elsif @operation_chain.any? operations_str = build_operations_chain_with_selection(field) query_parts << "{ #{operations_str} }" else query_parts << "{ #{field} }" end query_parts.join(" ") end |
#chain_operation(field, args = {}) ⇒ Object
16 17 18 19 20 21 |
# File 'lib/dagger_ruby/query_builder.rb', line 16 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 |
#load_from_id(id) ⇒ Object
23 24 25 |
# File 'lib/dagger_ruby/query_builder.rb', line 23 def load_from_id(id) chain_operation("loadFromId", { "id" => id }) 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 |