Method: GraphQL::Schema::Directive#initialize
- Defined in:
- lib/graphql/schema/directive.rb
#initialize(owner, **arguments) ⇒ Directive
Returns a new instance of Directive.
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/graphql/schema/directive.rb', line 122 def initialize(owner, **arguments) @owner = owner assert_valid_owner # It's be nice if we had the real context here, but we don't. What we _would_ get is: # - error handling # - lazy resolution # Probably, those won't be needed here, since these are configuration arguments, # not runtime arguments. context = Query::NullContext.instance self.class.all_argument_definitions.each do |arg_defn| keyword = arg_defn.keyword arg_type = arg_defn.type if arguments.key?(keyword) value = arguments[keyword] # This is a Ruby-land value; convert it to graphql for validation graphql_value = begin coerce_value = value if arg_type.list? && (!coerce_value.nil?) && (!coerce_value.is_a?(Array)) # When validating inputs, GraphQL accepts a single item # and implicitly converts it to a one-item list. # However, we're using result coercion here to go from Ruby value # to GraphQL value, so it doesn't have that feature. # Keep the GraphQL-type behavior but implement it manually: wrap_type = arg_type while wrap_type.list? if wrap_type.non_null? wrap_type = wrap_type.of_type end wrap_type = wrap_type.of_type coerce_value = [coerce_value] end end arg_type.coerce_isolated_result(coerce_value) rescue GraphQL::Schema::Enum::UnresolvedValueError # Let validation handle this value end else value = graphql_value = nil end result = arg_type.validate_input(graphql_value, context) if !result.valid? raise InvalidArgumentError, "@#{graphql_name}.#{arg_defn.graphql_name} on #{owner.path} is invalid (#{value.inspect}): #{result.problems.first["explanation"]}" end end self.class.validate!(arguments, context) @arguments = self.class.coerce_arguments(nil, arguments, context) if @arguments.is_a?(GraphQL::ExecutionError) raise @arguments end end |