Class: GraphQL::DSL::Operation

Inherits:
Node
  • Object
show all
Includes:
SelectionSet
Defined in:
lib/graphql/dsl/nodes/operation.rb

Overview

Operation GraphQL node

Instance Attribute Summary collapse

Attributes inherited from Node

#__name, #__nodes

Instance Method Summary collapse

Methods included from SelectionSet

#__field, #__fragment, #__inline_fragment

Methods inherited from Node

#to_gql

Constructor Details

#initialize(operation_type, name = nil, variable_definitions = {}, directives = [], &block) ⇒ Operation

Create operation (query, mutation, subscription)

Parameters:

  • operation_type (Symbol)

    operation type

  • name (String, Symbol, nil) (defaults to: nil)

    operation name

  • variable_definitions (Hash) (defaults to: {})

    variable definitions

  • directives (Array<Directive, Hash, Array>) (defaults to: [])

    list of directives

  • block (Proc)

    declare DSL for sub-fields

Options Hash (operation_type):

  • :query (Symbol)

    query operation

  • :mutation (Symbol)

    mutation operation

  • :subscription (Symbol)

    subscription operation



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/graphql/dsl/nodes/operation.rb', line 33

def initialize(operation_type, name = nil, variable_definitions = {}, directives = [], &block)
  variable_definitions.each do |variable_name, _|
    raise Error, 'Variable name must be specified' if variable_name.nil? || variable_name.empty?
  end

  @__operation_type = operation_type
  @__variable_definitions = variable_definitions.transform_values do |variable_definition|
    VariableDefinition.from(variable_definition)
  end
  @__directives = directives.map { |directive| Directive.from(directive) }

  super(name, &block)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class GraphQL::DSL::SelectionSet

Instance Attribute Details

#__directivesArray<Directive> (readonly)

Returns list of directives.

Returns:



20
21
22
# File 'lib/graphql/dsl/nodes/operation.rb', line 20

def __directives
  @__directives
end

#__operation_typeSymbol (readonly)

Returns operation type (see #initialize).

Returns:



12
13
14
# File 'lib/graphql/dsl/nodes/operation.rb', line 12

def __operation_type
  @__operation_type
end

#__variable_definitionsHash<Symbol, VariableDefinition> (readonly)

Returns variable definitions.

Returns:



16
17
18
# File 'lib/graphql/dsl/nodes/operation.rb', line 16

def __variable_definitions
  @__variable_definitions
end

Instance Method Details

#__var(name, type, default: UNDEFINED, directives: []) ⇒ void

This method returns an undefined value.

Declare operation variable

Parameters:

  • name (Symbol, String)

    variable name

  • type (Symbol, String)

    variable type

  • directives (Array<Directive, Hash, Array>) (defaults to: [])

    variable directives

  • default (Object) (defaults to: UNDEFINED)

    variable default value



56
57
58
# File 'lib/graphql/dsl/nodes/operation.rb', line 56

def __var(name, type, default: UNDEFINED, directives: [])
  @__variable_definitions[name] = VariableDefinition.new(type, default, directives)
end