Class: Tina4::GraphQLExecutor
- Inherits:
-
Object
- Object
- Tina4::GraphQLExecutor
- Defined in:
- lib/tina4/graphql.rb
Overview
─── Executor ─────────────────────────────────────────────────────────
Instance Method Summary collapse
- #execute(document, variables: {}, context: {}, operation_name: nil) ⇒ Object
-
#initialize(schema) ⇒ GraphQLExecutor
constructor
A new instance of GraphQLExecutor.
Constructor Details
#initialize(schema) ⇒ GraphQLExecutor
Returns a new instance of GraphQLExecutor.
578 579 580 |
# File 'lib/tina4/graphql.rb', line 578 def initialize(schema) @schema = schema end |
Instance Method Details
#execute(document, variables: {}, context: {}, operation_name: nil) ⇒ Object
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 |
# File 'lib/tina4/graphql.rb', line 582 def execute(document, variables: {}, context: {}, operation_name: nil) # Collect fragments fragments = {} operations = [] document[:definitions].each do |defn| case defn[:kind] when :fragment fragments[defn[:name]] = defn when :operation operations << defn end end # Pick the operation operation = if operation_name operations.find { |op| op[:name] == operation_name } elsif operations.length == 1 operations.first else raise GraphQLError, "Must provide operation name when multiple operations exist" end raise GraphQLError, "Unknown operation: #{operation_name}" unless operation # Resolve variables resolved_vars = resolve_variables(operation[:variables], variables) # Choose root fields root_fields = case operation[:operation] when :query then @schema.queries when :mutation then @schema.mutations else raise GraphQLError, "Unsupported operation: #{operation[:operation]}" end # Execute selection set data = {} errors = [] operation[:selection_set].each do |selection| resolve_selection(selection, root_fields, nil, resolved_vars, context, fragments, data, errors) end result = { "data" => data } result["errors"] = errors unless errors.empty? result end |