Commit 94d2289a authored by Ivan Minutillo's avatar Ivan Minutillo

absinthe basic setup and basic agent query

parent fd8c4feb
Pipeline #29908960 failed with stages
in 3 minutes and 26 seconds
defmodule Pleroma.Context do
@behaviour Plug
import Plug.Conn
alias Pleroma.{Repo}
alias Pleroma.Web.OAuth.Token
def init(opts), do: opts
def call(conn, _) do
context = build_context(conn)
Absinthe.Plug.put_options(conn, context: context)
end
defp build_context(conn) do
with {:ok, current_user} <- authorize(conn) do
%{current_user: current_user}
else
err -> %{current_user: err}
end
end
defp authorize(conn) do
token =
case get_req_header(conn, "authorization") do
["Bearer " <> header] -> header
_ -> get_session(conn, :oauth_token)
end
with token when not is_nil(token) <- token,
%Token{user_id: user_id} <- Repo.get_by(Token, token: token)
do
IO.inspect user_id
{:ok, user_id}
else
err -> {:error, err}
end
end
end
defmodule Pleroma.Schema do
use Absinthe.Schema
import_types Pleroma.Schema.Types
# alias Pleroma.Resolver
query do
# @desc "Get instance info"
# field :instance, type :instance do
# resolve &Pleroma.InstanceResolver.find/2
# end
@desc "Get all agents"
field :all_agents, non_null(list_of(non_null(:agent))) do
resolve &Pleroma.UserResolver.all/2
end
@desc "Get an agent"
field :agent, type: :agent do
arg :id, non_null(:id)
resolve &Pleroma.UserResolver.find/2
end
@desc "Get my agent"
field :my_agent, type: :agent do
resolve &Pleroma.UserResolver.profile/2
end
end
end
defmodule Pleroma.Schema.Types do
use Absinthe.Schema.Notation
object :instance do
field :uri, :string
field :title, :string
field :description, :string
field :email, :string
end
object :agent do
field :id, :id
field :avatar, :string
field :name, :string
field :email, :string
field :bio, :string
field :nickname, :string
field :ap_id, :string
field :local, :boolean
field :follower_address, :string
end
end
......@@ -56,6 +56,18 @@ defmodule Pleroma.Web.Router do
plug(:accepts, ["html", "json"])
end
pipeline :graphql do
plug Pleroma.Context
end
scope "/" do
pipe_through :graphql
forward "/graphiql", Absinthe.Plug.GraphiQL,
schema: Pleroma.Schema,
interface: :simple
end
scope "/api/pleroma", Pleroma.Web.TwitterAPI do
pipe_through(:pleroma_api)
get("/password_reset/:token", UtilController, :show_password_reset)
......
......@@ -48,7 +48,10 @@ defmodule Pleroma.Mixfile do
{:mogrify, "~> 0.6.1"},
{:ex_machina, "~> 2.2", only: :test},
{:credo, "~> 0.9.3", only: [:dev, :test]},
{:mock, "~> 0.3.1", only: :test}
{:mock, "~> 0.3.1", only: :test},
{:absinthe, "~> 1.4"},
{:absinthe_plug, "~> 1.4"}
# {:poison, "~> 2.1.0"}
]
end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment