Skip to content

Commit

Permalink
https://github.com/bonfire-networks/bonfire-app/issues/916
Browse files Browse the repository at this point in the history
  • Loading branch information
mayel committed Apr 26, 2024
1 parent aae6f6a commit b033085
Show file tree
Hide file tree
Showing 7 changed files with 31,409 additions and 7 deletions.
1 change: 1 addition & 0 deletions deps.hex
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ geo = "~> 3.3"
zest = "~> 0.1"
dataloader = "~> 2.0.0"
absinthe_relay = "~> 1.5.2"
apical = "~> 0.2.1"
3 changes: 3 additions & 0 deletions lib/rest/masto_compatible/masto_compatible.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
defmodule Bonfire.API.MastoCompatible do
use Bonfire.UI.Common.Web, :controller
end
21 changes: 14 additions & 7 deletions lib/rest/rest_adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ defmodule Bonfire.API.GraphQL.RestAdapter do
}
end

def return(name, ret, conn) do
def return(name, ret, conn, transform_fun \\ nil) do
case ret do
%{data: data, errors: errors} ->
# TODO: include in response?
error(errors, "partial_graphql_errors")
{:ok, ret_data(data, name)}
{:ok, ret_data(data, name) |> transform_data(transform_fun)}

%{data: data} ->
{:ok, ret_data(data, name)}
{:ok, ret_data(data, name) |> transform_data(transform_fun)}

%{errors: errors} ->
error(errors)
Expand All @@ -42,20 +43,26 @@ defmodule Bonfire.API.GraphQL.RestAdapter do
end
end

defp transform_response(response, conn, transform_fun \\ nil)
defp transform_response({:ok, response}, conn, _), do: success_fn(response, conn)
defp transform_response({:error, response}, conn, _), do: error_fn(response, conn)
defp transform_response({:ok, response}, conn), do: success_fn(response, conn)
defp transform_response({:error, response}, conn), do: error_fn(response, conn)

def success_fn(response, conn) do
# Plug.Conn.send_resp(conn, 200, Jason.encode!(transform_data(response)))
Phoenix.Controller.json(conn, transform_data(response))
Phoenix.Controller.json(conn, response)
end

def error_fn(response, conn) do
# error transformation logic
Plug.Conn.send_resp(conn, 500, Jason.encode!(transform_data(response)))
end

def transform_data(data, transform_fun) when is_function(transform_fun, 1) do
transform_fun.(data)
|> transform_data()
end

def transform_data(data, _), do: transform_data(data)

def transform_data(data) when is_binary(data), do: data
def transform_data(%{} = data), do: Enums.maybe_to_map(data, true)
def transform_data(data) when is_list(data), do: Enum.map(data, &transform_data/1)
Expand Down
File renamed without changes.
27 changes: 27 additions & 0 deletions lib/web/masto_compatible_router.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
defmodule Bonfire.API.GraphQL.MastoCompatible.Router do
@api_spec Path.join(:code.priv_dir(:bonfire_api_graphql), "specs/akkoma-openapi.json")

defmacro include_masto_api do
quote do
scope "/" do
pipe_through(:basic_json)
pipe_through(:load_current_auth)

# add here to override wrong priority order of routes
get "/api/v1/accounts/verify_credentials",
Bonfire.API.MastoCompatible.AccountController,
:verify_credentials

get "/api/v1/accounts/:id", Bonfire.API.MastoCompatible.AccountController, :show

# require Apical
# Apical.router_from_file(unquote(@api_spec),
# controller: Bonfire.API.MastoCompatible,
# nest_all_json: false, # If enabled, nest all json request body payloads under the "_json" key. Otherwise objects payloads will be merged into `conn.params`.
# root: "/",
# dump: :all # temp: ony for debug
# )
end
end
end
end

0 comments on commit b033085

Please sign in to comment.