Is it possible to produce structs and types from absinthe's `object` macros?

Is it possible to automatically produce something like

  defstruct [:id, :audio_url, :status, :public_key, :sender, :receiver, :created_at]

  @type t :: %__MODULE__{
          id: String.t(),
          audio_url: URL.t() | nil, # if that scalar type is defined
          status: :created | :sent | :delivered | :heard | nil,
          public_key: PublicKey.t() | nil, # if PublicKey is also defined
          sender: User.t() | nil,
          receiver: User.t() | nil,
          created_at: NaiveDatetime.t() | nil
        }

within the same module where

  enum :voice_message_status do
    value(:created)
    value(:sent)
    value(:delivered)
    value(:heard)
  end

  object :voice_message do
    field(:id, non_null(:id))
    field(:audio_url, :url)
    field(:status, :voice_message_status)
    field(:public_key, :public_key)
    field(:sender, :user)
    field(:receiver, :user)
    field(:created_at, :naive_datetime)
  end

is defined?

Or perhaps not within the module where the object macro is used, but in the module specified manually.

object :voice_message, into_struct: App.VoiceMessage, into_type: :t do

This would allow me to have some compiler checks in the resolver functions where I return {:ok, %App.VoiceMessage{}} so that the fields in my structs match the schema.

2 Likes