Grephql.TypeMapper (Grephql v0.10.1)

Copy Markdown View Source

Maps GraphQL type references to Ecto schema types.

Resolves the corresponding Ecto type (for embedded schema field definitions) and nullability from a parsed GraphQL type reference.

Scalar mapping

Built-in GraphQL scalars map to Ecto primitives:

  • String:string
  • Int:integer
  • Float:float
  • Boolean:boolean
  • ID:string
  • DateTimeGrephql.Types.DateTime
  • Date:date
  • JSON / JSONObject:map
  • URI / URL:string
  • BigInt / Long:integer
  • HTML:string
  • UnsignedInt64:integer
  • Base64String:string

Custom scalars map to user-provided Ecto.Type modules via the scalar_types config. Custom scalars override built-in defaults. Unknown scalars raise CompileError.

Enum mapping

Enum types are automatically resolved using Grephql.Types.Enum (a parameterized Ecto type). No user configuration is needed — enum values are read from the schema at compile time. Users can still override enum types via scalar_types if custom serialization is needed.

Summary

Functions

Resolves a GraphQL type reference to its Ecto type and nullability.

Types

ecto_type()

@type ecto_type() ::
  :string
  | :integer
  | :float
  | :boolean
  | :date
  | :map
  | {:array, ecto_type()}
  | {:object, String.t()}
  | module()

resolve_result()

@type resolve_result() :: %{
  ecto_type: ecto_type(),
  nullable: boolean(),
  enum_values: [String.t()] | nil,
  inner_nullable: boolean() | nil
}

scalar_types()

@type scalar_types() :: %{required(String.t()) => module()}

Functions

resolve(type_ref, schema, scalar_types)

@spec resolve(Grephql.Schema.TypeRef.t(), Grephql.Schema.t(), scalar_types()) ::
  resolve_result()

Resolves a GraphQL type reference to its Ecto type and nullability.

Returns a map with:

  • :ecto_type — the Ecto type for schema field definition
  • :nullable — whether the field allows nil
  • :enum_values — enum value strings when type is Grephql.Types.Enum, nil otherwise

Parameters

  • type_ref — the GraphQL type reference to resolve
  • schema — the parsed GraphQL schema (for enum value lookup)
  • scalar_types — user-provided custom scalar mappings (default: %{})