Class: GraphQL::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query_string, namespace: Object) ⇒ Query

Returns a new instance of Query.



3
4
5
6
7
8
9
10
# File 'lib/graphql/query.rb', line 3

def initialize(query_string, namespace: Object)
  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)
  @namespace = namespace
end

Instance Attribute Details

#namespaceObject (readonly)

Returns the value of attribute namespace.



2
3
4
# File 'lib/graphql/query.rb', line 2

def namespace
  @namespace
end

#query_stringObject (readonly)

Returns the value of attribute query_string.



2
3
4
# File 'lib/graphql/query.rb', line 2

def query_string
  @query_string
end

#rootObject (readonly)

Returns the value of attribute root.



2
3
4
# File 'lib/graphql/query.rb', line 2

def root
  @root
end

Instance Method Details

#get_node(identifier) ⇒ Object



22
23
24
25
# File 'lib/graphql/query.rb', line 22

def get_node(identifier)
  name = "#{identifier}_node"
  namespace.const_get(name.camelize)
end

#make_call(context, name, *arguments) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/graphql/query.rb', line 27

def make_call(context, name, *arguments)
  if context.nil?
    context = get_node(name)
    name = "call"
  end
  context.send(name, *arguments)
end

#to_jsonObject



12
13
14
15
16
17
18
19
20
# File 'lib/graphql/query.rb', line 12

def to_json
  root_node = make_call(nil, root.identifier, root.argument)
  raise "Couldn't find root for #{root.identifier}(#{root.argument})" if root.nil?

  root_node.fields = root.fields
  {
    root_node.cursor => root_node.to_json
  }
end