module GraphQL::Schema::Member::HasArguments
Constants
- NO_ARGUMENTS
Public Class Methods
Source
# File lib/graphql/schema/member/has_arguments.rb, line 14 def self.extended(cls) cls.extend(ArgumentClassAccessor) cls.include(ArgumentObjectLoader) cls.extend(ClassConfigured) end
Source
# File lib/graphql/schema/member/has_arguments.rb, line 9 def self.included(cls) cls.extend(ArgumentClassAccessor) cls.include(ArgumentObjectLoader) end
Public Instance Methods
# File lib/graphql/schema/member/has_arguments.rb, line 93 def add_argument(arg_defn) @own_arguments ||= {} prev_defn = @own_arguments[arg_defn.name] case prev_defn when nil @own_arguments[arg_defn.name] = arg_defn when Array prev_defn << arg_defn when GraphQL::Schema::Argument @own_arguments[arg_defn.name] = [prev_defn, arg_defn] else raise "Invariant: unexpected `@own_arguments[#{arg_defn.name.inspect}]`: #{prev_defn.inspect}" end arg_defn end
Source
# File lib/graphql/schema/member/has_arguments.rb, line 258 def all_argument_definitions if !own_arguments.empty? all_defns = own_arguments.values all_defns.flatten! all_defns else EmptyObjects::EMPTY_ARRAY end end
Source
# File lib/graphql/schema/member/has_arguments.rb, line 143 def any_arguments? !own_arguments.empty? end
# File lib/graphql/schema/member/has_arguments.rb, line 53 def argument(arg_name = nil, type_expr = nil, desc = nil, **kwargs, &definition_block) if kwargs[:loads] loads_name = arg_name || kwargs[:name] loads_name_as_string = loads_name.to_s inferred_arg_name = case loads_name_as_string when /_id$/ loads_name_as_string.sub(/_id$/, "").to_sym when /_ids$/ loads_name_as_string.sub(/_ids$/, "") .sub(/([^s])$/, "\\1s") .to_sym else loads_name end kwargs[:as] ||= inferred_arg_name end kwargs[:owner] = self arg_defn = self.argument_class.new( arg_name, type_expr, desc, **kwargs, &definition_block ) add_argument(arg_defn) arg_defn end
Parameters
-
arg_name(Symbol) β The underscore-cased name of this argument,name:keyword also accepted -
type_exprβ TheGraphQLtype of this argument;type:keyword also accepted -
desc(String) βArgumentdescription,description:keyword also accepted -
definition_block(Proc) β Called with the newly-createdArgument -
kwargs(Hash) β Keywords for defining an argument. Any keywords not documented here must be handled by your baseArgumentclass.
Options
-
kwargs.:required(Boolean, :nullable) β if true, this argument is non-null; if false, this argument is nullable. If:nullable, then the argument must be provided, though it may benull. -
kwargs.:description(String) β Positional argument also accepted -
kwargs.:type(Class, Array<Class>) β Input type; positional argument also accepted -
kwargs.:name(Symbol) β positional argument also accepted -
kwargs.:default_value(Object) -
kwargs.:loads(Class, Array<Class>) β AGraphQLtype to load for the given ID when one is present -
kwargs.:as(Symbol) β Override the keyword name when passed to a method -
kwargs.:prepare(Symbol) β A method to call to transform this argumentβs valuebefore sending it to field resolution -
kwargs.:camelize(Boolean) β if true, the name will be camelized when building the schema -
kwargs.:from_resolver(Boolean) β if true, aResolverclass defined this argument -
kwargs.:directives(Hash{Class => Hash}) -
kwargs.:deprecation_reason(String) -
kwargs.:comment(String) β Private, used by GraphQL-Ruby when parsingGraphQLschema files -
kwargs.:ast_node(GraphQL::Language::Nodes::InputValueDefinition) β Private, used by GraphQL-Ruby when parsing schema files -
kwargs.:validates(Hash, nil) β Options for building validators, if any should be applied -
kwargs.:replace_null_with_default(Boolean) β iftrue, incoming values ofnullwill be replaced with the configureddefault_value
Returns
-
GraphQL::Schema::Argumentβ An instance ofargument_classcreated from these arguments
# File lib/graphql/schema/member/has_arguments.rb, line 291 def argument_class(new_arg_class = nil) self.class.argument_class(new_arg_class) end
Parameters
-
new_arg_class(Class) β A class to use for building argument definitions
# File lib/graphql/schema/member/has_arguments.rb, line 130 def arguments(context = GraphQL::Query::NullContext.instance, _require_defined_arguments = nil) if !own_arguments.empty? own_arguments_that_apply = {} own_arguments.each do |name, args_entry| if (visible_defn = Warden.visible_entry?(:visible_argument?, args_entry, context)) own_arguments_that_apply[visible_defn.graphql_name] = visible_defn end end end # might be nil if there are actually no arguments own_arguments_that_apply || own_arguments end
Returns
-
Hash<String => GraphQL::Schema::Argument>β Arguments defined on this thing, keyed by name. Includes inherited definitions
# File lib/graphql/schema/member/has_arguments.rb, line 377 def arguments_statically_coercible? if defined?(@arguments_statically_coercible) && !@arguments_statically_coercible.nil? @arguments_statically_coercible else @arguments_statically_coercible = all_argument_definitions.all?(&:statically_coercible?) end end
# File lib/graphql/schema/member/has_arguments.rb, line 274 def get_argument(argument_name, context = GraphQL::Query::NullContext.instance) warden = Warden.from_context(context) if (arg_config = own_arguments[argument_name]) && ((context.respond_to?(:types) && context.types.is_a?(GraphQL::Schema::Visibility::Profile)) || (visible_arg = Warden.visible_entry?(:visible_argument?, arg_config, context, warden))) visible_arg || arg_config elsif defined?(@resolver_class) && @resolver_class @resolver_class.get_field_argument(argument_name, context) else nil end end
Returns
-
GraphQL::Schema::Argument, nilβArgumentdefined on this thing, fetched by name.
Source
# File lib/graphql/schema/member/has_arguments.rb, line 518 def own_arguments @own_arguments || NO_ARGUMENTS end
Source
# File lib/graphql/schema/member/has_arguments.rb, line 109 def remove_argument(arg_defn) prev_defn = @own_arguments[arg_defn.name] case prev_defn when nil # done when Array prev_defn.delete(arg_defn) when GraphQL::Schema::Argument @own_arguments.delete(arg_defn.name) else raise "Invariant: unexpected `@own_arguments[#{arg_defn.name.inspect}]`: #{prev_defn.inspect}" end nil end
# File lib/graphql/schema/member/has_arguments.rb, line 362 def validate_directive_argument(arg_defn, value) # this is only implemented on directives. nil end
Usually, this is validated statically by RequiredArgumentsArePresent, but not for directives. TODO apply static validations on schema definitions?