Class: GraphQL::Query
- Inherits:
-
Object
- Object
- GraphQL::Query
- Defined in:
- lib/graphql/query.rb
Overview
Executes queries from strings against SCHEMA.
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#query_string ⇒ Object
readonly
Returns the value of attribute query_string.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Instance Method Summary collapse
-
#as_result ⇒ Hash
Calling #as_result more than once won’t cause the query to be re-run.
-
#get_variable(identifier) ⇒ Object
returns a query variable named ‘identifier`, otherwise raises.
-
#initialize(query_string, context: nil) ⇒ Query
constructor
A new instance of Query.
Constructor Details
#initialize(query_string, context: nil) ⇒ Query
Returns a new instance of Query.
14 15 16 17 18 19 20 21 |
# File 'lib/graphql/query.rb', line 14 def initialize(query_string, context: nil) if !query_string.is_a?(String) || query_string.length == 0 raise "You must send a query string, not a #{query_string.class.name}" end @query_string = query_string @root = parse(query_string) @context = context end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
10 11 12 |
# File 'lib/graphql/query.rb', line 10 def context @context end |
#query_string ⇒ Object (readonly)
Returns the value of attribute query_string.
10 11 12 |
# File 'lib/graphql/query.rb', line 10 def query_string @query_string end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
10 11 12 |
# File 'lib/graphql/query.rb', line 10 def root @root end |
Instance Method Details
#as_result ⇒ Hash
Calling #as_result more than once won’t cause the query to be re-run
25 26 27 |
# File 'lib/graphql/query.rb', line 25 def as_result @as_result ||= execute! end |
#get_variable(identifier) ⇒ Object
returns a query variable named ‘identifier`, otherwise raises.
31 32 33 34 35 36 37 |
# File 'lib/graphql/query.rb', line 31 def get_variable(identifier) syntax_var = @root.variables.find { |v| v.identifier == identifier } if syntax_var.blank? raise "No variable found for #{identifier}, defined variables are #{@root.variables.map(&:identifier)}" end syntax_var end |