Class: Rails::GraphQL::Source::ScopedArguments::Argument

Inherits:
Argument
  • Object
show all
Defined in:
lib/rails/graphql/source/scoped_arguments.rb

Overview

Extended argument class to be the instance of the scoped

Instance Attribute Summary

Attributes inherited from Argument

#default, #gql_name, #name, #node, #owner, #type

Instance Method Summary collapse

Methods inherited from Argument

#+, #==, #=~, #array?, #as_json, #default_value?, #description, #deserialize, #initialize_copy, #inspect, #null?, #nullable?, #to_json, #type_klass, #valid_input?, #validate!, #validate_output!

Methods included from Helpers::WithDescription

#desc, #description, #description=, #description?

Constructor Details

#initialize(*args, block:, on: nil, **xargs) ⇒ Argument

Returns a new instance of Argument.



15
16
17
18
19
20
# File 'lib/rails/graphql/source/scoped_arguments.rb', line 15

def initialize(*args, block:, on: nil, **xargs)
  super(*args, **xargs)

  @block = (block == true) ? name : block
  @fields = on
end

Instance Method Details

#apply_to(object, value) ⇒ Object

Apply the argument block to the given object, using or not the value

Raises:

  • (::ArgumentError)


23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rails/graphql/source/scoped_arguments.rb', line 23

def apply_to(object, value)
  callable = @block.is_a?(Symbol) ? object.method(@block) : @block
  raise ::ArgumentError, (+<<~MSG) unless callable.respond_to?(:call)
    Unable to call "#{@block.inspect}" on #{object.class}.
  MSG

  args = (callable.arity == 1 || callable.arity == -1) ? [value] : nil

  return callable.call(*args) if callable.is_a?(Method)
  object.instance_exec(*args, &callable)
end

#attach_to?(field) ⇒ Boolean

Check if the argument should be attached to the given field

Returns:

  • (Boolean)


36
37
38
39
40
41
42
# File 'lib/rails/graphql/source/scoped_arguments.rb', line 36

def attach_to?(field)
  return true if @fields.nil?

  GraphQL.enumerate(@fields).any? do |item|
    (item.is_a?(Symbol) && field.name.eql?(item)) || field.gql_name.eql?(item)
  end
end