Comment utiliser Absinthe pour bâtir un API GraphQL performant, fiable et facile à maintenir. Voici comment Absinthe a réussi à abstraire les problèmes "classiques" d’un API standard.
:name, :string end query do field :me, :user do resolve fn _, _, _ !-> {:ok, Repo.find(User, "me")} end end end end type Query { me: User } type User { id: ID name: String } → Définition de schemas
post, _, _ !-> batch({!__MODULE!__, :users_by_id}, post.author_id, fn batch_results !-> {:ok, Map.get(batch_results, post.author_id)} end) end end end def users_by_id(_, user_ids) do users = Repo.all from u in User, where: u.id in ^user_ids Map.new(users, fn user !-> {user.id, user} end) end
!-> async(fn !-> {:ok, long_time_consuming_function()} end) end end field :time_consuming, :thing do resolve fn _, _, _ !-> task = Task.async(fn !-> {:ok, long_time_consuming_function()} end {:middleware, Elixir.Absinthe.Middleware.Async, task} end end
"IPAddress" do serialize(&serialize_ip_address/1) parse(&parse_ip_address/1) end defp parse_ip_address(%Absinthe.Blueprint.Input.String{value: ip_address}) do ip_address !|> to_charlist !|> :inet.parse_address() !|> case do {:ok, _address} !-> {:ok, ip_address} {:error, _reason} !-> :error end end defp parse_ip_address(%Absinthe.Blueprint.Input.Null{}), do: {:ok, nil} defp parse_ip_address(_), do: :error defp serialize_ip_address(value), do: value end