Class: GraphQL::Executor

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

Defined Under Namespace

Classes: FutureCompleter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, schema) ⇒ Executor

Returns a new instance of Executor.



42
43
44
45
46
47
48
49
# File 'lib/graphql/executor.rb', line 42

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

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



40
41
42
# File 'lib/graphql/executor.rb', line 40

def context
  @context
end

#documentObject (readonly)

Returns the value of attribute document.



40
41
42
# File 'lib/graphql/executor.rb', line 40

def document
  @document
end

#schemaObject (readonly)

Returns the value of attribute schema.



40
41
42
# File 'lib/graphql/executor.rb', line 40

def schema
  @schema
end

Instance Method Details

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

Raises:



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/graphql/executor.rb', line 51

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

  materialize(operation.evaluate(context))
end

#materialize(data) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/graphql/executor.rb', line 66

def materialize(data)
  case data
  when Hash
    data.each do |key, value|
      data[key] = value.value if value.is_a?(Celluloid::Future)
      materialize(data[key])
    end
  when Array
    data.each_with_index do |value, i|
      data[i] = value.value if value.is_a?(Celluloid::Future)
      materialize(data[i])
    end
  end
  data
end