TypedGql.TypeMapper (TypedGql v0.13.0)

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
  • DateTimeTypedGql.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 TypedGql.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

Builds the typespec AST for a (possibly nested) list type, with leaf_ast standing in for the innermost element.

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

list_type_ast(type_ref, leaf_ast)

@spec list_type_ast(TypedGql.Schema.TypeRef.t(), Macro.t()) :: Macro.t()

Builds the typespec AST for a (possibly nested) list type, with leaf_ast standing in for the innermost element.

Every list level and every element carries its own nullability in GraphQL and the Ecto type keeps none of it, so the AST is built from the type reference: [Post] with leaf Post.t() gives [Post.t() | nil].

The outermost | nil is deliberately omitted — typed_structor appends it from the field's null: option, so a generation plugin flipping nullability still flows through.

resolve(type_ref, schema, scalar_types)

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 TypedGql.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: %{})