From 94db20426ae27cd3da022b5ed562aac241afa97b Mon Sep 17 00:00:00 2001 From: steffel fenix Date: Mon, 22 Oct 2018 21:18:55 +0800 Subject: [PATCH 1/3] add schema for single block query --- .../lib/neoscan_web/resolvers/block.ex | 12 +++ .../lib/neoscan_web/schema/block_types.ex | 25 ++++- .../neoscan_web/schema/query/block_test.exs | 102 ++++++++++++++++++ 3 files changed, 136 insertions(+), 3 deletions(-) diff --git a/apps/neoscan_web/lib/neoscan_web/resolvers/block.ex b/apps/neoscan_web/lib/neoscan_web/resolvers/block.ex index 0b6d5b4f..ce94372f 100644 --- a/apps/neoscan_web/lib/neoscan_web/resolvers/block.ex +++ b/apps/neoscan_web/lib/neoscan_web/resolvers/block.ex @@ -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 diff --git a/apps/neoscan_web/lib/neoscan_web/schema/block_types.ex b/apps/neoscan_web/lib/neoscan_web/schema/block_types.ex index 98accd91..dc0d81c0 100644 --- a/apps/neoscan_web/lib/neoscan_web/schema/block_types.ex +++ b/apps/neoscan_web/lib/neoscan_web/schema/block_types.ex @@ -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) @@ -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 diff --git a/apps/neoscan_web/test/neoscan_web/schema/query/block_test.exs b/apps/neoscan_web/test/neoscan_web/schema/query/block_test.exs index 094d4b44..170348e6 100644 --- a/apps/neoscan_web/test/neoscan_web/schema/query/block_test.exs +++ b/apps/neoscan_web/test/neoscan_web/schema/query/block_test.exs @@ -66,4 +66,106 @@ 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 + + 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 From 298285cc74a0ecf13d3a3073db1269f46460c65c Mon Sep 17 00:00:00 2001 From: steffel fenix Date: Fri, 26 Oct 2018 19:22:30 +0800 Subject: [PATCH 2/3] fix --- apps/neoscan_web/test/neoscan_web/schema/query/block_test.exs | 1 + 1 file changed, 1 insertion(+) diff --git a/apps/neoscan_web/test/neoscan_web/schema/query/block_test.exs b/apps/neoscan_web/test/neoscan_web/schema/query/block_test.exs index 170348e6..1f3700f8 100644 --- a/apps/neoscan_web/test/neoscan_web/schema/query/block_test.exs +++ b/apps/neoscan_web/test/neoscan_web/schema/query/block_test.exs @@ -131,6 +131,7 @@ defmodule NeoscanWeb.Schema.Query.BlockTest do test "get one block by hash", %{conn: conn} do block = insert(:block) hash = block.hash + |> Base.encode16(case: :lower) conn = post( From 704474d09a1d411bc6e41edea3bc6528e707bf21 Mon Sep 17 00:00:00 2001 From: Adrien Date: Mon, 29 Oct 2018 13:39:19 +0800 Subject: [PATCH 3/3] format --- .../test/neoscan_web/schema/query/block_test.exs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/apps/neoscan_web/test/neoscan_web/schema/query/block_test.exs b/apps/neoscan_web/test/neoscan_web/schema/query/block_test.exs index 1f3700f8..fe301aa2 100644 --- a/apps/neoscan_web/test/neoscan_web/schema/query/block_test.exs +++ b/apps/neoscan_web/test/neoscan_web/schema/query/block_test.exs @@ -130,8 +130,10 @@ defmodule NeoscanWeb.Schema.Query.BlockTest do test "get one block by hash", %{conn: conn} do block = insert(:block) - hash = block.hash - |> Base.encode16(case: :lower) + + hash = + block.hash + |> Base.encode16(case: :lower) conn = post(