Can absinthe do additional process/transformation on object
before resolving field
inside object
, for example, can i preload an additional schema from ecto struct data, which preloaded data may be used in resolving multiple field for an object?
Example:
Currently my absinthe codebase serves user
graphql object, which is backed by user
Ecto schema. user
table currently bloated with too many column, and i want to progressively refactor user
table from one big table into one core table and multiple smaller table. This refactor require changes in user
graphql object field resolution, now some of the field (like username
) need to access data from one of the smaller table rather than core table.
# Old GraphQL object definition
object :user do
field :id, :string
field :email, :string
field :username, :string
field :phone_number, :string
end
# Old Ecto Schema
schema "users" do
field(:email, :string)
field(:username, :string)
field(:phone_number, :string)
end
# Refactored Ecto Schema
schema "users" do
field(:username, :string)
has_one(:user_profile, UserProfiles, foreign_key: :user_id)
end
schema "user_profiles" do
belongs_to(:user, User,
foreign_key: :user_id,
references: :id,
type: :string
)
field(:email, :string)
field(:phone_number, :string)
end
# Ideal change in graphql schema
# Preload `user` with `user_profile`
object :user do
field :id, :string
field :email, :string, do: resolve(fn user, _, _ -> user.user_profile.email end)
field :username, :string
field :phone_number, :string, do: resolve(fn user, _, _ -> user.user_profile.phone_number end)
end