refactor: use middleware to check if GraphQL API is enabled (#10772)
parent
36e6f8307b
commit
ea9e806bd5
@ -0,0 +1,19 @@ |
||||
defmodule BlockScoutWeb.GraphQL.Middleware.ApiEnabled do |
||||
@moduledoc """ |
||||
Middleware to check if the GraphQL API is enabled. |
||||
""" |
||||
alias Absinthe.Resolution |
||||
|
||||
@behaviour Absinthe.Middleware |
||||
|
||||
@api_is_disabled "GraphQL API is disabled." |
||||
|
||||
def call(resolution, _config) do |
||||
if resolution.context.api_enabled do |
||||
resolution |
||||
else |
||||
resolution |
||||
|> Resolution.put_result({:error, @api_is_disabled}) |
||||
end |
||||
end |
||||
end |
@ -1,28 +1,19 @@ |
||||
defmodule BlockScoutWeb.GraphQL.Resolvers.Address do |
||||
@moduledoc false |
||||
|
||||
alias BlockScoutWeb.GraphQL.Resolvers.Helper |
||||
alias Explorer.Chain |
||||
|
||||
def get_by(_, %{hashes: hashes}, resolution) do |
||||
if resolution.context.api_enabled do |
||||
def get_by(_, %{hashes: hashes}, _) do |
||||
case Chain.hashes_to_addresses(hashes) do |
||||
[] -> {:error, "Addresses not found."} |
||||
result -> {:ok, result} |
||||
end |
||||
else |
||||
{:error, Helper.api_is_disabled()} |
||||
end |
||||
end |
||||
|
||||
def get_by(_, %{hash: hash}, resolution) do |
||||
if resolution.context.api_enabled do |
||||
def get_by(_, %{hash: hash}, _) do |
||||
case Chain.hash_to_address(hash) do |
||||
{:error, :not_found} -> {:error, "Address not found."} |
||||
{:ok, _} = result -> result |
||||
end |
||||
else |
||||
{:error, Helper.api_is_disabled()} |
||||
end |
||||
end |
||||
end |
||||
|
@ -1,29 +1,32 @@ |
||||
defmodule BlockScoutWeb.GraphQL.Resolvers.Block do |
||||
@moduledoc false |
||||
|
||||
alias BlockScoutWeb.GraphQL.Resolvers.Helper |
||||
alias Explorer.Chain |
||||
alias Explorer.Chain.Transaction |
||||
|
||||
@api_true [api?: true] |
||||
|
||||
def get_by(_, %{number: number}, resolution) do |
||||
with {:api_enabled, true} <- {:api_enabled, resolution.context.api_enabled}, |
||||
{:ok, _} = result <- Chain.number_to_block(number, @api_true) do |
||||
def get_by(_, %{number: number}, _) do |
||||
number |
||||
|> Chain.number_to_block(@api_true) |
||||
|> case do |
||||
{:ok, _} = result -> |
||||
result |
||||
else |
||||
{:api_enabled, false} -> {:error, Helper.api_is_disabled()} |
||||
{:error, :not_found} -> {:error, "Block number #{number} was not found."} |
||||
|
||||
{:error, :not_found} -> |
||||
{:error, "Block number #{number} was not found."} |
||||
end |
||||
end |
||||
|
||||
def get_by(%Transaction{block_hash: hash}, _, resolution) do |
||||
with {:api_enabled, true} <- {:api_enabled, resolution.context.api_enabled}, |
||||
{:ok, _} = result <- Chain.hash_to_block(hash, @api_true) do |
||||
def get_by(%Transaction{block_hash: hash}, _, _) do |
||||
hash |
||||
|> Chain.hash_to_block(@api_true) |
||||
|> case do |
||||
{:ok, _} = result -> |
||||
result |
||||
else |
||||
{:api_enabled, false} -> {:error, Helper.api_is_disabled()} |
||||
{:error, :not_found} -> {:error, "Block hash #{to_string(hash)} was not found."} |
||||
|
||||
{:error, :not_found} -> |
||||
{:error, "Block hash #{to_string(hash)} was not found."} |
||||
end |
||||
end |
||||
end |
||||
|
@ -1,9 +0,0 @@ |
||||
defmodule BlockScoutWeb.GraphQL.Resolvers.Helper do |
||||
@moduledoc """ |
||||
Helper functions for BlockScoutWeb.GraphQL.Resolvers modules |
||||
""" |
||||
|
||||
@api_is_disabled "GraphQL API is disabled." |
||||
|
||||
def api_is_disabled, do: @api_is_disabled |
||||
end |
@ -1,19 +1,10 @@ |
||||
defmodule BlockScoutWeb.GraphQL.Resolvers.Token do |
||||
@moduledoc false |
||||
|
||||
alias BlockScoutWeb.GraphQL.Resolvers.Helper |
||||
alias Explorer.Chain.TokenTransfer |
||||
alias Explorer.GraphQL |
||||
|
||||
def get_by( |
||||
%TokenTransfer{token_contract_address_hash: token_contract_address_hash}, |
||||
_, |
||||
resolution |
||||
) do |
||||
if resolution.context.api_enabled do |
||||
def get_by(%TokenTransfer{token_contract_address_hash: token_contract_address_hash}, _, _) do |
||||
GraphQL.get_token(%{contract_address_hash: token_contract_address_hash}) |
||||
else |
||||
{:error, Helper.api_is_disabled()} |
||||
end |
||||
end |
||||
end |
||||
|
Loading…
Reference in new issue