Class: GraphQL::Executor

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, schema) ⇒ Executor

Returns a new instance of Executor.



6
7
8
9
10
11
12
13
14
# File 'lib/graphql/executor.rb', line 6

def initialize(document, schema)
  @document = document
  @schema   = schema
  @context  = {
    document:   document,
    schema:     schema,
    errors:     []
  }
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



4
5
6
# File 'lib/graphql/executor.rb', line 4

def context
  @context
end

#documentObject (readonly)

Returns the value of attribute document.



4
5
6
# File 'lib/graphql/executor.rb', line 4

def document
  @document
end

#schemaObject (readonly)

Returns the value of attribute schema.



4
5
6
# File 'lib/graphql/executor.rb', line 4

def schema
  @schema
end

Instance Method Details

#execute(root = {}, params = {}, operation_name = nil) ⇒ Object

Raises:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/graphql/executor.rb', line 16

def execute(root = {}, params = {}, operation_name = nil)
  raise GraphQLError, "At least one operation should be defined." if document.operations.size == 0
  raise GraphQLError, "Operation name should be defined for more than one operation." if document.operations.size > 1 && operation_name.nil?

  operation_name  = document.operations.first.name if operation_name.nil?
  operation       = document.operation(operation_name)

  raise GraphQLError, "Operation named '#{operation_name}' not found in document." if operation.nil?

  context[:root]    = root
  context[:params]  = params

  return materialize(operation.evaluate(context)), context[:errors]
end

#materialize(data) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/graphql/executor.rb', line 31

def materialize(data)
  case data
  when Celluloid::Future
    materialize(data.value)
  when Hash
    data.reduce({}) do |memo, pair|
      memo[pair.first] = materialize(pair.last)
      memo
    end
  when Array
    data.each_with_index.reduce([]) do |memo, pair|
      memo[pair.last] = materialize(pair.first)
      memo
    end
  else
    data
  end
end