Class: GraphQL::Schema::Argument
- Inherits:
-
Object
- Object
- GraphQL::Schema::Argument
- Defined in:
- lib/graphql/schema/argument.rb
Constant Summary collapse
- NO_DEFAULT =
:__no_default__
Instance Attribute Summary collapse
-
#default_value ⇒ Object
readonly
The value used when the client doesn’t provide a value for this argument.
-
#description(text = nil) ⇒ String
Documentation for this argument.
-
#keyword ⇒ Symbol
readonly
This argument’s name in Ruby keyword arguments.
-
#loads ⇒ Class, ...
readonly
If this argument should load an application object, this is the type of object to load.
-
#name ⇒ String
(also: #graphql_name)
readonly
The GraphQL name for this argument, camelized unless
camelize: false
is provided. -
#owner ⇒ GraphQL::Schema::Field, Class
readonly
The field or input object this argument belongs to.
-
#prepare ⇒ Symbol
readonly
A method to call to transform this value before sending it to field resolution method.
Instance Method Summary collapse
-
#accessible?(context) ⇒ Boolean
-
#authorized?(obj, ctx) ⇒ Boolean
-
#default_value? ⇒ Boolean
True if this argument has a default value.
-
#from_resolver? ⇒ Boolean
True if a resolver defined this argument.
-
#initialize(arg_name = nil, type_expr = nil, desc = nil, required:, type: nil, name: nil, loads: nil, description: nil, ast_node: nil, default_value: NO_DEFAULT, as: nil, from_resolver: false, camelize: true, prepare: nil, method_access: true, owner:, &definition_block) ⇒ Argument
constructor
A new instance of Argument.
-
#prepare_value(obj, value) ⇒ Object
private
Apply the #prepare configuration to
value
, using methods fromobj
. -
#to_graphql ⇒ Object
-
#type ⇒ Object
-
#visible?(context) ⇒ Boolean
Methods included from Member::HasPath
Methods included from Member::CachedGraphQLDefinition
#graphql_definition, #initialize_copy
Constructor Details
#initialize(arg_name = nil, type_expr = nil, desc = nil, required:, type: nil, name: nil, loads: nil, description: nil, ast_node: nil, default_value: NO_DEFAULT, as: nil, from_resolver: false, camelize: true, prepare: nil, method_access: true, owner:, &definition_block) ⇒ Argument
Returns a new instance of Argument
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/graphql/schema/argument.rb', line 43 def initialize(arg_name = nil, type_expr = nil, desc = nil, required:, type: nil, name: nil, loads: nil, description: nil, ast_node: nil, default_value: NO_DEFAULT, as: nil, from_resolver: false, camelize: true, prepare: nil, method_access: true, owner:, &definition_block) arg_name ||= name name_str = camelize ? Member::BuildType.camelize(arg_name.to_s) : arg_name.to_s @name = name_str.freeze @type_expr = type_expr || type @description = desc || description @null = !required @default_value = default_value @owner = owner @as = as @loads = loads @keyword = as || Schema::Member::BuildType.underscore(@name).to_sym @prepare = prepare @ast_node = ast_node @from_resolver = from_resolver @method_access = method_access if definition_block if definition_block.arity == 1 instance_exec(self, &definition_block) else instance_eval(&definition_block) end end end |
Instance Attribute Details
#default_value ⇒ Object (readonly)
Returns the value used when the client doesn’t provide a value for this argument
70 71 72 |
# File 'lib/graphql/schema/argument.rb', line 70 def default_value @default_value end |
#description(text = nil) ⇒ String
Returns Documentation for this argument
80 81 82 83 84 85 86 |
# File 'lib/graphql/schema/argument.rb', line 80 def description(text = nil) if text @description = text else @description end end |
#keyword ⇒ Symbol (readonly)
Returns This argument’s name in Ruby keyword arguments
22 23 24 |
# File 'lib/graphql/schema/argument.rb', line 22 def keyword @keyword end |
#loads ⇒ Class, ... (readonly)
Returns If this argument should load an application object, this is the type of object to load
25 26 27 |
# File 'lib/graphql/schema/argument.rb', line 25 def loads @loads end |
#name ⇒ String (readonly) Also known as: graphql_name
Returns the GraphQL name for this argument, camelized unless camelize: false
is provided
12 13 14 |
# File 'lib/graphql/schema/argument.rb', line 12 def name @name end |
#owner ⇒ GraphQL::Schema::Field, Class (readonly)
Returns The field or input object this argument belongs to
16 17 18 |
# File 'lib/graphql/schema/argument.rb', line 16 def owner @owner end |
#prepare ⇒ Symbol (readonly)
Returns A method to call to transform this value before sending it to field resolution method
19 20 21 |
# File 'lib/graphql/schema/argument.rb', line 19 def prepare @prepare end |
Instance Method Details
#accessible?(context) ⇒ Boolean
92 93 94 |
# File 'lib/graphql/schema/argument.rb', line 92 def accessible?(context) true end |
#authorized?(obj, ctx) ⇒ Boolean
96 97 98 |
# File 'lib/graphql/schema/argument.rb', line 96 def (obj, ctx) true end |
#default_value? ⇒ Boolean
Returns True if this argument has a default value
73 74 75 |
# File 'lib/graphql/schema/argument.rb', line 73 def default_value? @default_value != NO_DEFAULT end |
#from_resolver? ⇒ Boolean
Returns true if a resolver defined this argument
28 29 30 |
# File 'lib/graphql/schema/argument.rb', line 28 def from_resolver? @from_resolver end |
#prepare_value(obj, value) ⇒ Object
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.
Apply the #prepare configuration to value
, using methods from obj
.
Used by the runtime.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/graphql/schema/argument.rb', line 123 def prepare_value(obj, value) if value.is_a?(GraphQL::Schema::InputObject) value = value.prepare end if @prepare.nil? value elsif @prepare.is_a?(String) || @prepare.is_a?(Symbol) obj.public_send(@prepare, value) elsif @prepare.respond_to?(:call) @prepare.call(value, obj.context) else raise "Invalid prepare for #{@owner.name}.name: #{@prepare.inspect}" end end |
#to_graphql ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/graphql/schema/argument.rb', line 100 def to_graphql argument = GraphQL::Argument.new argument.name = @name argument.type = -> { type } argument.description = @description argument.[:type_class] = self argument.as = @as argument.method_access = @method_access if NO_DEFAULT != @default_value argument.default_value = @default_value end argument end |
#type ⇒ Object
114 115 116 117 118 |
# File 'lib/graphql/schema/argument.rb', line 114 def type @type ||= Member::BuildType.parse_type(@type_expr, null: @null) rescue StandardError => err raise ArgumentError, "Couldn't build type for Argument #{@owner.name}.#{name}: #{err.class.name}: #{err.}", err.backtrace end |
#visible?(context) ⇒ Boolean
88 89 90 |
# File 'lib/graphql/schema/argument.rb', line 88 def visible?(context) true end |