mochi/input_coercion

Input value coercion and validation for GraphQL arguments.

This module validates and coerces input values (arguments, variables) against their declared types in the schema. It ensures:

Example

let result = input_coercion.coerce_argument_value(
  ast.StringValue("hello"),
  schema.Named("String"),
  schema,
  variables,
  ["createUser", "input", "name"],
)
// Ok(dynamic("hello"))

Types

Errors that can occur during input coercion

pub type CoercionError {
  TypeMismatch(path: List(String), expected: String, got: String)
  InvalidEnumValue(
    path: List(String),
    enum_name: String,
    value: String,
  )
  MissingRequiredField(path: List(String), field: String)
  UnknownField(
    path: List(String),
    field: String,
    type_name: String,
  )
  UnknownArgument(path: List(String), name: String)
  NullNotAllowed(path: List(String))
  UnknownType(path: List(String), type_name: String)
  ScalarCoercionFailed(
    path: List(String),
    scalar_name: String,
    reason: String,
  )
}

Constructors

  • TypeMismatch(path: List(String), expected: String, got: String)

    Value type doesn’t match expected type

  • InvalidEnumValue(
      path: List(String),
      enum_name: String,
      value: String,
    )

    Enum value is not valid for the enum type

  • MissingRequiredField(path: List(String), field: String)

    Required field is missing from input object

  • UnknownField(
      path: List(String),
      field: String,
      type_name: String,
    )

    Unknown field provided in input object

  • UnknownArgument(path: List(String), name: String)

    Unknown argument provided on a field

  • NullNotAllowed(path: List(String))

    Null value provided for non-null type

  • UnknownType(path: List(String), type_name: String)

    Referenced type not found in schema

  • ScalarCoercionFailed(
      path: List(String),
      scalar_name: String,
      reason: String,
    )

    Custom scalar rejected the value

Result of coercion - either a Dynamic value or an error

pub type CoercionResult =
  Result(dynamic.Dynamic, CoercionError)

Values

pub fn coerce_argument_value(
  value: @internal Value,
  expected_type: schema.FieldType,
  schema: schema.Schema,
  variables: dict.Dict(String, dynamic.Dynamic),
  path: List(String),
) -> Result(dynamic.Dynamic, CoercionError)

Coerce and validate an argument value against its declared type.

This is the main entry point for input validation. It recursively validates the value structure against the schema type definition.

pub fn coerce_arguments(
  ast_args: List(@internal Argument),
  arg_defs: dict.Dict(String, schema.ArgumentDefinition),
  schema: schema.Schema,
  variables: dict.Dict(String, dynamic.Dynamic),
  field_path: List(String),
) -> Result(dict.Dict(String, dynamic.Dynamic), CoercionError)

Coerce all arguments for a field, returning either all coerced values or the first error

pub fn format_error(error: CoercionError) -> String

Format a coercion error as a human-readable message

Search Document