Module: Rails::GraphQL::Helpers::WithArguments
- Included in:
- Directive, Field::OutputField
- Defined in:
- lib/rails/graphql/helpers/with_arguments.rb
Overview
Helper module that allows other objects to hold arguments
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#=~(other) ⇒ Object
Check if all the arguments are compatible.
-
#argument(name, type = nil, **xargs) ⇒ Object
See Argument class.
-
#has_argument?(name) ⇒ Boolean
Check if a given
name
is already defined on the list of arguments. -
#has_arguments? ⇒ Boolean
Check if any argument has been defined at all.
-
#id_argument(*args, **xargs, &block) ⇒ Object
A short cute for arguments named and typed as id.
- #initialize(*args, arguments: nil, **xargs, &block) ⇒ Object
- #initialize_copy(orig) ⇒ Object
-
#ref_argument(object) ⇒ Object
Since arguments’ owner are more flexible, their instances can be directly associated to objects that have argument.
-
#validate! ⇒ Object
Validate all the arguments to make sure the definition is valid.
Class Method Details
.extended(other) ⇒ Object
8 9 10 11 12 |
# File 'lib/rails/graphql/helpers/with_arguments.rb', line 8 def self.extended(other) other.extend(Helpers::InheritedCollection) other.extend(WithArguments::ClassMethods) other.inherited_collection(:arguments, type: :hash) end |
.included(other) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/rails/graphql/helpers/with_arguments.rb', line 14 def self.included(other) other.define_method(:arguments) do @arguments ||= {} end other.define_method(:all_arguments) do @arguments if defined?(@arguments) end other.define_method(:arguments?) do defined?(@arguments) && @arguments.present? end end |
Instance Method Details
#=~(other) ⇒ Object
Check if all the arguments are compatible
57 58 59 |
# File 'lib/rails/graphql/helpers/with_arguments.rb', line 57 def =~(other) super && other.respond_to?(:all_arguments) && match_arguments?(other) end |
#argument(name, type = nil, **xargs) ⇒ Object
See Argument class.
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/rails/graphql/helpers/with_arguments.rb', line 62 def argument(name, type = nil, **xargs) object = GraphQL::Argument.new(name, type, **xargs, owner: self) raise DuplicatedError, (+<<~MSG).squish if has_argument?(object.name) The #{name.inspect} argument is already defined and can't be redefined. MSG arguments[object.name] = object self rescue DefinitionError => e raise e.class, +"#{e.}\n Defined at: #{caller(2)[0]}" end |
#has_argument?(name) ⇒ Boolean
Check if a given name
is already defined on the list of arguments
99 100 101 |
# File 'lib/rails/graphql/helpers/with_arguments.rb', line 99 def has_argument?(name) defined?(@arguments) && @arguments.key?(name) end |
#has_arguments? ⇒ Boolean
Check if any argument has been defined at all
104 105 106 |
# File 'lib/rails/graphql/helpers/with_arguments.rb', line 104 def has_arguments? defined?(@arguments) && !@arguments.empty? end |
#id_argument(*args, **xargs, &block) ⇒ Object
A short cute for arguments named and typed as id
92 93 94 95 96 |
# File 'lib/rails/graphql/helpers/with_arguments.rb', line 92 def id_argument(*args, **xargs, &block) name = args.size >= 1 ? args.shift : :id xargs[:null] = false unless xargs.key?(:null) argument(name, :id, *args, **xargs, &block) end |
#initialize(*args, arguments: nil, **xargs, &block) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/rails/graphql/helpers/with_arguments.rb', line 38 def initialize(*args, arguments: nil, **xargs, &block) @arguments = GraphQL.enumerate(arguments).each_with_object({}) do |item, hash| raise ArgumentError, (+<<~MSG).squish unless item.is_a?(Argument) The given "#{item.inspect}" is not a valid Argument object. MSG hash[item.name] = Helpers.dup_with_owner(item, self) end unless arguments.nil? super(*args, **xargs, &block) end |
#initialize_copy(orig) ⇒ Object
50 51 52 53 54 |
# File 'lib/rails/graphql/helpers/with_arguments.rb', line 50 def initialize_copy(orig) super @arguments = Helpers.dup_all_with_owner(orig.arguments.transform_values, self) end |
#ref_argument(object) ⇒ Object
Since arguments’ owner are more flexible, their instances can be directly associated to objects that have argument
77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/rails/graphql/helpers/with_arguments.rb', line 77 def ref_argument(object) raise ArgumentError, (+<<~MSG).squish unless object.is_a?(GraphQL::Argument) The given object #{object.inspect} is not a valid argument. MSG raise DuplicatedError, (+<<~MSG).squish if has_argument?(object.name) The #{object.name.inspect} argument is already defined and can't be redefined. MSG (@arguments ||= {})[object.name] = object rescue DefinitionError => e raise e.class, +"#{e.}\n Defined at: #{caller(2)[0]}" end |
#validate! ⇒ Object
Validate all the arguments to make sure the definition is valid
109 110 111 112 113 114 115 |
# File 'lib/rails/graphql/helpers/with_arguments.rb', line 109 def validate!(*) super if defined? super return unless defined? @arguments @arguments.each_value(&:validate!) @arguments.freeze end |