Skip to content

Commit

Permalink
Add nearest_endpoints GraphQL query
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmanzanera committed Sep 15, 2022
1 parent ed1e678 commit ee2a6ab
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/archethic_web/graphl_context.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
defmodule ArchethicWeb.GraphQLContext do
@moduledoc false

@behaviour Plug

def init(opts), do: opts

def call(conn, _) do
context = build_context(conn)
Absinthe.Plug.put_options(conn, context: context)
end

def build_context(conn) do
%{ip: conn.remote_ip}
end
end
12 changes: 12 additions & 0 deletions lib/archethic_web/graphql_schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,24 @@ defmodule ArchethicWeb.GraphQLSchema do
end)
end

@desc """
List all the nodes registered in the network
"""
field :nodes, list_of(:node) do
resolve(fn _, _ ->
{:ok, Resolver.nodes()}
end)
end

@desc """
List the nearest endpoints nodes from the client's IP
"""
field :nearest_endpoints, list_of(:node) do
resolve(fn _, %{context: %{ip: ip}} ->
{:ok, Resolver.nearest_endpoints(ip)}
end)
end

@desc """
Query the network to list the transaction on the type
"""
Expand Down
13 changes: 13 additions & 0 deletions lib/archethic_web/graphql_schema/resolver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,19 @@ defmodule ArchethicWeb.GraphQLSchema.Resolver do
)
end

def nearest_endpoints(ip) do
geo_patch = P2P.get_geo_patch(ip)
nearest_nodes = P2P.nearest_nodes(P2P.list_nodes(), geo_patch)

Enum.map(
nearest_nodes,
&%{
ip: :inet.ntoa(&1.ip),
port: &1.http_port
}
)
end

def network_transactions(type, page) do
TransactionChain.list_transactions_by_type(type, [])
|> paginate_transactions(page)
Expand Down
1 change: 1 addition & 0 deletions lib/archethic_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ defmodule ArchethicWeb.Router do

pipeline :api do
plug(:accepts, ["json"])
plug(ArchethicWeb.GraphQLContext)
end

# Add the on chain implementation of the archethic.io at the root of the webserver
Expand Down

0 comments on commit ee2a6ab

Please sign in to comment.