Skip to content

Commit

Permalink
Merge pull request #369 from CityOfZion/add_graphql_first_schema
Browse files Browse the repository at this point in the history
Add graphql first schema
  • Loading branch information
adrienmo committed Oct 29, 2018
2 parents 923090e + 704474d commit 4d59761
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 3 deletions.
12 changes: 12 additions & 0 deletions apps/neoscan_web/lib/neoscan_web/resolvers/block.ex
Expand Up @@ -21,4 +21,16 @@ defmodule NeoscanWeb.Resolvers.Block do

{:ok, resp}
end

def get(_, %{params: %{hash: hash}}, _) do
block = Blocks.get(hash)
if is_nil(block), do: {:error, "block not found"}, else: {:ok, block}
end

def get(_, %{params: %{index: index}}, _) do
block = Blocks.get(index)
if is_nil(block), do: {:error, "block not found"}, else: {:ok, block}
end

def get(_, _, _), do: {:error, "missing search parameter index or hash"}
end
25 changes: 22 additions & 3 deletions apps/neoscan_web/lib/neoscan_web/schema/block_types.ex
Expand Up @@ -5,20 +5,33 @@ defmodule NeoscanWeb.Schema.BlockTypes do

@desc "Queries for blocks"
object :block_queries do
@desc "Blocks query"
@desc """
Blocks query.
Get blocks, by default you will get the latest 20 blocks
"""
field :blocks, :blocks do
arg(:paginator, :paginator)
resolve(&Block.all/3)
end

@desc """
Block query.
Get information about one block.
Use the filter to get the block using the block by index or hash.
"""
field :block, :block_row do
arg(:params, non_null(:params))
resolve(&Block.get/3)
end
end

@desc "Blocks"
@desc "Blocks contains rows and pagination data"
object :blocks do
field(:block_rows, type: list_of(:block_row))
field(:pagination, type: :pagination)
end

@desc "Block"
@desc "a single block row"
object :block_row do
field(:cumulative_sys_fee, :decimal)
field(:gas_generated, :decimal)
Expand All @@ -43,4 +56,10 @@ defmodule NeoscanWeb.Schema.BlockTypes do
field(:invocation, :string)
field(:verification, :string)
end

@desc "parameters used to get a single block"
input_object :params do
field(:index, :integer)
field(:hash, :string)
end
end
105 changes: 105 additions & 0 deletions apps/neoscan_web/test/neoscan_web/schema/query/block_test.exs
Expand Up @@ -66,4 +66,109 @@ defmodule NeoscanWeb.Schema.Query.BlockTest do

assert length(rows) == 2
end

@query """
query Block($params: Params!){
block(params: $params){
cumulative_sys_fee
gasGenerated
hash
index
inserted_at
lag
merkle_root
next_consensus
nonce
script {
invocation
verification
}
}
}
"""

test "get one block by index", %{conn: conn} do
block = insert(:block)
index = block.index

conn =
post(
conn,
"/graphql",
%{
query: @query,
variables: %{
params: %{
index: index
}
}
}
)

body = json_response(conn, 200)

assert %{
"data" => %{
"block" => %{
"cumulative_sys_fee" => _,
"gasGenerated" => _,
"hash" => _,
"index" => ^index,
"inserted_at" => _,
"lag" => _,
"merkle_root" => _,
"next_consensus" => _,
"nonce" => _,
"script" => %{
"invocation" => _,
"verification" => _
}
}
}
} = body
end

test "get one block by hash", %{conn: conn} do
block = insert(:block)

hash =
block.hash
|> Base.encode16(case: :lower)

conn =
post(
conn,
"/graphql",
%{
query: @query,
variables: %{
params: %{
hash: block.hash
}
}
}
)

body = json_response(conn, 200)

assert %{
"data" => %{
"block" => %{
"cumulative_sys_fee" => _,
"gasGenerated" => _,
"hash" => ^hash,
"index" => _,
"inserted_at" => _,
"lag" => _,
"merkle_root" => _,
"next_consensus" => _,
"nonce" => _,
"script" => %{
"invocation" => _,
"verification" => _
}
}
}
} = body
end
end

0 comments on commit 4d59761

Please sign in to comment.