Class: GraphQL::Execution::Interpreter::Arguments

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Dig
Defined in:
lib/graphql/execution/interpreter/arguments.rb

Overview

A wrapper for argument hashes in GraphQL queries.

This object is immutable so that the runtime code can be sure that modifications don’t leak from one use to another

See Also:

  • to get access to these objects.

Constant Summary collapse

NO_ARGS =
GraphQL::EmptyObjects::EMPTY_HASH
EMPTY =
self.new(argument_values: nil, keyword_arguments: NO_ARGS).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Dig

#dig

Constructor Details

#initialize(keyword_arguments: nil, argument_values:) ⇒ Arguments

Returns a new instance of Arguments.

Parameters:

  • argument_values (nil, Hash{Symbol => ArgumentValue})
  • keyword_arguments (nil, Hash{Symbol => Object}) (defaults to: nil)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/graphql/execution/interpreter/arguments.rb', line 24

def initialize(keyword_arguments: nil, argument_values:)
  @empty = argument_values.nil? || argument_values.empty?
  # This is only present when `extras` have been merged in:
  if keyword_arguments
    # This is a little crazy. We expect the `:argument_details` extra to _include extras_,
    # but the object isn't created until _after_ extras are put together.
    # So, we have to use a special flag here to say, "at the last minute, add yourself to the keyword args."
    #
    # Otherwise:
    # - We can't access the final Arguments instance _while_ we're preparing extras
    # - After we _can_ access it, it's frozen, so we can't add anything.
    #
    # So, this flag gives us a chance to sneak it in before freezing, _and_ while we have access
    # to the new Arguments instance itself.
    if keyword_arguments[:argument_details] == :__arguments_add_self
      keyword_arguments[:argument_details] = self
    end
    @keyword_arguments = keyword_arguments.freeze
  elsif !@empty
    @keyword_arguments = {}
    argument_values.each do |name, arg_val|
      @keyword_arguments[name] = arg_val.value
    end
    @keyword_arguments.freeze
  else
    @keyword_arguments = NO_ARGS
  end
  @argument_values = argument_values ? argument_values.freeze : NO_ARGS
  freeze
end

Instance Attribute Details

#argument_valuesHash{Symbol => ArgumentValue} (readonly)

Returns:



56
57
58
# File 'lib/graphql/execution/interpreter/arguments.rb', line 56

def argument_values
  @argument_values
end

#keyword_argumentsHash<Symbol, Object> (readonly)

The Ruby-style arguments hash, ready for a resolver. This hash is the one used at runtime.

Returns:

  • (Hash<Symbol, Object>)


20
21
22
# File 'lib/graphql/execution/interpreter/arguments.rb', line 20

def keyword_arguments
  @keyword_arguments
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/graphql/execution/interpreter/arguments.rb', line 58

def empty?
  @empty
end

#inspectObject



65
66
67
# File 'lib/graphql/execution/interpreter/arguments.rb', line 65

def inspect
  "#<#{self.class} @keyword_arguments=#{keyword_arguments.inspect}>"
end

#merge_extras(extra_args) ⇒ Interpreter::Arguments

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a new arguments instance which includes these extras.

This is called by the runtime to implement field extras: [...]

Parameters:

  • extra_args (Hash<Symbol => Object>)

Returns:



76
77
78
79
80
81
# File 'lib/graphql/execution/interpreter/arguments.rb', line 76

def merge_extras(extra_args)
  self.class.new(
    argument_values: argument_values,
    keyword_arguments: keyword_arguments.merge(extra_args)
  )
end