Method: GraphQL::Schema::Enum.coerce_input

Defined in:
lib/graphql/schema/enum.rb

.coerce_input(value_name, ctx) ⇒ Object

Called by the runtime with incoming string representations from a query. It will match the string to a configured by name or by Ruby value.

Parameters:

  • value_name (String, Object)

    A string from a GraphQL query, or a Ruby value matching a value(..., value: ...) configuration

  • ctx (GraphQL::Query::Context)

Returns:

Raises:



216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/graphql/schema/enum.rb', line 216

def coerce_input(value_name, ctx)
  all_values = ctx.types ? ctx.types.enum_values(self) : values.each_value

  # This tries matching by incoming GraphQL string, then checks Ruby-defined values
  if v = (all_values.find { |val| val.graphql_name == value_name } || all_values.find { |val| val.value == value_name })
    if v.authorized?(ctx)
      v.value
    else
      raise GraphQL::UnauthorizedEnumValueError.new(type: self, enum_value: v, context: ctx)
    end
  else
    nil
  end
end